11 #ifndef EMP_VECTOR_UTILS_H 12 #define EMP_VECTOR_UTILS_H 14 #include "../base/vector.h" 21 for (
size_t i = start_pos; i < vec.
size(); i++) {
22 if (vec[i] == val)
return (
int) i;
36 for (
size_t id = 0;
id < v.
size();
id++) {
47 const std::function<
bool(
typename T::value_type,
typename T::value_type)> & fun) {
49 using v_type =
typename T::value_type;
50 v_type best_val = v[0];
51 size_t best_index = 0;
52 for (
size_t i = 1; i < v.size(); i++) {
53 if (fun(v[i], best_val)) {
64 using v_type =
typename T::value_type;
65 return FindIndex(v, [](v_type a, v_type b){
return a < b; });
71 using v_type =
typename T::value_type;
72 return FindIndex(v, [](v_type a, v_type b){
return a > b; });
80 for (
auto x : v) sum += x;
88 for (
auto x : v) product *= x;
93 template <
typename T,
typename... Ts>
100 template <
typename T>
107 for (
int i = start; i < stop; i++){
114 constexpr
size_t tree_left(
size_t id) {
return id*2+1; }
121 template <
typename T>
124 if (id_left >= v.
size())
return false;
127 const T val_left = v[id_left];
130 if (id_right < v.
size()) {
131 const T val_right = v[id_right];
132 if (val_right > val_left && val_right > val) {
140 if (val_left > val) {
151 template <
typename T>
153 size_t id = v.
size();
158 template <
typename T>
163 const size_t last_pos = v.
size() - 1;
173 template <
typename T>
175 size_t pos = v.
size();
void Sort(emp::vector< T > &v, Ts...args)
A quick shortcut for sorting a vector.
Definition: vector_utils.h:94
iterator end() noexcept
Definition: vector.h:155
void HeapInsert(emp::vector< T > &v, T val)
Insert a new element into a heap.
Definition: vector_utils.h:174
bool Heapify(emp::vector< T > &v, size_t id)
Heapify an individual node in a vector.
Definition: vector_utils.h:122
void push_back(PB_Ts &&...args)
Definition: vector.h:189
size_t FindMaxIndex(const T &v)
Find the index with the maximal value (picks first in cases of a tie).
Definition: vector_utils.h:70
size_t size() const
Definition: vector.h:151
T Product(const emp::vector< T > &v)
Multiply all of the contents of a vector.
Definition: vector_utils.h:86
constexpr size_t tree_left(size_t id)
Tree manipulation in vectors.
Definition: vector_utils.h:114
emp::vector< T > Slice(emp::vector< T > vec, int start, int stop)
Definition: vector_utils.h:101
T HeapExtract(emp::vector< T > &v)
Extraxt maximum element from a heap.
Definition: vector_utils.h:159
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.
Definition: map_utils.h:21
void resize(size_t new_size)
Definition: vector.h:161
iterator begin() noexcept
Definition: vector.h:153
constexpr size_t tree_right(size_t id)
Definition: vector_utils.h:115
int FindValue(const emp::vector< T > vec, const T &val, size_t start_pos=0)
Return the first position of a value in a vector (or -1 if none exists)
Definition: vector_utils.h:20
If we are in emscripten, make sure to include the header.
Definition: array.h:37
size_t FindIndex(const T &v, const std::function< bool(typename T::value_type, typename T::value_type)> &fun)
Definition: vector_utils.h:46
#define emp_assert(...)
Definition: assert.h:199
typename internal::ip_sort< T >::result sort
Definition: IntPack.h:193
size_t FindMinIndex(const T &v)
Find the index with the minimal value (picks first in cases of a tie).
Definition: vector_utils.h:63
constexpr size_t tree_parent(size_t id)
Definition: vector_utils.h:116
void Print(const emp::vector< T > &v, std::ostream &os=std::cout, const std::string &spacer=" ")
Print the contects of a vector.
Definition: vector_utils.h:35
std::enable_if<!emp::is_ptr_type< typename C::value_type >::value &&std::is_scalar< typename C::value_type >::value, typename C::value_type >::type Sum(C &elements)
Definition: stats.h:33