Skip to content

Commit

Permalink
warning
Browse files Browse the repository at this point in the history
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@551 ec762483-ff6d-05da-a07a-a48fb63a330f
  • Loading branch information
[email protected] committed Aug 15, 2010
1 parent 66d9f91 commit 3b245a5
Show file tree
Hide file tree
Showing 7 changed files with 493 additions and 34 deletions.
10 changes: 6 additions & 4 deletions utils/indices_after.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ unsigned new_indices_keep_if_n(unsigned n,It i,KeepIf const& r,O out)
}

template <class KEEP,class O>
unsigned new_indices(KEEP keep,O out) {
return new_indices(keep.begin(),keep.end(),out);
unsigned new_indices_keep(KEEP keep,O out) {
return new_indices_keep(keep.begin(),keep.end(),out);
}

template <class V,class Out,class Permi>
Expand Down Expand Up @@ -129,8 +129,10 @@ struct indices_after
if (n_mapped>0) {
map=(unsigned *)::operator new(sizeof(unsigned)*n_mapped);
n_kept=new_indices_keep(i,end,map);
} else
} else {
n_kept=0;
map=NULL;
}
}
template <class A>
void init(A const& a)
Expand All @@ -139,7 +141,7 @@ struct indices_after
}

template <class A>
indices_after(A const& a)
explicit indices_after(A const& a)
{
init(a.begin(),a.end());
}
Expand Down
36 changes: 23 additions & 13 deletions utils/logval.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

#define LOGVAL_CHECK_NEG false

#include <boost/functional/hash.hpp>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <limits>

template <typename T>
template <class T>
class LogVal {
public:
LogVal() : s_(), v_(-std::numeric_limits<T>::infinity()) {}
Expand All @@ -23,6 +24,11 @@ class LogVal {
static LogVal<T> e() { return LogVal(1,false); }
void logeq(const T& v) { s_ = false; v_ = v; }

std::size_t hash_impl() const {
using namespace boost;
return hash_value(v_)+s_;
}

LogVal& operator+=(const LogVal& a) {
if (a.v_ == -std::numeric_limits<T>::infinity()) return *this;
if (a.s_ == s_) {
Expand Down Expand Up @@ -98,44 +104,44 @@ class LogVal {
};

// copy elision - as opposed to explicit copy of LogVal<T> const& o1, we should be able to construct Logval r=a+(b+c) as a single result in place in r. todo: return std::move(o1) - C++0x
template<typename T>
template<class T>
LogVal<T> operator+(LogVal<T> o1, const LogVal<T>& o2) {
o1 += o2;
return o1;
}

template<typename T>
template<class T>
LogVal<T> operator*(LogVal<T> o1, const LogVal<T>& o2) {
o1 *= o2;
return o1;
}

template<typename T>
template<class T>
LogVal<T> operator/(LogVal<T> o1, const LogVal<T>& o2) {
o1 /= o2;
return o1;
}

template<typename T>
template<class T>
LogVal<T> operator-(LogVal<T> o1, const LogVal<T>& o2) {
o1 -= o2;
return o1;
}

template<typename T>
template<class T>
T log(const LogVal<T>& o) {
#ifdef LOGVAL_CHECK_NEG
if (o.s_) return log(-1.0);
#endif
return o.v_;
}

template <typename T>
template <class T>
LogVal<T> pow(const LogVal<T>& b, const T& e) {
return b.pow(e);
}

template <typename T>
template <class T>
bool operator<(const LogVal<T>& lhs, const LogVal<T>& rhs) {
if (lhs.s_ == rhs.s_) {
return (lhs.v_ < rhs.v_);
Expand All @@ -145,28 +151,32 @@ bool operator<(const LogVal<T>& lhs, const LogVal<T>& rhs) {
}

#if 0
template <typename T>
template <class T>
bool operator<=(const LogVal<T>& lhs, const LogVal<T>& rhs) {
return (lhs.v_ <= rhs.v_);
}

template <typename T>
template <class T>
bool operator>(const LogVal<T>& lhs, const LogVal<T>& rhs) {
return (lhs.v_ > rhs.v_);
}

template <typename T>
template <class T>
bool operator>=(const LogVal<T>& lhs, const LogVal<T>& rhs) {
return (lhs.v_ >= rhs.v_);
}
#endif

template <typename T>

template <class T>
std::size_t hash_value(const LogVal<T>& x) { return x.hash_impl(); }

template <class T>
bool operator==(const LogVal<T>& lhs, const LogVal<T>& rhs) {
return (lhs.v_ == rhs.v_) && (lhs.s_ == rhs.s_);
}

template <typename T>
template <class T>
bool operator!=(const LogVal<T>& lhs, const LogVal<T>& rhs) {
return !(lhs == rhs);
}
Expand Down
136 changes: 119 additions & 17 deletions utils/sparse_vector.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#ifndef _SPARSE_VECTOR_H_
#define _SPARSE_VECTOR_H_

/*
TODO: specialize for int value types, where it probably makes sense to check if adding/subtracting brings a value to 0, and remove it from the map (e.g. in a gibbs sampler). or add a separate policy argument for that.
*/

//#define SPARSE_VECTOR_HASH
// if defined, use hash_map rather than map. map is probably faster/smaller for small vectors

#ifdef SPARSE_VECTOR_HASH
#include "hash.h"
# define SPARSE_VECTOR_MAP HASH_MAP
# define SPARSE_VECTOR_MAP_RESERVED(h,empty,deleted) HASH_MAP_RESERVED(h,empty,deleted)
#else
# define SPARSE_VECTOR_MAP std::map
# define SPARSE_VECTOR_MAP_RESERVED(h,empty,deleted)
#endif
/*
use SparseVectorList (pair smallvector) for feat funcs / hypergraphs (you rarely need random access; just append a feature to the list)
*/
Expand Down Expand Up @@ -38,6 +35,17 @@
// this is a modified version of code originally written
// by Phil Blunsom

#include <boost/functional/hash.hpp>
#include <stdexcept>
#ifdef SPARSE_VECTOR_HASH
#include "hash.h"
# define SPARSE_VECTOR_MAP HASH_MAP
# define SPARSE_VECTOR_MAP_RESERVED(h,empty,deleted) HASH_MAP_RESERVED(h,empty,deleted)
#else
# define SPARSE_VECTOR_MAP std::map
# define SPARSE_VECTOR_MAP_RESERVED(h,empty,deleted)
#endif

#include <iostream>
#include <map>
#include <tr1/unordered_map>
Expand All @@ -46,6 +54,7 @@

#include "fdict.h"
#include "small_vector.h"
#include "string_to.h"

template <class T>
inline T & extend_vector(std::vector<T> &v,int i) {
Expand All @@ -54,7 +63,7 @@ inline T & extend_vector(std::vector<T> &v,int i) {
return v[i];
}

template <typename T>
template <class T>
class SparseVector {
void init_reserved() {
SPARSE_VECTOR_MAP_RESERVED(values_,-1,-2);
Expand All @@ -71,17 +80,97 @@ class SparseVector {
SparseVector() {
init_reserved();
}
typedef typename MapType::value_type value_type;
typedef typename MapType::iterator iterator;
explicit SparseVector(std::vector<T> const& v) {
init_reserved();
typename MapType::iterator p=values_.begin();
iterator p=values_.begin();
const T z=0;
for (unsigned i=0;i<v.size();++i) {
T const& t=v[i];
if (t!=z)
p=values_.insert(p,typename MapType::value_type(i,t)); //hint makes insertion faster
p=values_.insert(p,value_type(i,t)); //hint makes insertion faster
}
}

typedef char const* Str;
template <class O>
void print(O &o,Str pre="",Str post="",Str kvsep="=",Str pairsep=" ") const {
o << pre;
bool first=true;
for (const_iterator i=values_.begin(),e=values_.end();i!=e;++i) {
if (first)
first=false;
else
o<<pairsep;
o<<FD::Convert(i->first)<<kvsep<<i->second;
}
o << post;
}

static void error(std::string const& msg) {
throw std::runtime_error("SparseVector: "+msg);
}

enum DupPolicy {
NO_DUPS,
KEEP_FIRST,
KEEP_LAST,
SUM
};

// either key val alternating whitespace sep, or key=val (kvsep char is '='). end at eof or terminator (non-ws) char
template <class S>
void read(S &s,DupPolicy dp=NO_DUPS,bool use_kvsep=true,char kvsep='=',bool stop_at_terminator=false,char terminator=')') {
values_.clear();
std::string id;
WordID k;
T v;
#undef SPARSE_MUST_READ
#define SPARSE_MUST_READ(x) if (!(x)) error(#x);
int ki;
while (s) {
if (stop_at_terminator) {
char c;
if (!(s>>c)) goto eof;
s.unget();
if (c==terminator) return;
}
if (!(s>>id)) goto eof;
if (use_kvsep && (ki=id.find(kvsep))!=std::string::npos) {
k=FD::Convert(std::string(id,0,ki));
string_into(id.c_str()+ki+1,v);
} else {
k=FD::Convert(id);
if (!(s>>v)) error("reading value failed");
}
std::pair<iterator,bool> vi=values_.insert(value_type(k,v));
if (vi.second) {
T &oldv=vi.first->second;
switch(dp) {
case NO_DUPS: error("read duplicate key with NO_DUPS. key="
+FD::Convert(k)+" val="+to_string(v)+" old-val="+to_string(oldv));
break;
case KEEP_FIRST: break;
case KEEP_LAST: oldv=v; break;
case SUM: oldv+=v; break;
}
}
}
return;
eof:
if (!s.eof()) error("reading key failed (before EOF)");
}

friend inline std::ostream & operator<<(std::ostream &o,Self const& s) {
s.print(o);
return o;
}

friend inline std::istream & operator>>(std::istream &o,Self & s) {
s.read(o);
return o;
}

void init_vector(std::vector<T> *vp) const {
init_vector(*vp);
Expand Down Expand Up @@ -118,6 +207,10 @@ class SparseVector {
return values_[index];
}

inline void maybe_set_value(int index, const T &value) {
if (value) values_[index] = value;
}

inline void set_value(int index, const T &value) {
values_[index] = value;
}
Expand Down Expand Up @@ -352,6 +445,10 @@ class SparseVector {
return size()==other.size() && contains_keys_of(other) && other.contains_i(*this);
}

std::size_t hash_impl() const {
return boost::hash_range(begin(),end());
}

bool contains(Self const &o) const {
return size()>o.size() && contains(o);
}
Expand All @@ -371,7 +468,7 @@ class SparseVector {

bool contains_keys_of(Self const& o) const {
for (typename MapType::const_iterator i=o.begin(),e=o.end();i!=e;++i)
if (values_.find(i)==values_.end())
if (values_.find(i->first)==values_.end())
return false;
return true;
}
Expand Down Expand Up @@ -478,31 +575,36 @@ class SparseVectorList {
List p;
};

template <typename T>
template <class T>
std::size_t hash_value(SparseVector<T> const& x) {
return x.hash_impl();
}

template <class T>
SparseVector<T> operator+(const SparseVector<T>& a, const SparseVector<T>& b) {
SparseVector<T> result = a;
return result += b;
}

template <typename T>
template <class T>
SparseVector<T> operator*(const SparseVector<T>& a, const double& b) {
SparseVector<T> result = a;
return result *= b;
}

template <typename T>
template <class T>
SparseVector<T> operator*(const SparseVector<T>& a, const T& b) {
SparseVector<T> result = a;
return result *= b;
}

template <typename T>
template <class T>
SparseVector<T> operator*(const double& a, const SparseVector<T>& b) {
SparseVector<T> result = b;
return result *= a;
}

template <typename T>
template <class T>
std::ostream &operator<<(std::ostream &out, const SparseVector<T> &vec)
{
return vec.operator<<(out);
Expand Down
Loading

0 comments on commit 3b245a5

Please sign in to comment.