62 lines
1.5 KiB
C++
62 lines
1.5 KiB
C++
#pragma once
|
|
|
|
#include <functional> // less
|
|
#include <memory> // allocator
|
|
#include <utility> // pair
|
|
#include <vector> // vector
|
|
|
|
namespace nlohmann
|
|
{
|
|
|
|
/// ordered_map: a minimal map-like container that preserves insertion order
|
|
/// for use within nlohmann::basic_json<ordered_map>
|
|
template<class Key, class T, class IgnoredLess = std::less<Key>,
|
|
class Allocator = std::allocator<std::pair<const Key, T>>,
|
|
class Container = std::vector<std::pair<const Key, T>, Allocator>>
|
|
struct ordered_map : Container
|
|
{
|
|
using key_type = Key;
|
|
using mapped_type = T;
|
|
using value_type = std::pair<const Key, T>;
|
|
using Container::Container;
|
|
|
|
std::pair<typename Container::iterator, bool> emplace(key_type&& key, T&& t)
|
|
{
|
|
for (auto it = this->begin(); it != this->end(); ++it)
|
|
{
|
|
if (it->first == key)
|
|
{
|
|
return {it, false};
|
|
}
|
|
}
|
|
|
|
this->emplace_back(key, t);
|
|
return {--this->end(), true};
|
|
}
|
|
|
|
std::size_t erase(const key_type& key)
|
|
{
|
|
std::size_t result = 0;
|
|
for (auto it = this->begin(); it != this->end();)
|
|
{
|
|
if (it->first == key)
|
|
{
|
|
++result;
|
|
it = Container::erase(it);
|
|
}
|
|
else
|
|
{
|
|
++it;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
T& operator[](key_type&& key)
|
|
{
|
|
return emplace(std::move(key), T{}).first->second;
|
|
}
|
|
};
|
|
|
|
} // namespace nlohmann
|