32template <
typename Key,
typename Value>
42 void insert(
const Key& key,
const Value& value)
44 if (map_.find(key) == map_.end())
46 map_[key] = vector_.size();
47 vector_.emplace_back(std::make_pair(key, value));
57 const Value&
at(
const Key& key)
const
59 auto index = map_.at(key);
60 return vector_.at(index).second;
69 Value&
at(
const Key& key)
71 auto index = map_.at(key);
72 return vector_.at(index).second;
80 const std::vector<Key>
keys()
const
82 std::vector<Key> result;
83 for (
const auto& pair : vector_)
85 result.push_back(pair.first);
95 const std::vector<Value>
values()
const
97 std::vector<Value> result;
98 for (
const auto& pair : vector_)
100 result.push_back(pair.second);
116 std::unordered_map<Key, size_t> map_;
117 std::vector<std::pair<Key, Value>> vector_;
129template <
typename FPType>
130static Eigen::Matrix<FPType, Eigen::Dynamic, 1> diff(
131 const Eigen::Matrix<FPType, Eigen::Dynamic, 1>& x,
132 const std::function<
double(
const Eigen::Matrix<FPType, Eigen::Dynamic, 1>&)>& f,
136 Eigen::Matrix<FPType, Eigen::Dynamic, 1> grad(n);
137 for (
int i = 0; i < n; i++)
139 Eigen::Matrix<FPType, Eigen::Dynamic, 1> x_plus = x, x_minus = x;
142 grad(i) = (f(x_plus) - f(x_minus)) / (2 * h);
159template <
typename Numeric,
typename Generator = std::mt19937>
160static Numeric random(Numeric from, Numeric to)
162 thread_local static Generator gen(std::random_device{}());
163 using dist_type =
typename std::conditional<std::is_integral<Numeric>::value, std::uniform_int_distribution<Numeric>,
164 std::uniform_real_distribution<Numeric>>::type;
165 thread_local static dist_type dist;
166 return dist(gen,
typename dist_type::param_type{from, to});
174static inline void trimString(std::string& s)
176 s.erase(s.begin(), std::find_if_not(s.begin(), s.end(), [](
unsigned char ch) { return std::isspace(ch); }));
177 s.erase(std::find_if_not(s.rbegin(), s.rend(), [](
unsigned char ch) { return std::isspace(ch); }).base(), s.end());
188static std::vector<T> flatten(
const std::vector<std::vector<T>>& vector_of_vectors)
191 size_t total_size = std::accumulate(vector_of_vectors.begin(), vector_of_vectors.end(), 0,
192 [](
size_t size,
const std::vector<T>& vec) { return size + vec.size(); });
193 flat.reserve(total_size);
194 for (
const auto& vec : vector_of_vectors)
196 flat.insert(flat.end(), vec.begin(), vec.end());
229static inline int pow2(
const int& n) {
return static_cast<int>(std::ldexp(1.0f, n)); }
241std::ostream& operator<<(std::ostream& stream,
const std::vector<T>& v)
250 std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(stream,
", "));
253 stream << v.back() <<
"]";
265template <
typename T, std::
size_t N>
266std::ostream& operator<<(std::ostream& stream,
const std::array<T, N>& v)
275 std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(stream,
", "));
278 stream << v.back() <<
"]";
291std::ostream& operator<<(std::ostream& stream,
const std::deque<T>& v)
300 std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(stream,
", "));
303 stream << v.back() <<
"]";
315template <typename T, typename std::enable_if<std::is_enum<T>::value, T>::type* =
nullptr>
316std::ostream& operator<<(std::ostream& stream,
const T& e)
318 return stream << static_cast<typename std::underlying_type<T>::type>(e);
This calss define a map that keeps the insertion order.
Definition tools.hpp:34
const std::vector< Key > keys() const
Return a vector containing the keys.
Definition tools.hpp:80
const Value & at(const Key &key) const
Return the value associated with the key.
Definition tools.hpp:57
void clear()
Clear the map and the vector.
Definition tools.hpp:109
void insert(const Key &key, const Value &value)
Insert a key-value pair into the map if the key does not exists.
Definition tools.hpp:42
Value & at(const Key &key)
Return the value associated with the key.
Definition tools.hpp:69
const std::vector< Value > values() const
Return a vector containing the values.
Definition tools.hpp:95