vector_utils.hpp

A set of simple functions to manipulate vector.

Todo:

consider adding a work-around to avoid vector<bool> ?

speed up Append to count all additions at once, resize, and fill them in.

Note

Status: BETA

Functions

template<typename T>
T PopFront(vector<T> &v)

Remove and return the first element of a vector.

template<typename T>
void InsertAt(vector<T> &v, size_t id, T value)

Insert a value at a specified position in a vector.

template<typename T, typename V1, typename ...Vs>
vector<T> &Append(vector<T> &base, const V1 &v1, const Vs&... vs)

Append one or more vectors on to the end of an existing vector.

template<typename T, typename ...Vs>
vector<T> Concat(const vector<T> &v1, const Vs&... vs)

Concatenate two or more vectors together, creating a new vector.

template<typename T, typename INDEX_T = size_t>
vector<T> ToVector(const std::map<INDEX_T, T> &in_map, T default_val = T(), INDEX_T index_cap = 32768)

Convert a map to a vector.

template<typename T, typename INDEX_T = size_t>
vector<T> ToVector(const std::unordered_map<INDEX_T, T> &in_map, T default_val = T(), INDEX_T index_cap = 32768)

Convert an unordered map to a vector.

template<typename T>
std::map<size_t, T> ToMap(const vector<T> &in_vec)

Convert a vector into a map.

template<typename INDEX_T = size_t, typename T>
std::unordered_map<INDEX_T, T> ToUMap(const vector<T> &in_vec)

Convert a vector into a map.

template<typename T>
int FindValue(const vector<T> &v, const T &val, size_t start_pos = 0)

Return the first position of a value in a vector (or -1 if none exists)

template<typename T>
bool RemoveValue(vector<T> &v, const T &val, size_t start_pos = 0)

Remove the first value after start_pos with a given value. Return if removal successful.

template<typename T>
void RemoveAt(vector<T> &v, size_t id)

Remove value at an index.

template<typename T>
void RemoveAt(vector<T> &v, size_t id, size_t count)

Remove values starting at an index.

template<typename T>
vector<T> RemoveDuplicates(const vector<T> &v)

Return a new vector containing the same elements as

Parameters:

v – with any duplicate elements removed. Not guaranteed to preserve order

template<typename T>
bool Has(const vector<T> &v, const T &val)

Return whether a value exists in a vector.

template<typename T>
int Count(const vector<T> &vec, const T &val)

Return number of times a value occurs in a vector.

template<typename T>
void Print(const vector<T> &v, std::ostream &os = std::cout, const std::string &spacer = " ")

Print the contents of a vector.

template<typename T, typename FUN>
int FindEval(const vector<T> &v, const FUN &fun, size_t start_pos = 0)

Find the first index where the provided function returns true; return -1 otherwise.

template<typename T>
size_t FindIndex(const T &v, const std::function<bool(typename T::value_type, typename T::value_type)> &fun)

Find the index with the “optimal” value (picks first in cases of a tie).

Parameters:
  • v – Any object allowing indexing (e.g. vector, array, etc.)

  • fun – Comparison function; returns true if the first value os more optimal than second.

template<typename T>
size_t FindMinIndex(const T &v)

Find the index with the minimal value (picks first in cases of a tie).

template<typename T>
size_t FindMaxIndex(const T &v)

Find the index with the maximal value (picks first in cases of a tie).

template<typename T>
T FindMin(const vector<T> &v)

Find the minimum value in a vector.

template<typename T>
T FindMax(const vector<T> &v)

Find the maximum value in a vector.

template<typename T, typename C2>
vector<T> FindIntersect(const vector<T> &in1, const C2 &in2)

Find the intersection between this vector and another container.

template<typename T>
T Sum(const vector<T> &v)

Sum all of the contents of a vector.

template<typename T>
T Product(const vector<T> &v)

Multiply all of the contents of a vector.

template<typename T, typename ...Ts>
void Sort(vector<T> &v, Ts... args)

A quick shortcut for sorting a vector.

template<typename T>
void Scale(vector<T> &v, T scale)

Scale all elements of a vector by the same value.

template<typename T>
vector<T> Slice(vector<T> vec, int start, int stop)

Returns a vector containing a chunk of elements from

Parameters:
  • vec – starting at

  • start – and going up to but not including

  • stop

template<typename T>
vector<T> Flatten(const vector<vector<T>> &vv)

Collapse a vector of vectors into a single vector.

template<typename T>
vector<vector<T>> Transpose(const vector<vector<T>> &in_vv)

Swap the order of a vector of vectors. That is, swap rows and columns. NOTE: All rows must be the same size or smaller than those above for this to work.

template<typename T>
vector<T> NRange(T N1, T N2)

Returns a vector containing the numbers from.

Parameters:
  • N1 – to

  • N2

template<typename T>
static inline vector<T> BuildRange(T min, T max, T step = 1)

Build a vector with a range of values from min to max at the provided step size.

constexpr size_t tree_left(size_t id)

Tree manipulation in vectors.

constexpr size_t tree_right(size_t id)
constexpr size_t tree_parent(size_t id)
template<typename T>
bool Heapify(vector<T> &v, size_t id)

Heapify an individual node in a vector.

template<typename T>
void Heapify(vector<T> &v)

Heapify all elements in a vector.

template<typename T>
T HeapExtract(vector<T> &v)

Extract maximum element from a heap.

template<typename T>
void HeapInsert(vector<T> &v, T val)

Insert a new element into a heap.