summaryrefslogtreecommitdiff
path: root/src/jit/jitstd/unordered_map.h
blob: 05e97f450c6e2521512624b0f421bb9d51b5bb73 (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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// ==++==
//

//

//
// ==--==

/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX                                                                           XX
XX                          unordered_map<K,V,H,P,A>                         XX
XX  Derives from hashtable for most implementation. Inserted elements are    XX
XX  value pairs and the hash key is provided by the helper method that       XX
XX  extracts the key from the key value pair                                 XX
XX                                                                           XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/

#pragma once

#include "hashtable.h"

namespace jitstd
{

template <typename Key, typename Value>
struct pair_key
{
    Key& operator()(const jitstd::pair<Key, Value>& pair) const
    {
        return pair.first;
    }
};

template<typename Key,
         typename Value,
         typename Hash = jitstd::hash<Key>,
         typename Pred = jitstd::equal_to<Key>,
         typename Alloc = jitstd::allocator<jitstd::pair<const Key, Value> > >
class unordered_map
    : public hashtable<Key, pair<const Key, Value>, Hash, Pred, Alloc, pair_key<const Key, Value>>
{
public:

    typedef Key key_type;
    typedef Value mapped_type;
    typedef jitstd::pair<const Key, Value> value_type;
    typedef Hash hasher;
    typedef Pred key_equal;
    typedef Alloc allocator_type;
    typedef typename allocator_type::pointer pointer;             
    typedef typename allocator_type::const_pointer const_pointer;       
    typedef typename allocator_type::reference reference;
    typedef typename allocator_type::const_reference const_reference;
    typedef size_t size_type;
    typedef ptrdiff_t difference_type;

    explicit unordered_map(size_type size, const hasher& hasher, const key_equal& pred, const allocator_type& allocator);
    explicit unordered_map(size_type size, const allocator_type& allocator);
    template<typename InputIterator> 
    unordered_map(InputIterator, InputIterator, 
                  size_type size, 
                  const hasher& hasher,
                  const key_equal& pred, 
                  const allocator_type& allocator);

    unordered_map(const unordered_map& map);
    explicit unordered_map(const allocator_type& allocator);
    unordered_map(const unordered_map& map, const allocator_type& allocator);
    ~unordered_map();

    unordered_map& operator=(unordered_map const&);
    mapped_type& operator[](const Key& key);
    mapped_type& operator[](key_type&& key);

    typename unordered_map<Key, Value, Hash, Pred, Alloc>::iterator insert(const key_type& key, const mapped_type& value);

private:
    typedef hashtable<Key, pair<const Key, Value>, Hash, Pred, Alloc, pair_key<const Key, Value>> base_type;
};

}


namespace jitstd
{

template<typename Key, typename Value, typename Hash, typename Pred, typename Alloc>
unordered_map<Key, Value, Hash, Pred, Alloc>::unordered_map(size_type size, const hasher& hasher, const key_equal& pred, const allocator_type& allocator)
    : base_type(size, hasher, pred, allocator)
{
}

template<typename Key, typename Value, typename Hash, typename Pred, typename Alloc>
unordered_map<Key, Value, Hash, Pred, Alloc>::unordered_map(size_type size, const allocator_type& allocator)
    : base_type(size, allocator)
{
}

template<typename Key, typename Value, typename Hash, typename Pred, typename Alloc>
template<typename InputIterator> 
unordered_map<Key, Value, Hash, Pred, Alloc>::unordered_map(InputIterator first, InputIterator last, 
                size_type size, 
                const hasher& hasher,
                const key_equal& pred, 
                const allocator_type& allocator)
    : base_type(first, last, size, hasher, pred, allocator)
{
}

template<typename Key, typename Value, typename Hash, typename Pred, typename Alloc>
unordered_map<Key, Value, Hash, Pred, Alloc>::unordered_map(const unordered_map& map)
    : base_type(map)
{
}

template<typename Key, typename Value, typename Hash, typename Pred, typename Alloc>
unordered_map<Key, Value, Hash, Pred, Alloc>::unordered_map(const allocator_type& allocator)
    : base_type(allocator)
{
}

template<typename Key, typename Value, typename Hash, typename Pred, typename Alloc>
unordered_map<Key, Value, Hash, Pred, Alloc>::unordered_map(const unordered_map& map, const allocator_type& allocator)
    : base_type(map, allocator)
{
}

template<typename Key, typename Value, typename Hash, typename Pred, typename Alloc>
unordered_map<Key, Value, Hash, Pred, Alloc>::~unordered_map()
{
}

template<typename Key, typename Value, typename Hash, typename Pred, typename Alloc>
unordered_map<Key, Value, Hash, Pred, Alloc>& unordered_map<Key, Value, Hash, Pred, Alloc>::operator=(const unordered_map& map)
{
    base_type::operator=(map);
    return *this;
}

template<typename Key, typename Value, typename Hash, typename Pred, typename Alloc>
Value& unordered_map<Key, Value, Hash, Pred, Alloc>::operator[](const Key& key)
{
    iterator<Key, Value> iter = base_type::find(key, this->key_eq());
    if (iter == this->end())
    {
        iter = base_type::insert(jitstd::pair<const Key, mapped_type>(key, mapped_type())).first;
    }
    return (*iter).second;
}

template<typename Key, typename Value, typename Hash, typename Pred, typename Alloc>
Value& unordered_map<Key, Value, Hash, Pred, Alloc>::operator[](key_type&& key)
{
    iterator<Key, Value> iter = base_type::find(key, this->key_eq());
    if (iter == this->end())
    {
        iter = base_type::insert(jitstd::pair<const Key, mapped_type>(key, mapped_type())).first;
    }
    return (*iter).second;
}


template<typename Key, typename Value, typename Hash, typename Pred, typename Alloc>
typename unordered_map<Key, Value, Hash, Pred, Alloc>::iterator
unordered_map<Key, Value, Hash, Pred, Alloc>::insert(const key_type& key, const mapped_type& value)
{
    typename unordered_map<Key, Value, Hash, Pred, Alloc>::iterator iter = base_type::find(key, this->key_eq());
    iter = base_type::insert(jitstd::pair<const Key, mapped_type>(key, value)).first;
    return iter;
}

}