11 #ifndef __RD_SPARSE_INT_VECT_20070921__ 12 #define __RD_SPARSE_INT_VECT_20070921__ 20 #include <boost/cstdint.hpp> 26 template <
typename IndexType>
38 d_length = other.d_length;
39 d_data.insert(other.d_data.begin(), other.d_data.end());
44 initFromText(pkl.c_str(), pkl.size());
48 initFromText(pkl, len);
55 #pragma clang diagnostic push 56 #pragma clang diagnostic ignored "-Wtautological-compare" 57 #elif (defined(__GNUC__) || defined(__GNUG__)) && \ 58 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 1)) 59 #if (__GNUC__ > 4 || __GNUC_MINOR__ > 5) 60 #pragma GCC diagnostic push 62 #pragma GCC diagnostic ignored "-Wtype-limits" 66 if (idx < 0 || idx >= d_length) {
70 typename StorageType::const_iterator iter = d_data.find(idx);
71 if (iter != d_data.end()) {
78 void setVal(IndexType idx,
int val) {
79 if (idx < 0 || idx >= d_length) {
89 #pragma clang diagnostic pop 90 #elif (defined(__GNUC__) || defined(__GNUG__)) && \ 91 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5)) 92 #pragma GCC diagnostic pop 104 typename StorageType::const_iterator iter;
105 for (iter = d_data.begin(); iter != d_data.end(); ++iter) {
109 res += abs(iter->second);
123 if (other.d_length != d_length) {
127 typename StorageType::iterator iter = d_data.begin();
128 typename StorageType::const_iterator oIter = other.d_data.begin();
129 while (iter != d_data.end()) {
131 while (oIter != other.d_data.end() && oIter->first < iter->first) {
134 if (oIter != other.d_data.end() && oIter->first == iter->first) {
136 if (oIter->second < iter->second) {
137 iter->second = oIter->second;
144 typename StorageType::iterator tmpIter = iter;
162 if (other.d_length != d_length) {
166 typename StorageType::iterator iter = d_data.begin();
167 typename StorageType::const_iterator oIter = other.d_data.begin();
168 while (iter != d_data.end()) {
170 while (oIter != other.d_data.end() && oIter->first < iter->first) {
171 d_data[oIter->first] = oIter->second;
174 if (oIter != other.d_data.end() && oIter->first == iter->first) {
176 if (oIter->second > iter->second) {
177 iter->second = oIter->second;
184 while (oIter != other.d_data.end()) {
185 d_data[oIter->first] = oIter->second;
197 if (other.d_length != d_length) {
200 typename StorageType::iterator iter = d_data.begin();
201 typename StorageType::const_iterator oIter = other.d_data.begin();
202 while (oIter != other.d_data.end()) {
203 while (iter != d_data.end() && iter->first < oIter->first) {
206 if (iter != d_data.end() && oIter->first == iter->first) {
208 iter->second += oIter->second;
210 typename StorageType::iterator tIter = iter;
218 d_data[oIter->first] = oIter->second;
231 if (other.d_length != d_length) {
234 typename StorageType::iterator iter = d_data.begin();
235 typename StorageType::const_iterator oIter = other.d_data.begin();
236 while (oIter != other.d_data.end()) {
237 while (iter != d_data.end() && iter->first < oIter->first) {
240 if (iter != d_data.end() && oIter->first == iter->first) {
242 iter->second -= oIter->second;
244 typename StorageType::iterator tIter = iter;
252 d_data[oIter->first] = -oIter->second;
264 typename StorageType::iterator iter = d_data.begin();
265 while (iter != d_data.end()) {
276 typename StorageType::iterator iter = d_data.begin();
277 while (iter != d_data.end()) {
288 typename StorageType::iterator iter = d_data.begin();
289 while (iter != d_data.end()) {
300 typename StorageType::iterator iter = d_data.begin();
301 while (iter != d_data.end()) {
313 if (d_length != v2.d_length) {
316 return d_data == v2.d_data;
319 return !(*
this == v2);
324 std::stringstream ss(std::ios_base::binary | std::ios_base::out |
326 boost::uint32_t tInt;
329 tInt =
sizeof(IndexType);
332 IndexType nEntries = d_data.size();
335 typename StorageType::const_iterator iter = d_data.begin();
336 while (iter != d_data.end()) {
338 boost::int32_t tInt = iter->second;
346 initFromText(txt.c_str(), txt.length());
353 void initFromText(
const char *pkl,
const unsigned int len) {
355 std::stringstream ss(std::ios_base::binary | std::ios_base::out |
359 boost::uint32_t vers;
361 if (vers == 0x0001) {
362 boost::uint32_t tInt;
364 if (tInt >
sizeof(IndexType)) {
366 "IndexType cannot accomodate index size in SparseIntVect pickle");
370 readVals<unsigned char>(ss);
372 case sizeof(boost::int32_t):
373 readVals<boost::uint32_t>(ss);
375 case sizeof(boost::int64_t):
376 readVals<boost::uint64_t>(ss);
385 template <
typename T>
386 void readVals(std::stringstream &ss) {
387 PRECONDITION(
sizeof(T) <=
sizeof(IndexType),
"invalid size");
393 for (T i = 0; i < nEntries; ++i) {
402 template <
typename IndexType,
typename SequenceType>
404 const SequenceType &seq) {
405 typename SequenceType::const_iterator seqIt;
406 for (seqIt = seq.begin(); seqIt != seq.end(); ++seqIt) {
408 IndexType idx = *seqIt;
414 template <
typename IndexType>
417 double &v2Sum,
double &andSum) {
421 v1Sum = v2Sum = andSum = 0.0;
431 iter2->first < iter1->first) {
436 if (iter2->first == iter1->first) {
437 if (abs(iter2->second) < abs(iter1->second)) {
438 andSum += abs(iter2->second);
440 andSum += abs(iter1->second);
454 v1Sum += abs(iter1->second);
461 v2Sum += abs(iter2->second);
468 template <
typename IndexType>
471 bool returnDistance =
false,
double bounds = 0.0) {
477 if (!returnDistance && bounds > 0.0) {
480 double denom = v1Sum + v2Sum;
481 if (fabs(denom) < 1e-6) {
482 if (returnDistance) {
488 double minV = v1Sum < v2Sum ? v1Sum : v2Sum;
489 if (2. * minV / denom < bounds) {
498 calcVectParams(v1, v2, v1Sum, v2Sum, numer);
500 double denom = v1Sum + v2Sum;
502 if (fabs(denom) < 1e-6) {
505 sim = 2. * numer / denom;
507 if (returnDistance) sim = 1. - sim;
512 template <
typename IndexType>
515 bool returnDistance =
false,
double bounds = 0.0) {
524 calcVectParams(v1, v2, v1Sum, v2Sum, andSum);
526 double denom = a * v1Sum + b * v2Sum + (1 - a - b) * andSum;
529 if (fabs(denom) < 1e-6) {
532 sim = andSum / denom;
534 if (returnDistance) sim = 1. - sim;
539 template <
typename IndexType>
542 bool returnDistance =
false,
double bounds = 0.0) {
const SparseIntVect< IndexType > operator|(const SparseIntVect< IndexType > &other) const
double DiceSimilarity(const SparseIntVect< IndexType > &v1, const SparseIntVect< IndexType > &v2, bool returnDistance=false, double bounds=0.0)
SparseIntVect(const char *pkl, const unsigned int len)
constructor from a pickle
const SparseIntVect< IndexType > operator &(const SparseIntVect< IndexType > &other) const
void updateFromSequence(SparseIntVect< IndexType > &vect, const SequenceType &seq)
const int ci_SPARSEINTVECT_VERSION
version number to use in pickles
const SparseIntVect< IndexType > operator-(const SparseIntVect< IndexType > &other) const
void streamRead(std::istream &ss, T &loc)
does a binary read of an object from a stream
~SparseIntVect()
destructor (doesn't need to do anything)
const StorageType & getNonzeroElements() const
returns our nonzero elements as a map(IndexType->int)
std::map< IndexType, int > StorageType
SparseIntVect< IndexType > & operator*(int v)
SparseIntVect(const SparseIntVect< IndexType > &other)
Copy constructor.
const SparseIntVect< IndexType > operator+(const SparseIntVect< IndexType > &other) const
int getVal(IndexType idx) const
return the value at an index
IndexType getLength() const
returns the length
double TanimotoSimilarity(const SparseIntVect< IndexType > &v1, const SparseIntVect< IndexType > &v2, bool returnDistance=false, double bounds=0.0)
SparseIntVect< IndexType > & operator/=(int v)
SparseIntVect< IndexType > & operator/(int v)
int operator[](IndexType idx) const
support indexing using []
SparseIntVect< IndexType > & operator-=(const SparseIntVect< IndexType > &other)
Class to allow us to throw an IndexError from C++ and have it make it back to Python.
unsigned int size() const
returns the length
#define RDUNUSED_PARAM(x)
SparseIntVect(IndexType length)
initialize with a particular length
SparseIntVect< IndexType > & operator*=(int v)
double TverskySimilarity(const SparseIntVect< IndexType > &v1, const SparseIntVect< IndexType > &v2, double a, double b, bool returnDistance=false, double bounds=0.0)
int getTotalVal(bool doAbs=false) const
bool operator!=(const SparseIntVect< IndexType > &v2) const
void streamWrite(std::ostream &ss, const T &val)
does a binary write of an object to a stream
SparseIntVect< IndexType > & operator &=(const SparseIntVect< IndexType > &other)
#define PRECONDITION(expr, mess)
SparseIntVect(const std::string &pkl)
constructor from a pickle
a class for efficiently storing sparse vectors of ints
void setVal(IndexType idx, int val)
set the value at an index
SparseIntVect< IndexType > & operator+=(const SparseIntVect< IndexType > &other)
Class to allow us to throw a ValueError from C++ and have it make it back to Python.
SparseIntVect< IndexType > & operator-=(int v)
SparseIntVect< IndexType > & operator|=(const SparseIntVect< IndexType > &other)
SparseIntVect< IndexType > & operator+=(int v)
SparseIntVect< IndexType > & operator+(int v)
std::string toString() const
returns a binary string representation (pickle)
bool operator==(const SparseIntVect< IndexType > &v2) const
void fromString(const std::string &txt)
SparseIntVect< IndexType > & operator-(int v)