Skip to content

Commit 0dfbf5d

Browse files
authored
Merge pull request #5 from dkourilov/dkourilov/json-merge-patch-fix
Persist fifo counter at copy
2 parents d9d08e0 + c0ea4f4 commit 0dfbf5d

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

src/fifo_map.hpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ class fifo_map_compare
4848
{
4949
public:
5050
/// constructor given a pointer to a key storage
51-
fifo_map_compare(std::unordered_map<Key, std::size_t>* k) : keys(k) {}
51+
fifo_map_compare(
52+
std::unordered_map<Key, std::size_t>* keys,
53+
std::size_t timestamp = 1)
54+
:
55+
m_timestamp(timestamp),
56+
m_keys(keys)
57+
{}
5258

5359
/*!
5460
This function compares two keys with respect to the order in which they
@@ -57,16 +63,16 @@ class fifo_map_compare
5763
bool operator()(const Key& lhs, const Key& rhs) const
5864
{
5965
// look up timestamps for both keys
60-
const auto timestamp_lhs = keys->find(lhs);
61-
const auto timestamp_rhs = keys->find(rhs);
66+
const auto timestamp_lhs = m_keys->find(lhs);
67+
const auto timestamp_rhs = m_keys->find(rhs);
6268

63-
if (timestamp_lhs == keys->end())
69+
if (timestamp_lhs == m_keys->end())
6470
{
6571
// timestamp for lhs not found - cannot be smaller than for rhs
6672
return false;
6773
}
6874

69-
if (timestamp_rhs == keys->end())
75+
if (timestamp_rhs == m_keys->end())
7076
{
7177
// timestamp for rhs not found - timestamp for lhs is smaller
7278
return true;
@@ -78,19 +84,30 @@ class fifo_map_compare
7884

7985
void add_key(const Key& key)
8086
{
81-
keys->insert({key, timestamp++});
87+
m_keys->insert({key, m_timestamp++});
8288
}
8389

8490
void remove_key(const Key& key)
8591
{
86-
keys->erase(key);
92+
m_keys->erase(key);
8793
}
8894

8995
private:
90-
/// pointer to a mapping from keys to insertion timestamps
91-
std::unordered_map<Key, std::size_t>* keys = nullptr;
96+
/// helper to access m_timestamp from fifo_map copy ctor,
97+
/// must have same number of template args as fifo_map
98+
template <
99+
class MapKey,
100+
class MapT,
101+
class MapCompare,
102+
class MapAllocator
103+
> friend class fifo_map;
104+
105+
private:
92106
/// the next valid insertion timestamp
93-
size_t timestamp = 1;
107+
std::size_t m_timestamp = 1;
108+
109+
/// pointer to a mapping from keys to insertion timestamps
110+
std::unordered_map<Key, std::size_t>* m_keys = nullptr;
94111
};
95112

96113

@@ -126,7 +143,7 @@ template <
126143
fifo_map() : m_keys(), m_compare(&m_keys), m_map(m_compare) {}
127144

128145
/// copy constructor
129-
fifo_map(const fifo_map &f) : m_keys(f.m_keys), m_compare(&m_keys), m_map(f.m_map.begin(), f.m_map.end(), m_compare) {}
146+
fifo_map(const fifo_map &f) : m_keys(f.m_keys), m_compare(&m_keys, f.m_compare.m_timestamp), m_map(f.m_map.begin(), f.m_map.end(), m_compare) {}
130147

131148
/// constructor for a range of elements
132149
template<class InputIterator>

0 commit comments

Comments
 (0)