Data structures

Cache

similar to an std::unordered_map, but all lookups come with a function to generate the result should the lookup fail.

Note

This file is part of Empirical, https://github.com/devosoft/Empirical

Copyright

Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md

Date

2016-2018

Note

Status: BETA

namespace emp

If we are in emscripten, make sure to include the header.

template<class KEY, class T, class HASH = std::hash<KEY>, class PRED = std::equal_to<KEY>, class ALLOC = std::allocator<std::pair<const KEY, T>>>
class Cache

Public Types

using key_type = KEY

Type we are using to look up values.

using mapped_type = T

Contents of the value we look up.

using hasher = HASH

Hash method to use.

using key_equal = PRED

Function to test if two values are identical.

using allocator_type = ALLOC

Function to allocate new space.

Public Functions

Cache()
Cache(const Cache&) = default
Cache(Cache&&) = default
Cache &operator=(const Cache&) = default
Cache &operator=(Cache&&) = default
size_t size() const

How many entries are stored in the cache?

bool Has(const KEY &k) const

Determine if a specific key is already in the cache.

void Clear()

Erase contents of cache.

void Erase(const KEY &k)

Erase a specific entry from cache.

T Get(KEY k, const std::function<T(KEY k)> &calc_fun)

Lookup a specific key; provide a function to use if value is not in cache.

const T &GetRef(const KEY &k, const std::function<T(const KEY &k)> &calc_fun)

A version of Get that allows calls with const references instead of pass-by-value.

Private Members

std::unordered_map<KEY, T, HASH, PRED, ALLOC> cache_map

Dynamic Strings

A string handler where sections update dynamically based on functions.

Note

This file is part of Empirical, https://github.com/devosoft/Empirical

Copyright

Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md

Date

2016-2017

Note

Status: BETA

namespace emp

If we are in emscripten, make sure to include the header.

class DynamicString
#include <DynamicString.hpp>

A string handler where some sections can be fixed strings, while others update dynamically based on functions.

Public Types

using value_t = std::function<std::string()>

Public Functions

DynamicString()
DynamicString(const DynamicString&) = default
size_t GetSize() const

How many string components (funcations or continuous substrings) are in this DynamicString?

std::string operator[](size_t id) const

Index in to a specific component (not a specific character, since size is variable) and return it’s associated string.

const value_t &GetFunction(size_t id) const

Index in to a specific component (not a specific character, since size is variable) and return it’s associated function.

DynamicString &Clear()

Remove all contents on this DynamicString.

std::string str()

Convert to an std::string.

DynamicString &Set(size_t id, const value_t &in_fun)

Set the value of a specified component to the provided function.

DynamicString &Set(size_t id, const std::string &in_text)

Set the value of a specified component to the provided std::string text.

DynamicString &Append(const value_t &in_fun)

Add a new function to the end of the DynamicString.

DynamicString &Append(const std::string &in_text)

Add new std::string text to the end of the DynamicString.

template<typename IN_TYPE>
DynamicString &operator<<(IN_TYPE &&_in)

Allow operator<< to append to the back of a DynamicString.

Private Members

emp::vector<value_t> fun_set
namespace std

For hashing BitArrays.

Functions

std::ostream &operator<<(std::ostream &os, const emp::DynamicString &strings)

Make sure that DynamicString works with with std::ostream.

Graph Utilities

This file provides a number of tools for manipulating graphs.

Note

This file is part of Empirical, https://github.com/devosoft/Empirical

Copyright

Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md

Date

2017

Note

Status: BETA

namespace emp

If we are in emscripten, make sure to include the header.

Functions

Graph shuffle_graph(const Graph &in_graph, Random &random)

Take an existing graph, and build a new one that is isomorphic to it, but with randomized vertex IDs.

Graph build_graph_ring(size_t v_count, Random &random)

Construct a graph where all vertics are degree two and form a single ring.

Graph build_graph_tree(size_t v_count, Random &random)

Construct a random tree graph (new vertices are repeatedly attached to a random position in a tree as it is constructed.)

Graph build_graph_random(size_t v_count, size_t e_count, Random &random, bool connected = true)

Construct a random, graph with the specified number of vertices and edges. If connected is set, start by building a tree. Then connect random (unconnected) pairs of vertices until the proper number of edges are included.

Graph build_graph_grid(size_t width, size_t height, Random &random, double prob_use = 1.0)

Construct a graph with width x height vertices setup into a grid structure.

Graph build_graph_clique_set(size_t clique_size, size_t clique_count, Random &random, double extra_prob = 0.5)

Build a set of cliques (such that one member of each can be part of an independent set) and then links them together

Graph build_graph_dag(size_t v_count, size_t e_count, Random &random, bool connected = true)

Construct a random, graph with the specified number of vertices and edges. If connected is set, start by building a tree. Then connect random (unconnected) pairs of vertices until the proper number of edges are included.

WeightedGraph build_weighted_graph_tree(size_t v_count, size_t min_weight, size_t max_weight, Random &random)

Construct a random WEIGHTED tree graph (new vertices are repeatedly attached to a random position in a tree as it is constructed.)

WeightedGraph build_weighted_graph_random(size_t v_count, size_t e_count, size_t min_weight, size_t max_weight, Random &random, bool connected = true)

Construct a random, WEIGHTED graph with the specified number of vertices, edges, and range of edge weights. If connected is set, start by building a tree. Then connect random (unconnected) pairs of vertices until the proper number of edges are included.

Graph load_graph_sym(std::istream &is, bool sub1 = false)

Helper function for loading symmetric graphs from an input stream. sub1 indicates that verticies are numbered 1 to N instead of 0 to N-1.

Graph load_graph_sym(std::string filename, bool sub1 = false)

Load a graph with a specified filename.

Graph load_graph_table(std::istream &is)

Load a graph from a connection matrix. Format: Number of vertices followed by v^2 0’s or 1’s Example: “3 0 1 0 1 0 0 0 0 1”

Graph load_graph_table(std::string filename)

Load a graph from a connection matrix in a file by the specified name.

Graphs

A simple, fast class for managing verticies (nodes) and edges.

Note

This file is part of Empirical, https://github.com/devosoft/Empirical

Copyright

Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md

Date

2017

Note

Status: BETA

namespace emp

If we are in emscripten, make sure to include the header.

class Graph
#include <Graph.hpp>

A graph class that maintains a set of vertices (nodes) and edges (connecting pairs of nodes)

Subclassed by emp::WeightedGraph

Public Functions

Graph(size_t num_nodes = 0)

Construct a new graph with the specified number of nodes.

Graph(const Graph&) = default

Copy constructor.

Graph(Graph&&) = default

Move constructor.

~Graph()
Graph &operator=(const Graph&) = default

Copy operator.

Graph &operator=(Graph&&) = default

Move operator.

size_t GetSize() const

Get number of vertices in this graph.

size_t GetEdgeCount() const

Get the total number of edges in this graph.

Node GetNode(int i)

Return

the i th node in the graph

emp::vector<Node> GetNodes()

Return

a vector of all nodes in the graph

void Resize(size_t new_size)

Change the number of vertices in this graph.

const BitVector &GetEdgeSet(size_t id) const

Get the set of nodes that a specified node is connected to.

size_t GetDegree(size_t id) const

Get the degree of a specified node. For directed graphs, this is the out-degree

size_t GetInDegree(size_t id) const

Get the in-degree (number of incoming edges) of the node

Parameters
  • id: This should only be used for directed graphs (for undirected graphs, GetDegree() is equivalent and faster)

size_t GetMaskedDegree(size_t id, const BitVector &mask) const

Get how many of a set of nodes that a specified node is connected to.

void SetLabel(size_t id, std::string lab)

Set label of node.

Parameters
  • id: to

  • lab:

std::string GetLabel(size_t id)

Get label of node.

Parameters
  • id:

bool HasEdge(size_t from, size_t to) const

Determine if a specific edge is included in this graph.

void AddEdge(size_t from, size_t to)

Add a specified edge into this graph.

void RemoveEdge(size_t from, size_t to)

Remove a specified edge from this graph.

void SetEdge(size_t from, size_t to, bool val)

Set the status of a specified edge as to whether or not it should be in the graph.

bool HasEdgePair(size_t from, size_t to) const

Determine if edges exist in both directions between a pair of vertices.

void AddEdgePair(size_t from, size_t to)

Add a pair of edges between two vertieces (in both directions)

void RemoveEdgePair(size_t from, size_t to)

Remove edges in both directions between a pair of vertices.

void SetEdgePairs(size_t from, size_t to, bool val)

Set the status as to whether a pair of edges (in both direction) exist.

void Merge(const Graph &in_graph)

Merge a second graph into this one.

void PrintSym(std::ostream &os = std::cout)

Print a symmetric graph to the provided output stream (defaulting to standard out)

void PrintDirected(std::ostream &os = std::cout)

Print a directed graph to the provided output stream (defaulting to standard out)

Protected Attributes

emp::vector<Node> nodes

Set of vertices in this graph.

class Node
#include <Graph.hpp>

Information about nodes within a graph.

Public Functions

Node(size_t num_nodes)
Node(const Node &in_node)
~Node()
Node &operator=(const Node &in_node)

Set this node to have the same connections as another node.

bool HasEdge(size_t to) const

Is this node connect to a specific other node?

void AddEdge(size_t to)

Add a connection between this node and another.

void AddEdgeSet(BitVector in_set)

Add a full set of connections from this node to others.

void RemoveEdge(size_t to)

Remove the connection (if there is one) between this node and another one.

void SetEdge(size_t to, bool val)

Set whether a connection to another specific node should exist or not.

const BitVector &GetEdgeSet() const

Get a BitVector representing which nodes this one is connected to.

void Resize(size_t new_size)

Change the number of potential node connections that we are tracking.

void Clear()

Remove all edges from this node.

size_t GetDegree() const

Identify how many other nodes this one is connected to.

size_t GetMaskedDegree(const BitVector &mask) const

Identify how many other nodes from a provided set (a BitVector) this one is connected to.

void SetLabel(std::string lab)
std::string GetLabel()

Private Members

BitVector edge_set
std::string label

What other node IDs is this one connected to?

class WeightedGraph : public emp::Graph
#include <Graph.hpp>

A graph class that maintains a set of vertices (nodes), edges (connecting pairs of nodes), and edge weights

Public Functions

WeightedGraph(size_t num_nodes = 0)

The weight of each edge in the graph.

WeightedGraph(const WeightedGraph&) = default

Copy constructor.

WeightedGraph(WeightedGraph&&) = default

Move constructor.

~WeightedGraph()
WeightedGraph &operator=(const WeightedGraph&) = default

Copy operator.

WeightedGraph &operator=(WeightedGraph&&) = default

Move operator.

void Resize(size_t new_size)
double GetWeight(size_t from, size_t to) const

Determine weight of a specific edge in this graph.

void AddEdge(size_t from, size_t to, double weight)

When Adding an edge, must also provide a weight.

void AddEdgePair(size_t from, size_t to, double weight)

When Adding an edge pair, must also provide a weight.

void Merge(const WeightedGraph &in_graph)

Merge two WeightedGraphs into one.

void PrintSym(std::ostream &os = std::cout)

Print a symmetric graph to the provided output stream (defaulting to standard out)

void PrintDirected(std::ostream &os = std::cout)

Print a directed graph to the provided output stream (defaulting to standard out)

emp::vector<emp::vector<double>> GetWeights()

Return

the weights for all edges in the graph

Protected Attributes

emp::vector<emp::vector<double>> weights

Index Map

A simple class to weight items differently within a container and return the correct index.

Note

This file is part of Empirical, https://github.com/devosoft/Empirical

Copyright

Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md

Date

2015-2018

Note

Status: BETA

namespace emp

If we are in emscripten, make sure to include the header.

class IndexMap
#include <IndexMap.hpp>

A map of weighted indices. If a random index is selected, the probability of an index being returned is directly proportional to its weight.

Public Functions

IndexMap(size_t _items = 0)

Construct an IndexMap where num_items is the maximum number of items that can be placed into the data structure. All item weights default to zero.

IndexMap(size_t _items, double init_weight)
IndexMap(const IndexMap&) = default
IndexMap(IndexMap&&) = default
~IndexMap() = default
IndexMap &operator=(const IndexMap&) = default
IndexMap &operator=(IndexMap&&) = default
size_t GetSize() const

How many indices are in this map?

double GetWeight() const

What is the total weight of all indices in this map?

double RawWeight(size_t id) const

What is the current weight of the specified index?

double GetWeight(size_t id) const
double RawProb(size_t id) const

What is the probability of the specified index being selected?

double GetProb(size_t id) const
void Resize(size_t new_size, double def_value = 0.0)

Change the number of indices in the map.

void PushBack(double new_value)

Added a new value to the end of the index map.

size_t size() const

Standard library compatibility.

void resize(size_t new_size)

Standard library compatibility.

void push_back(double new_value)

Standard library compatibility.

void Clear()

Reset all item weights to zero.

void ResizeClear(size_t new_size)

Change the size of this map AND change all weights to zero.

void RawAdjust(size_t id, const double new_weight)

Adjust the weight associated with a particular index in the map.

Parameters
  • id: is the identification number of the item whose weight is being adjusted.

  • new_weight: is the new weight for that entry.

void Adjust(size_t id, const double new_weight)
void Adjust(const emp::vector<double> &new_weights)

Adjust all index weights to the set provided.

void AdjustAll(double new_weight)

Adjust all index weights to the set provided.

size_t Index(double index, size_t cur_id = 0) const

Determine the ID at the specified index position.

Proxy operator[](size_t id)

Index into a specified ID.

double operator[](size_t id) const
IndexMap &operator+=(IndexMap &in_map)

Add the weights in another index map to this one.

IndexMap &operator-=(IndexMap &in_map)

Substract the weights from another index map from this one.

void DeferRefresh()

Indicate that we need to adjust weights before relying on them in the future; this will prevent refreshes from occuring immediately and is useful when many updates to weights are likely to be done before any are accessed again.

Private Functions

size_t ParentID(size_t id) const

Which ID is the parent of the ID provided?

size_t LeftID(size_t id) const

Which ID is the left child of the ID provided?

size_t RightID(size_t id) const

Which ID is the right child of the ID provided?

size_t CalcZeroOffset() const

Sift through the nodes to find the where index zero maps to.

size_t ToInternalID(size_t id) const
size_t ToInternalID(size_t id, size_t _items, size_t _offset) const
size_t ToExternalID(size_t id) const
void ResolveRefresh() const

Check if we need to do a refresh, and if so do it!

Private Members

size_t num_items

How many items are being stored in this IndexMap?

size_t zero_offset

Position of id zero.

bool needs_refresh

Are tree weights out of date?

emp::vector<double> weights

The total weights in each sub-tree.

class Proxy

A Proxy class so that an index can be treated as an l-value.

Public Functions

Proxy(IndexMap &_im, size_t _id)
operator double() const
Proxy &operator=(double new_weight)

Private Members

IndexMap &index_map

Which index map is this proxy from?

size_t id

Which id does it represent?

Map Utilities

A set of simple functions to manipulate maps.

Note

This file is part of Empirical, https://github.com/devosoft/Empirical

Copyright

Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md

Date

2016-2017

Note

Status: BETA

namespace emp

If we are in emscripten, make sure to include the header.

Functions

template<class MAP_T, class KEY_T>
bool Has(const MAP_T &in_map, const KEY_T &key)

Take any map type, and run find to determine if a key is present.

template<class MAP_T>
auto Keys(const MAP_T &in_map) -> emp::vector<typename std::remove_const<decltype(in_map.begin()->first)>::type>
template<class MAP_T, class KEY_T>
auto Find(const MAP_T &in_map, const KEY_T &key, const typename MAP_T::mapped_type &dval)

Take any map, run find() member function, and return the result found (or default value if no results found).

template<class MAP_T, class KEY_T>
const auto &FindRef(const MAP_T &in_map, const KEY_T &key, const typename MAP_T::mapped_type &dval)

Take any map and element, run find() member function, and return a reference to the result found (or default value if no results found).

template<typename A, typename B>
constexpr std::pair<B, A> flip_pair(const std::pair<A, B> &p)

Take an std::pair<A,B> and return the flipped pair std::pair<B,A>

template<typename A, typename B>
std::multimap<B, A> flip_map(const std::map<A, B> &src)

Take an std::map<A,B> and return the flipped map (now multimap to be safe): std::multimap<B,A>

template<typename A, typename B>
emp::multimap<B, A> flip_map(const emp::map<A, B> &src)

Take an emp::map<A,B> and return the flipped map (now multimap to be safe): emp::multimap<B,A>

RandomAccess Set

This file defines a Random Access Set template.

Note

This file is part of Empirical, https://github.com/devosoft/Empirical

Copyright

Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md

Date

2017-2019

Note

Status: ALPHA

namespace emp

If we are in emscripten, make sure to include the header.

template<typename T>
class ra_set
#include <ra_set.hpp>

This class uses a combination of a hashtable (std::unordered_map) and emp::vector to lookup insert, lookup, and delete values in constant time, while still being able to step through all values (albeit in an arbitrary order).

Note

The arbitrary order of values may change if any values are deleted.

Public Types

using value_type = T

Public Functions

ra_set() = default
ra_set(const ra_set&) = default
ra_set(ra_set&&) = default
ra_set<T> &operator=(const ra_set&) = default
ra_set<T> &operator=(ra_set&&) = default
bool empty() const

Are there any values in this ra_set?

size_t size() const

How many elements are in this set?

const T &operator[](size_t pos) const

Index into the ra_set, similar to a vector.

void clear()

Remove all values from this container.

void insert(const T &v)

Insert a new value into container.

bool erase(const T &v)

Erase a specific value from the container.

size_t count(const T &v) const

Count the number of times a particular value in in the container (0 or 1).

Private Members

emp::map<T, size_t> id_map

A map of where to find values in vector.

emp::vector<T> vals

A vector of all values contained.

Set Utilities

Tools to save and load data from classes.

Note

This file is part of Empirical, https://github.com/devosoft/Empirical

Copyright

Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md

Date

2016-2018

Note

Status: ALPHA

namespace emp

If we are in emscripten, make sure to include the header.

Functions

template<typename T>
void insert(std::set<T> &s1, const std::set<T> &s2)

Insert the full contents of s2 into s1.

template<typename T, typename H, typename V>
bool Has(const std::set<T, H> &s, const V &val)

Test if an std::set has a particular element without modifying the set in any way.

template<typename T, typename H, typename V>
bool Has(const std::multiset<T, H> &s, const V &val)

Test if an std::multiset has a particular element without modifying the set in any way.

template<typename T, typename H, typename V>
bool Has(const std::unordered_set<T, H> &s, const V &val)

Test if an std::unordered_set has a particular element without modifying the set in any way.

template<typename T, typename H, typename V>
bool Has(const std::unordered_multiset<T, H> &s, const V &val)

Test if an std::unordere_multiset has a particular element without modifying the set in any way.

template<typename T>
std::set<T> difference(std::set<T> &s1, std::set<T> &s2)

Compute the set difference of.

Parameters
  • s1: and

  • s2: (elements that are in S1 but no S2)

template<typename T>
std::set<T> difference(emp::vector<T> s1, emp::vector<T> s2)

Compute the set difference of.

Parameters
  • s1: and

  • s2: (elements that are in S1 but no S2)

template<typename T>
std::set<T> difference(std::set<T> &s1, emp::vector<T> s2)

Compute the set difference of.

Parameters
  • s1: and

  • s2: (elements that are in S1 but not S2)

template<typename T>
std::set<T> difference(emp::vector<T> s1, std::set<T> &s2)

Compute the set difference of.

Parameters
  • s1: and

  • s2: (elements that are in S1 but no S2)

template<typename T>
std::set<T> intersection(std::set<T> &s1, std::set<T> &s2)

Compute the set intersection of.

Parameters
  • s1: and

  • s2: (elements that are in both S1 and S2)

template<typename T>
std::set<T> intersection(emp::vector<T> s1, emp::vector<T> s2)

Compute the set intersection of.

Parameters
  • s1: and

  • s2: (elements that are in both S1 and S2)

template<typename T>
std::set<T> intersection(std::set<T> &s1, emp::vector<T> s2)

Compute the set intersection of.

Parameters
  • s1: and

  • s2: (elements that are in both S1 and S2)

template<typename T>
std::set<T> intersection(emp::vector<T> s1, std::set<T> &s2)

Compute the set intersection of.

Parameters
  • s1: and

  • s2: (elements that are in both S1 and S2)

template<typename T>
std::set<T> set_union(std::set<T> &s1, std::set<T> &s2)

Compute the set union of.

Parameters
  • s1: and

  • s2: (elements that are in either S1 or S2)

template<typename T>
std::set<T> set_union(emp::vector<T> s1, emp::vector<T> s2)

Compute the set union of.

Parameters
  • s1: and

  • s2: (elements that are in either S1 or S2)

template<typename T>
std::set<T> set_union(std::set<T> &s1, emp::vector<T> s2)

Compute the set union of.

Parameters
  • s1: and

  • s2: (elements that are in either S1 or S2)

template<typename T>
std::set<T> set_union(emp::vector<T> s1, std::set<T> &s2)

Compute the set union of.

Parameters
  • s1: and

  • s2: (elements that are in either S1 or S2)

template<typename T>
std::set<T> symmetric_difference(std::set<T> &s1, std::set<T> &s2)

Compute the set symmetric_difference of.

Parameters
  • s1: and

  • s2: (elements that are in either S1 or S2 but not both)

template<typename T>
std::set<T> symmetric_difference(emp::vector<T> s1, emp::vector<T> s2)

Compute the set symmetric_difference of.

Parameters
  • s1: and

  • s2: (elements that are in either S1 or S2 but not both)

template<typename T>
std::set<T> symmetric_difference(std::set<T> &s1, emp::vector<T> s2)

Compute the set symmetric_difference of.

Parameters
  • s1: and

  • s2: (elements that are in either S1 or S2 but not both)

template<typename T>
std::set<T> symmetric_difference(emp::vector<T> s1, std::set<T> &s2)

Compute the set symmetric_difference of.

Parameters
  • s1: and

  • s2: (elements that are in either S1 or S2 but not both)

Tuple Struct

These macros will build a tuple and accessors to that tuple’s members inside of a class definintion.

Status: ALPHA

Note

This file is part of Empirical, https://github.com/devosoft/Empirical

Copyright

Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md

Date

2021

“But WHY???” you ask. Let me explain: Keeping a tuple allows us to easily track the members in the stuct or class, and makes possible powerful types of reflection including identifying all members and performing an action on each (such as serialization). Since tuples instantiate members directly, these benefits should come at no cost to performance.

To instantiate these tuple members, inside the class definition use:

`EMP_BUILD_TUPLE( int, MyInt, char, MyChar, int, MyInt2, std::string, MyString );`

This will create a std::tuple<int, char, int, std::string> and create accessors. For example the accessors created for MyInt2 would look like:

`const int & MyInt2() const; int & MyInt2(); int & MyInt2(int);`

The default tuple will be called “emp__tuple_body”. If you want to use a different name for it (for example if you want more than one tuple in the class), you need to call EMP_BUILD_NAMED_TUPLE, which is identical to EMP_BUILD_TUPLE, but takes the name to be used for the tuple as its first argument.

Example of using an introspective tuple:

`struct JSONObject { EMP_BUILD_INTROSPECTIVE_TUPLE( double, x, int, name, int, parent, double, y, int, depth ) };`

This struct can now be used as a C++ stand-in for Javscript objects for use with JSWrap.

Introspective tuple structs have a static member n_fields that indicates how many variables they have.

Development notes:

  • Add static member function to help with reflection?

  • Make play nicely with serialization techniques.

  • It would be really awesome to make tuple structs (or at least the introspective ones) be indexable and iterable. (right now the main obstacle is type inference)

Changes in last revision:

  • Changed name of macro that builds tuple that keeps track of variable names to EMP_BUILD_INTROSPECTIVE_TUPLE

  • Introspective tuples now also keep track of variable types and maintain an array of pointers to each element, which is very useful for doing operations on all members of a tuple.

Tuple Utilities

Functions to simplify the use of std::tuple.

Note

This file is part of Empirical, https://github.com/devosoft/Empirical

Copyright

Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md

Date

2016-2021.

Note

Status: RELEASE

namespace emp

If we are in emscripten, make sure to include the header.

Functions

template<typename TUPLE_T>
constexpr int tuple_size()

Quick way to calculate tuple size.

template<typename ...Ts, int... Ps>
auto shuffle_tuple(const std::tuple<Ts...> &tup, ValPack<Ps...>)

Reorganize the entries in tuple; the provided int pack must specify the new ordering.

template<typename TUPLE_T, typename FUN_T>
void TupleIterate(TUPLE_T &tup, const FUN_T &fun)

Call a provided function on each element of a tuple.

template<typename TUP1_T, typename TUP2_T, typename FUN_T>
void TupleIterate(TUP1_T &tup1, TUP2_T &tup2, const FUN_T &fun)

Call a provided function on each pair of elements in two tuples.

template<typename ...TYPES>
struct TupleHash
#include <tuple_utils.hpp>

Setup tuples to be able to be used in hash tables.

Public Types

using tuple_t = std::tuple<TYPES...>
using fun_t = std::function<std::size_t(TYPES...)>

Public Functions

std::size_t operator()(const tuple_t &tup) const

Vector Utilities

A set of simple functions to manipulate emp::vector.

Note

This file is part of Empirical, https://github.com/devosoft/Empirical

Copyright

Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md

Date

2017-2021.

Note

Status: BETA

namespace emp

If we are in emscripten, make sure to include the header.

Functions

template<typename T>
emp::vector<T> &Append(emp::vector<T> &base)

Base case for Append; we just have a single vector with nothing to append.

template<typename T, typename V1, typename ...Vs>
emp::vector<T> &Append(emp::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>
emp::vector<T> Concat(const emp::vector<T> &v1, const Vs&... vs)

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

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

Convert a map to a vector.

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

Convert an unordered map to a vector.

template<typename T>
std::map<size_t, T> ToMap(const emp::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 emp::vector<T> &in_vec)

Convert a vector into a map.

template<typename T>
int FindValue(const emp::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(emp::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>
bool Has(const emp::vector<T> &v, const T &val)

Return whether a value exists in a vector.

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

Return number of times a value occurs in a vector.

template<typename T>
void Print(const emp::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 emp::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 emp::vector<T> &v)

Find the minimum value in a vector.

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

Find the maximum value in a vector.

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

Find the intersection between this vector and another container.

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

Sum all of the contents of a vector.

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

Multiply all of the contents of a vector.

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

A quick shortcut for sorting a vector.

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

Scale all elements of a vector by the same value.

template<typename T>
emp::vector<T> Slice(emp::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>
emp::vector<T> Flatten(const emp::vector<emp::vector<T>> &vv)

Collapse a vector of vectors into a single vector.

template<typename T>
emp::vector<emp::vector<T>> Transpose(const emp::vector<emp::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>
emp::vector<T> NRange(T N1, T N2)

Returns a vector containing the numbers from.

Parameters
  • N1: to

  • N2:

template<typename T>
emp::vector<T> RemoveDuplicates(const emp::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>
emp::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(emp::vector<T> &v, size_t id)

Heapify an individual node in a vector.

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

Heapify all elements in a vector.

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

Extract maximum element from a heap.

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

Insert a new element into a heap.