40template <
typename Key,
typename Value>
50 void insert(
const Key& key,
const Value& value)
52 if (map_.find(key) == map_.end())
54 map_[key] = vector_.size();
55 vector_.emplace_back(std::make_pair(key, value));
65 const Value&
at(
const Key& key)
const
67 auto index = map_.at(key);
68 return vector_.at(index).second;
77 Value&
at(
const Key& key)
79 auto index = map_.at(key);
80 return vector_.at(index).second;
88 const std::vector<Key>
keys()
const
90 std::vector<Key> result;
91 for (
const auto& pair : vector_)
93 result.push_back(pair.first);
105 std::vector<Value> result;
106 for (
const auto& pair : vector_)
108 result.push_back(pair.second);
124 std::unordered_map<Key, size_t> map_;
125 std::vector<std::pair<Key, Value>> vector_;
137template <
typename FPType>
138static Eigen::Matrix<FPType, Eigen::Dynamic, 1> diff(
139 const Eigen::Matrix<FPType, Eigen::Dynamic, 1>& x,
140 const std::function<
double(
const Eigen::Matrix<FPType, Eigen::Dynamic, 1>&)>& f,
144 Eigen::Matrix<FPType, Eigen::Dynamic, 1> grad(n);
145 for (
int i = 0; i < n; i++)
147 Eigen::Matrix<FPType, Eigen::Dynamic, 1> x_plus = x, x_minus = x;
150 grad(i) = (f(x_plus) - f(x_minus)) / (2 * h);
167template <
typename Numeric,
typename Generator = std::mt19937>
168static Numeric random(Numeric from, Numeric to)
170 thread_local static Generator gen(std::random_device{}());
171 using dist_type =
typename std::conditional<std::is_integral<Numeric>::value, std::uniform_int_distribution<Numeric>,
172 std::uniform_real_distribution<Numeric>>::type;
173 thread_local static dist_type dist;
174 return dist(gen,
typename dist_type::param_type{from, to});
182static inline void trimString(std::string& s)
184 s.erase(s.begin(), std::find_if_not(s.begin(), s.end(), [](
unsigned char ch) { return std::isspace(ch); }));
185 s.erase(std::find_if_not(s.rbegin(), s.rend(), [](
unsigned char ch) { return std::isspace(ch); }).base(), s.end());
196static std::vector<T> flatten(
const std::vector<std::vector<T>>& vector_of_vectors)
199 size_t total_size = std::accumulate(vector_of_vectors.begin(), vector_of_vectors.end(), 0,
200 [](
size_t size,
const std::vector<T>& vec) { return size + vec.size(); });
201 flat.reserve(total_size);
202 for (
const auto& vec : vector_of_vectors)
204 flat.insert(flat.end(), vec.begin(), vec.end());
237static inline int pow2(
const int& n) {
return static_cast<int>(std::ldexp(1.0f, n)); }
249std::ostream& operator<<(std::ostream& stream,
const std::vector<T>& v)
258 std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(stream,
", "));
261 stream << v.back() <<
"]";
273template <
typename T, std::
size_t N>
274std::ostream& operator<<(std::ostream& stream,
const std::array<T, N>& v)
283 std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(stream,
", "));
286 stream << v.back() <<
"]";
299std::ostream& operator<<(std::ostream& stream,
const std::deque<T>& v)
308 std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(stream,
", "));
311 stream << v.back() <<
"]";
323template <typename T, typename std::enable_if<std::is_enum<T>::value, T>::type* =
nullptr>
324std::ostream& operator<<(std::ostream& stream,
const T& e)
326 return stream << static_cast<typename std::underlying_type<T>::type>(e);
This calss define a map that keeps the insertion order.
Definition tools.hpp:42
const std::vector< Key > keys() const
Return a vector containing the keys.
Definition tools.hpp:88
const Value & at(const Key &key) const
Return the value associated with the key.
Definition tools.hpp:65
void clear()
Clear the map and the vector.
Definition tools.hpp:117
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:50
Value & at(const Key &key)
Return the value associated with the key.
Definition tools.hpp:77
const std::vector< Value > values() const
Return a vector containing the values.
Definition tools.hpp:103