HighMap library (C++)
Loading...
Searching...
No Matches
vector_utils.hpp
Go to the documentation of this file.
1
7#pragma once
8#include <algorithm>
9#include <random>
10#include <string>
11#include <vector>
12
13namespace hmap
14{
15
16// ============================================================
17// Indexing / Sorting
18// ============================================================
19
28std::vector<size_t> argsort(const std::vector<float> &v);
29
39template <typename T>
40void reindex_vector(std::vector<T> &v, std::vector<size_t> &idx)
41{
42 std::vector<T> v_new(v.size());
43 for (std::uint32_t k = 0; k < v.size(); k++)
44 v_new[k] = v[idx[k]];
45 v = v_new;
46}
47
57size_t upperbound_right(const std::vector<float> &v, float value);
58
59// ============================================================
60// Random / Shuffling
61// ============================================================
62
77std::vector<float> generate_random_vector(size_t size,
78 float min_value,
79 float max_value,
80 uint32_t seed);
81
95std::vector<int> generate_random_vector(size_t size,
96 int min_value,
97 int max_value,
98 uint32_t seed);
99
115std::vector<int> generate_unique_random_vector(size_t size,
116 int min_value,
117 int max_value,
118 uint32_t seed);
119
127template <typename T>
128void shuffle_vector(std::vector<T> &values, std::uint32_t seed)
129{
130 std::mt19937 rng(seed);
131 std::shuffle(values.begin(), values.end(), rng);
132}
133
142template <typename T>
143std::vector<T> shuffled_vector(const std::vector<T> &values, std::uint32_t seed)
144{
145 std::vector<T> result = values;
146 shuffle_vector(result, seed);
147 return result;
148}
149
150// ============================================================
151// Value Processing
152// ============================================================
153
163std::vector<size_t> find_sign_changes(const std::vector<float> &data);
164
170float compute_median(std::vector<float> values);
171
183std::vector<float> moving_average(const std::vector<float> &input, int radius);
184
193void vector_unique_values(std::vector<float> &v);
194
205std::vector<float> remap(const std::vector<float> &data,
206 float new_min = 0.f,
207 float new_max = 1.f);
208
209// ============================================================
210// Analysis / Visualization
211// ============================================================
212
221std::string make_histogram(const std::vector<float> &values,
222 int bin_count,
223 int hist_height);
224
225} // namespace hmap
Definition algebra.hpp:23
std::vector< size_t > find_sign_changes(const std::vector< float > &data)
Returns indices where a sign change occurs in the input vector.
Definition vector_utils.cpp:48
std::vector< T > shuffled_vector(const std::vector< T > &values, std::uint32_t seed)
Returns a shuffled copy of a vector.
Definition vector_utils.hpp:143
size_t upperbound_right(const std::vector< float > &v, float value)
Returns the index of the first element greater than a given value.
Definition vector_utils.cpp:236
std::vector< float > moving_average(const std::vector< float > &input, int radius)
Smooths a vector using a centered moving average.
Definition vector_utils.cpp:210
std::string make_histogram(const std::vector< float > &values, int bin_count, int hist_height)
Generates an ASCII histogram representation of values.
Definition vector_utils.cpp:144
std::vector< float > generate_random_vector(size_t size, float min_value, float max_value, uint32_t seed)
Generate a vector of random floating-point values within a given range.
Definition vector_utils.cpp:77
std::vector< size_t > argsort(const std::vector< float > &v)
Returns the indices that would sort the vector.
Definition vector_utils.cpp:17
std::vector< int > generate_unique_random_vector(size_t size, int min_value, int max_value, uint32_t seed)
Generate a vector of unique random integers within a given range.
Definition vector_utils.cpp:113
float compute_median(std::vector< float > values)
Computes the median value of a set of floats.
Definition vector_utils.cpp:28
std::vector< float > remap(const std::vector< float > &data, float new_min=0.f, float new_max=1.f)
Remaps values of a vector to a given range [new_min, new_max].
Definition vector_utils.cpp:251
void vector_unique_values(std::vector< float > &v)
Removes duplicate values from a vector.
Definition vector_utils.cpp:280
void reindex_vector(std::vector< T > &v, std::vector< size_t > &idx)
Reorders a vector using a given index mapping.
Definition vector_utils.hpp:40
void shuffle_vector(std::vector< T > &values, std::uint32_t seed)
Shuffles a vector in-place using a deterministic seed.
Definition vector_utils.hpp:128