aboutsummaryrefslogtreecommitdiff
path: root/gcc/ordered-hash-map.h
blob: aa9c86de39894e8fe3c6979621d25cd84f950617 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* A type-safe hash map that retains the insertion order of keys.
   Copyright (C) 2019-2024 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */


#ifndef GCC_ORDERED_HASH_MAP_H
#define GCC_ORDERED_HASH_MAP_H

/* Notes:
   - The keys must be PODs, since vec<> uses assignment to populate slots
     without properly initializing them.
   - doesn't have GTY support.
   - supports removal, but retains order of original insertion.
     (Removal might be better handled by using a doubly-linked list
     of nodes, holding the values).  */

template<typename KeyId, typename Value,
	 typename Traits>
class ordered_hash_map
{
  typedef typename Traits::key_type Key;

public:
  ordered_hash_map () {}

  ordered_hash_map (const ordered_hash_map &other)
  : m_map (other.m_map),
    m_keys (other.m_keys.length ()),
    m_key_index (other.m_key_index)
  {
     unsigned i;
     Key key;
     FOR_EACH_VEC_ELT (other.m_keys, i, key)
       m_keys.quick_push (key);
  }

  /* If key K isn't already in the map add key K with value V to the map, and
     return false.  Otherwise set the value of the entry for key K to be V and
     return true.  */

  bool put (const Key &k, const Value &v)
  {
    bool existed = m_map.put (k, v);
    if (!existed)
      {
        bool key_present;
        int &slot = m_key_index.get_or_insert (k, &key_present);
        if (!key_present)
          {
             slot = m_keys.length ();
             m_keys.safe_push (k);
          }
      }
    return existed;
  }

  /* If the passed in key is in the map return its value otherwise NULL.  */

  Value *get (const Key &k)
  {
    return m_map.get (k);
  }

  /* Return a reference to the value for the passed in key, creating the entry
    if it doesn't already exist.  If existed is not NULL then it is set to
    false if the key was not previously in the map, and true otherwise.  */

  Value &get_or_insert (const Key &k, bool *existed = NULL)
  {
    bool _existed;
    Value &ret = m_map.get_or_insert (k, &_existed);

    if (!_existed)
      {
	bool key_present;
	int &slot = m_key_index.get_or_insert (k, &key_present);
	if (!key_present)
	  {
	    slot = m_keys.length ();
	    m_keys.safe_push (k);
	  }
      }

    if (existed)
      *existed = _existed;

    return ret;
  }

  /* Removing a key removes it from the map, but retains the insertion
     order.  */

  void remove (const Key &k)
  {
     m_map.remove (k);
  }

  size_t elements () const { return m_map.elements (); }

  class iterator
  {
  public:
    explicit iterator (const ordered_hash_map &map, unsigned idx) :
      m_ordered_hash_map (map), m_idx (idx) {}

    iterator &operator++ ()
    {
       /* Increment m_idx until we find a non-deleted element, or go beyond
	  the end.  */
       while (1)
	 {
	   ++m_idx;
	   if (valid_index_p ())
	     break;
	}
      return *this;
    }

    /* Can't use std::pair here, because GCC before 4.3 don't handle
       std::pair where template parameters are references well.
       See PR86739.  */
    struct reference_pair {
      const Key &first;
      Value &second;

      reference_pair (const Key &key, Value &value)
      : first (key), second (value) {}

      template <typename K, typename V>
      operator std::pair<K, V> () const { return std::pair<K, V> (first, second); }
    };

    reference_pair operator* ()
    {
      const Key &k = m_ordered_hash_map.m_keys[m_idx];
      Value *slot
        = const_cast<ordered_hash_map &> (m_ordered_hash_map).get (k);
      gcc_assert (slot);
      return reference_pair (k, *slot);
    }

    bool
    operator != (const iterator &other) const
    {
      return m_idx != other.m_idx;
    }

    /* Treat one-beyond-the-end as valid, for handling the "end" case.  */

    bool valid_index_p () const
    {
      if (m_idx > m_ordered_hash_map.m_keys.length ())
	return false;
      if (m_idx == m_ordered_hash_map.m_keys.length ())
	return true;
      const Key &k = m_ordered_hash_map.m_keys[m_idx];
      Value *slot
	= const_cast<ordered_hash_map &> (m_ordered_hash_map).get (k);
      return slot != NULL;
    }

    const ordered_hash_map &m_ordered_hash_map;
    unsigned m_idx;
  };

  /* Standard iterator retrieval methods.  */

  iterator begin () const
  {
    iterator i = iterator (*this, 0);
    while (!i.valid_index_p () && i != end ())
      ++i;
    return i;
  }
  iterator end () const { return iterator (*this, m_keys.length ()); }

private:
  /* The assignment operator is not yet implemented; prevent erroneous
     usage of unsafe compiler-generated one.  */
  void operator= (const ordered_hash_map &);

  /* The underlying map.  */
  hash_map<KeyId, Value, Traits> m_map;

  /* The ordering of the keys.  */
  auto_vec<Key> m_keys;

  /* For each key that's ever been in the map, its index within m_keys.  */
  hash_map<KeyId, int> m_key_index;
};

/* Two-argument form.  */

template<typename Key, typename Value,
	 typename Traits = simple_hashmap_traits<default_hash_traits<Key>,
						 Value> >
class ordered_hash_map;

#endif /* GCC_ORDERED_HASH_MAP_H */