Template Class SparseVector

Class Documentation

template<typename T>
class SparseVector

A vector that imitates a map with unsigned integer keys. This class has faster access than a map, but needs space linear in the maximum key value.

Public Functions

SparseVector()
explicit SparseVector(index size)

Construct an empty vector. Empty values are created using the default constructor.

Parameters:

size – upper bound for the maximum usable index

SparseVector(index size, T emptyValue)

Construct an empty vector.

Parameters:
  • size – upper bound for the maximum usable index

  • emptyValue – value used for empty entries

void setUpperBound(index size)

Resize the vector so that indexes up to size-1 can be used.

index upperBound() const

Returns:

the upper bound of the indexes that can be used

count size() const
Returns:

the number of inserted elements.

void insert(index i, T value)

Insert a value at a given position.

Parameters:
  • i – index where the value is inserted

  • value

T &operator[](index i)

Access operator. Before accessing an element, insert it by using the insert() method.

const T &operator[](index i) const

Const access operator. Before accessing an element, insert it by using the insert() method.

bool indexIsUsed(index idx)

Returns true iff an element was previously inserted at the given index.

Parameters:

idx

void insertOrSet(index i, T value)

Inserts value at position i, or replaces the value if previously inserted

Parameters:
  • i

  • value

inline void removeUnusedIndexes()

Remove all indexes for which the value is set to the emptyValue.

void reset()

Reset all values to the default value, so it is “empty”. The upper bound is not changed.

void clear()

Clear the vector, setting the upper bound of usable indexes to 0.

void resize(size_t size, T emptyValue)

Reallocate the datastructure if size exceeds current upper bound This is different from setUpperBound() since we want to make sure both usedIndexes and data are allocated on the socket of the calling thread

Parameters:
  • size

  • emptyValue – new emptyValue

template<typename ElementHandler>
inline void forElements(ElementHandler &&lambda)

Applies the given lambda to each inserted index and associated value

Template Parameters:

ElementHandler

Parameters:

lambda