summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorPatrick Simianer <simianer@cl.uni-heidelberg.de>2013-11-28 11:11:22 +0100
committerPatrick Simianer <simianer@cl.uni-heidelberg.de>2013-11-28 11:11:22 +0100
commitab02696b1c104febc7f13c896acf4165f2721018 (patch)
tree61969895daa79d1d67c90f4adc1de7d91ef3cdfd /utils
parentab63f2f2988e0093a721d0599c7fe68e183561d8 (diff)
parente346cd5cd3c5d7164819c35e485a9850d825996e (diff)
Merge branch 'master' of github.com:pks/cdec-dtrain
Diffstat (limited to 'utils')
-rw-r--r--utils/Makefile.am8
-rw-r--r--utils/small_vector.h12
-rw-r--r--utils/small_vector_test.cc12
-rw-r--r--utils/sv_test.cc24
-rw-r--r--utils/swap_pod.h23
-rw-r--r--utils/value_array.h9
-rw-r--r--utils/weights.cc10
7 files changed, 59 insertions, 39 deletions
diff --git a/utils/Makefile.am b/utils/Makefile.am
index c5fedb78..a22b6727 100644
--- a/utils/Makefile.am
+++ b/utils/Makefile.am
@@ -7,9 +7,10 @@ noinst_PROGRAMS = \
m_test \
weights_test \
logval_test \
- small_vector_test
+ small_vector_test \
+ sv_test
-TESTS = ts small_vector_test logval_test weights_test dict_test m_test
+TESTS = ts small_vector_test logval_test weights_test dict_test m_test sv_test
noinst_LIBRARIES = libutils.a
@@ -50,7 +51,6 @@ libutils_a_SOURCES = \
sparse_vector.h \
static_utoa.h \
stringlib.h \
- swap_pod.h \
tdict.h \
timing_stats.h \
utoa.h \
@@ -101,6 +101,8 @@ logval_test_SOURCES = logval_test.cc
logval_test_LDADD = libutils.a $(BOOST_UNIT_TEST_FRAMEWORK_LDFLAGS) $(BOOST_UNIT_TEST_FRAMEWORK_LIBS)
small_vector_test_SOURCES = small_vector_test.cc
small_vector_test_LDADD = libutils.a $(BOOST_UNIT_TEST_FRAMEWORK_LDFLAGS) $(BOOST_UNIT_TEST_FRAMEWORK_LIBS)
+sv_test_SOURCES = sv_test.cc
+sv_test_LDADD = libutils.a $(BOOST_UNIT_TEST_FRAMEWORK_LDFLAGS) $(BOOST_UNIT_TEST_FRAMEWORK_LIBS)
################################################################
# do NOT NOT NOT add any other -I includes NO NO NO NO NO ######
diff --git a/utils/small_vector.h b/utils/small_vector.h
index c8a69927..280ab72c 100644
--- a/utils/small_vector.h
+++ b/utils/small_vector.h
@@ -14,7 +14,6 @@
#include <stdint.h>
#include <new>
#include <stdint.h>
-#include "swap_pod.h"
#include <boost/functional/hash.hpp>
//sizeof(T)/sizeof(T*)>1?sizeof(T)/sizeof(T*):1
@@ -278,8 +277,15 @@ public:
return !(a==b);
}
- void swap(Self& o) {
- swap_pod(*this,o);
+ inline void swap(Self& o) {
+ const unsigned s=sizeof(SmallVector<T,SV_MAX>);
+ char tmp[s];
+ void *pt=static_cast<void*>(tmp);
+ void *pa=static_cast<void*>(this);
+ void *pb=static_cast<void*>(&o);
+ std::memcpy(pt,pa,s);
+ std::memcpy(pa,pb,s);
+ std::memcpy(pb,pt,s);
}
inline std::size_t hash_impl() const {
diff --git a/utils/small_vector_test.cc b/utils/small_vector_test.cc
index cded4619..a4eb89ae 100644
--- a/utils/small_vector_test.cc
+++ b/utils/small_vector_test.cc
@@ -82,6 +82,18 @@ BOOST_AUTO_TEST_CASE(LargerThan2) {
BOOST_CHECK(cc.size() == 0);
}
+BOOST_AUTO_TEST_CASE(SwapSV) {
+ SmallVectorInt v;
+ SmallVectorInt v2(2, 10);
+ SmallVectorInt v3(2, 10);
+ BOOST_CHECK(v2 == v3);
+ BOOST_CHECK(v != v3);
+ v.swap(v2);
+ BOOST_CHECK(v == v3);
+ SmallVectorInt v4;
+ BOOST_CHECK(v4 == v2);
+}
+
BOOST_AUTO_TEST_CASE(Small) {
SmallVectorInt v;
SmallVectorInt v1(1,0);
diff --git a/utils/sv_test.cc b/utils/sv_test.cc
new file mode 100644
index 00000000..c7ac9e54
--- /dev/null
+++ b/utils/sv_test.cc
@@ -0,0 +1,24 @@
+#define BOOST_TEST_MODULE WeightsTest
+#include <boost/test/unit_test.hpp>
+#include <boost/test/floating_point_comparison.hpp>
+#include "sparse_vector.h"
+
+using namespace std;
+
+BOOST_AUTO_TEST_CASE(Equality) {
+ SparseVector<double> x;
+ SparseVector<double> y;
+ x.set_value(1,-1);
+ y.set_value(1,-1);
+ BOOST_CHECK(x == y);
+}
+
+BOOST_AUTO_TEST_CASE(Division) {
+ SparseVector<double> x;
+ SparseVector<double> y;
+ x.set_value(1,1);
+ y.set_value(1,-1);
+ BOOST_CHECK(!(x == y));
+ x /= -1;
+ BOOST_CHECK(x == y);
+}
diff --git a/utils/swap_pod.h b/utils/swap_pod.h
deleted file mode 100644
index bb9a830d..00000000
--- a/utils/swap_pod.h
+++ /dev/null
@@ -1,23 +0,0 @@
-#ifndef SWAP_POD_H
-#define SWAP_POD_H
-
-//for swapping objects of the same concrete type where just swapping their bytes will work. will at least work on plain old data.
-
-#include <algorithm> // not used, but people who use this will want to bring std::swap in anyway
-#include <cstring>
-
-template <class T>
-inline void swap_pod(T &a,T &b) {
- using namespace std;
- const unsigned s=sizeof(T);
- char tmp[s];
- void *pt=(void*)tmp;
- void *pa=(void*)&a;
- void *pb=(void*)&b;
- memcpy(pt,pa,s);
- memcpy(pa,pb,s);
- memcpy(pb,pt,s);
-}
-
-
-#endif
diff --git a/utils/value_array.h b/utils/value_array.h
index 12fc9d87..e59349b5 100644
--- a/utils/value_array.h
+++ b/utils/value_array.h
@@ -1,8 +1,6 @@
#ifndef VALUE_ARRAY_H
#define VALUE_ARRAY_H
-//TODO: option for non-constructed version (type_traits pod?), option for small array optimization (if sz < N, store inline in union, see small_vector.h)
-
#define DBVALUEARRAY(x) x
#include <cstdlib>
@@ -30,8 +28,7 @@
// valarray like in that size is fixed (so saves space compared to vector), but same interface as vector (less resize/push_back/insert, of course)
template <class T, class A = std::allocator<T> >
-class ValueArray : A // private inheritance so stateless allocator adds no size.
-{
+class ValueArray : A { // private inheritance so stateless allocator adds no size.
typedef ValueArray<T,A> Self;
public:
#if VALUE_ARRAY_OSTREAM
@@ -323,14 +320,14 @@ private:
//friend class boost::serialization::access;
public:
template <class Archive>
- void save(Archive& ar, unsigned int version) const
+ void save(Archive& ar, unsigned int /*version*/) const
{
ar << sz;
for (size_type i = 0; i != sz; ++i) ar << at(i);
}
template <class Archive>
- void load(Archive& ar, unsigned int version)
+ void load(Archive& ar, unsigned int /*version*/)
{
size_type s;
ar >> s;
diff --git a/utils/weights.cc b/utils/weights.cc
index effdfc5e..1f66c441 100644
--- a/utils/weights.cc
+++ b/utils/weights.cc
@@ -127,9 +127,11 @@ void Weights::InitSparseVector(const vector<weight_t>& dv,
}
void Weights::SanityCheck(const vector<weight_t>& w) {
- for (unsigned i = 0; i < w.size(); ++i) {
- assert(!std::isnan(w[i]));
- assert(!std::isinf(w[i]));
+ for (unsigned i = 1; i < w.size(); ++i) {
+ if (std::isnan(w[i]) || std::isinf(w[i])) {
+ cerr << FD::Convert(i) << " has bad weight: " << w[i] << endl;
+ abort();
+ }
}
}
@@ -161,7 +163,7 @@ string Weights::GetString(const vector<weight_t>& w,
bool hide_zero_value_features) {
ostringstream os;
os.precision(17);
- int nf = FD::NumFeats();
+ const unsigned nf = FD::NumFeats();
for (unsigned i = 1; i < nf; i++) {
weight_t val = (i < w.size() ? w[i] : 0.0);
if (hide_zero_value_features && val == 0.0) {