11#ifndef __RD_SPARSE_INT_VECT_20070921__
12#define __RD_SPARSE_INT_VECT_20070921__
27template <
typename IndexType>
39 d_length =
other.d_length;
41 d_data.insert(
other.d_data.begin(),
other.d_data.end());
46 initFromText(
pkl.c_str(),
pkl.size());
57 d_length =
other.d_length;
59 d_data.insert(
other.d_data.begin(),
other.d_data.end());
67#pragma clang diagnostic push
68#pragma clang diagnostic ignored "-Wtautological-compare"
69#elif (defined(__GNUC__) || defined(__GNUG__)) && \
70 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 1))
71#if (__GNUC__ > 4 || __GNUC_MINOR__ > 5)
72#pragma GCC diagnostic push
74#pragma GCC diagnostic ignored "-Wtype-limits"
78 if (!checkIndex(idx)) {
82 typename StorageType::const_iterator
iter = d_data.find(idx);
83 if (
iter != d_data.end()) {
91 if (!checkIndex(idx)) {
101#pragma clang diagnostic pop
102#elif (defined(__GNUC__) || defined(__GNUG__)) && \
103 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5))
104#pragma GCC diagnostic pop
116 typename StorageType::const_iterator
iter;
117 for (
iter = d_data.begin();
iter != d_data.end(); ++
iter) {
136 if (
other.d_length != d_length) {
140 typename StorageType::iterator
iter = d_data.begin();
141 typename StorageType::const_iterator
oIter =
other.d_data.begin();
142 while (
iter != d_data.end()) {
175 if (
other.d_length != d_length) {
179 typename StorageType::iterator
iter = d_data.begin();
180 typename StorageType::const_iterator
oIter =
other.d_data.begin();
181 while (
iter != d_data.end()) {
210 if (
other.d_length != d_length) {
213 typename StorageType::iterator
iter = d_data.begin();
214 typename StorageType::const_iterator
oIter =
other.d_data.begin();
223 typename StorageType::iterator
tIter =
iter;
244 if (
other.d_length != d_length) {
247 typename StorageType::iterator
iter = d_data.begin();
248 typename StorageType::const_iterator
oIter =
other.d_data.begin();
257 typename StorageType::iterator
tIter =
iter;
277 typename StorageType::iterator
iter = d_data.begin();
278 while (
iter != d_data.end()) {
289 typename StorageType::iterator
iter = d_data.begin();
290 while (
iter != d_data.end()) {
301 typename StorageType::iterator
iter = d_data.begin();
302 while (
iter != d_data.end()) {
313 typename StorageType::iterator
iter = d_data.begin();
314 while (
iter != d_data.end()) {
326 if (d_length != v2.d_length) {
329 return d_data == v2.d_data;
332 return !(*
this == v2);
337 std::stringstream
ss(std::ios_base::binary | std::ios_base::out |
348 typename StorageType::const_iterator
iter = d_data.begin();
349 while (
iter != d_data.end()) {
359 initFromText(
txt.c_str(),
txt.length());
366 void initFromText(
const char *
pkl,
const unsigned int len) {
368 std::stringstream
ss(std::ios_base::binary | std::ios_base::out |
374 if (
vers == 0x0001) {
379 "IndexType cannot accommodate index size in SparseIntVect pickle");
385 case sizeof(std::int32_t):
388 case sizeof(boost::int64_t):
398 template <
typename T>
399 void readVals(std::stringstream &
ss) {
415 (idx == d_length && d_length < std::numeric_limits<IndexType>::max())) {
422template <
typename IndexType,
typename SequenceType>
425 typename SequenceType::const_iterator
seqIt;
429 vect.setVal(idx,
vect.getVal(idx) + 1);
434template <
typename IndexType>
438 if (v1.getLength() != v2.getLength()) {
444 typename SparseIntVect<IndexType>::StorageType::const_iterator
iter1,
iter2;
445 iter1 = v1.getNonzeroElements().begin();
446 if (
iter1 != v1.getNonzeroElements().end()) {
449 iter2 = v2.getNonzeroElements().begin();
450 if (
iter2 != v2.getNonzeroElements().end()) {
453 while (
iter1 != v1.getNonzeroElements().end()) {
454 while (
iter2 != v2.getNonzeroElements().end() &&
457 if (
iter2 != v2.getNonzeroElements().end()) {
461 if (
iter2 != v2.getNonzeroElements().end()) {
469 if (
iter2 != v2.getNonzeroElements().end()) {
474 if (
iter1 != v1.getNonzeroElements().end()) {
481 if (
iter1 != v1.getNonzeroElements().end()) {
483 while (
iter1 != v1.getNonzeroElements().end()) {
488 if (
iter2 != v2.getNonzeroElements().end()) {
490 while (
iter2 != v2.getNonzeroElements().end()) {
498template <
typename IndexType>
541template <
typename IndexType>
570template <
typename IndexType>
#define RDUNUSED_PARAM(x)
#define PRECONDITION(expr, mess)
const int ci_SPARSEINTVECT_VERSION
version number to use in pickles
Class to allow us to throw an IndexError from C++ and have it make it back to Python.
a class for efficiently storing sparse vectors of ints
const StorageType & getNonzeroElements() const
returns our nonzero elements as a map(IndexType->int)
SparseIntVect< IndexType > & operator&=(const SparseIntVect< IndexType > &other)
SparseIntVect(IndexType length)
initialize with a particular length
SparseIntVect< IndexType > & operator-=(int v)
unsigned int size() const
returns the length
SparseIntVect< IndexType > & operator+(int v)
SparseIntVect< IndexType > & operator-(int v)
SparseIntVect< IndexType > & operator/(int v)
bool operator==(const SparseIntVect< IndexType > &v2) const
SparseIntVect(const SparseIntVect< IndexType > &other)
Copy constructor.
~SparseIntVect()=default
destructor (doesn't need to do anything)
const SparseIntVect< IndexType > operator&(const SparseIntVect< IndexType > &other) const
SparseIntVect< IndexType > & operator+=(int v)
SparseIntVect(const char *pkl, const unsigned int len)
constructor from a pickle
int operator[](IndexType idx) const
support indexing using []
void fromString(const std::string &txt)
void setVal(IndexType idx, int val)
set the value at an index
SparseIntVect< IndexType > & operator|=(const SparseIntVect< IndexType > &other)
std::string toString() const
returns a binary string representation (pickle)
int getTotalVal(bool doAbs=false) const
SparseIntVect< IndexType > & operator-=(const SparseIntVect< IndexType > &other)
const SparseIntVect< IndexType > operator+(const SparseIntVect< IndexType > &other) const
std::map< IndexType, int > StorageType
SparseIntVect & operator=(const SparseIntVect< IndexType > &other)
bool operator!=(const SparseIntVect< IndexType > &v2) const
SparseIntVect< IndexType > & operator*=(int v)
SparseIntVect< IndexType > & operator+=(const SparseIntVect< IndexType > &other)
SparseIntVect< IndexType > & operator*(int v)
SparseIntVect(const std::string &pkl)
constructor from a pickle
SparseIntVect< IndexType > & operator/=(int v)
const SparseIntVect< IndexType > operator-(const SparseIntVect< IndexType > &other) const
IndexType getLength() const
returns the length
int getVal(IndexType idx) const
return the value at an index
const SparseIntVect< IndexType > operator|(const SparseIntVect< IndexType > &other) const
Class to allow us to throw a ValueError from C++ and have it make it back to Python.
bool rdvalue_is(const RDValue_cast_t)
double TverskySimilarity(const SparseIntVect< IndexType > &v1, const SparseIntVect< IndexType > &v2, double a, double b, bool returnDistance=false, double bounds=0.0)
void updateFromSequence(SparseIntVect< IndexType > &vect, const SequenceType &seq)
double TanimotoSimilarity(const SparseIntVect< IndexType > &v1, const SparseIntVect< IndexType > &v2, bool returnDistance=false, double bounds=0.0)
double DiceSimilarity(const SparseIntVect< IndexType > &v1, const SparseIntVect< IndexType > &v2, bool returnDistance=false, double bounds=0.0)
void streamRead(std::istream &ss, T &loc)
does a binary read of an object from a stream
void streamWrite(std::ostream &ss, const T &val)
does a binary write of an object to a stream