Skip to content

Commit

Permalink
⚗️ remove const from value type
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Jun 20, 2020
1 parent 4fd0d02 commit 6ee9e5f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 45 deletions.
38 changes: 16 additions & 22 deletions include/nlohmann/ordered_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ 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>>
template <class Key, class T, class IgnoredLess = std::less<Key>,
class Allocator = std::allocator<std::pair<Key, T>>,
class Container = std::vector<std::pair<Key, T>, Allocator>>
struct ordered_map : Container
{
using key_type = Key;
using mapped_type = T;
using value_type = std::pair<const Key, T>;
using value_type = typename Container::value_type;
using size_type = typename Container::size_type;
using Container::Container;

std::pair<typename Container::iterator, bool> emplace(key_type&& key, T&& t)
Expand All @@ -29,33 +30,26 @@ struct ordered_map : Container
return {it, false};
}
}

this->emplace_back(key, t);
Container::emplace_back(key, t);
return {--this->end(), true};
}

std::size_t erase(const key_type& key)
T& operator[](Key&& key)
{
std::size_t result = 0;
for (auto it = this->begin(); it != this->end();)
return emplace(std::move(key), T{}).first->second;
}

size_type erase(const Key& key)
{
for (auto it = this->begin(); it != this->end(); ++it)
{
if (it->first == key)
{
++result;
it = Container::erase(it);
}
else
{
++it;
Container::erase(it);
return 1;
}
}

return result;
}

T& operator[](key_type&& key)
{
return emplace(std::move(key), T{}).first->second;
return 0;
}
};

Expand Down
44 changes: 21 additions & 23 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2777,6 +2777,10 @@ template<class Key, class T, class IgnoredLess, class Allocator, class Container
struct ordered_map;

/*!
@brief ordered JSON class

This type preserves the insertion order of object keys.

@since version 3.9.0
*/
using ordered_json = basic_json<nlohmann::ordered_map>;
Expand Down Expand Up @@ -15875,17 +15879,18 @@ 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>>
template <class Key, class T, class IgnoredLess = std::less<Key>,
class Allocator = std::allocator<std::pair<Key, T>>,
class Container = std::vector<std::pair<Key, T>, Allocator>>
struct ordered_map : Container
{
using key_type = Key;
using mapped_type = T;
using value_type = std::pair<const Key, T>;
using value_type = typename Container::value_type;
using size_type = typename Container::size_type;
using Container::Container;

std::pair<typename Container::iterator, bool> emplace(Key&& key, T&& t)
std::pair<typename Container::iterator, bool> emplace(key_type&& key, T&& t)
{
for (auto it = this->begin(); it != this->end(); ++it)
{
Expand All @@ -15894,33 +15899,26 @@ struct ordered_map : Container
return {it, false};
}
}

this->emplace_back(key, t);
Container::emplace_back(key, t);
return {--this->end(), true};
}

std::size_t erase(const Key& key)
T& operator[](Key&& key)
{
std::size_t result = 0;
for (auto it = this->begin(); it != this->end(); )
return emplace(std::move(key), T{}).first->second;
}

size_type erase(const Key& key)
{
for (auto it = this->begin(); it != this->end(); ++it)
{
if (it->first == key)
{
++result;
it = Container::erase(it);
}
else
{
++it;
Container::erase(it);
return 1;
}
}

return result;
}

T& operator[]( Key&& key )
{
return emplace(std::move(key), T{}).first->second;
return 0;
}
};

Expand Down

0 comments on commit 6ee9e5f

Please sign in to comment.