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 215 216 217 218 219 220
|
/****************************************************************************
* Copyright (C) from 2009 to Present EPAM Systems.
*
* This file is part of Indigo toolkit.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/
#ifndef __multimap_h__
#define __multimap_h__
#include "base_cpp/red_black.h"
namespace indigo
{
DECL_EXCEPTION(MultiMapError);
template <typename K, typename V> class MultiMap : public NonCopyable
{
public:
DECL_TPL_ERROR(MultiMapError);
explicit MultiMap()
{
}
~MultiMap()
{
}
const RedBlackSet<K>& keys() const;
const RedBlackSet<V>& get(const K& k) const;
const RedBlackSet<V>& operator[](const K& k) const;
int size() const;
bool find(const K& k, const V& v) const;
void insert(const K& k, const V& v);
void insert(const K& k, const Array<V>& vs);
void insert(const K& k, const RedBlackSet<V>& vs);
bool remove(const K& k);
bool remove(const K& k, const V& v);
void invert(MultiMap<V, K>& target) const;
void copy(MultiMap<K, V>& target) const;
void clear();
protected:
RedBlackSet<V>& _provide_set(const K& k);
template <typename L, typename R> void _copy(MultiMap<L, R>& target, bool invert) const
{
for (auto i = _map.begin(); i != _map.end(); i = _map.next(i))
{
const RedBlackSet<V>& set = *_sets[_map.value(i)];
for (auto j = set.begin(); j != set.end(); j = set.next(j))
{
K& k = _map.key(i);
V& v = set.key(j);
if (invert)
{
target.insert(v, k);
}
else
{
target.insert(k, v);
}
}
}
}
RedBlackMap<K, int> _map;
RedBlackSet<K> _keys;
PtrArray<RedBlackSet<V>> _sets;
const RedBlackSet<V> _nil;
};
} // namespace indigo
#ifdef _DEBUG
#include "assert.h"
#define CHECK_KEYS assert(_map.size() == _keys.size());
#else
#define CHECK_KEYS
#endif
using namespace indigo;
template <typename K, typename V> bool MultiMap<K, V>::find(const K& k, const V& v) const
{
return get(k).find(v);
}
template <typename K, typename V> const RedBlackSet<V>& MultiMap<K, V>::get(const K& k) const
{
int* i = _map.at2(k);
if (i)
{
return *_sets[*i];
}
return _nil;
}
template <typename K, typename V> void MultiMap<K, V>::insert(const K& k, const V& v)
{
_provide_set(k).insert(v);
}
template <typename K, typename V> void MultiMap<K, V>::insert(const K& k, const Array<V>& vs)
{
RedBlackSet<V>& set = _provide_set(k);
for (auto i = 0; i < vs.size(); i++)
{
set.insert(vs[i]);
}
}
template <typename K, typename V> void MultiMap<K, V>::insert(const K& k, const RedBlackSet<V>& vs)
{
RedBlackSet<V>& set = _provide_set(k);
for (auto i = vs.begin(); i != vs.end(); i = vs.next(i))
{
set.insert(vs.key(i));
}
}
template <typename K, typename V> bool MultiMap<K, V>::remove(const K& k)
{
int* i = _map.at2(k);
if (!i)
{
return false;
}
_sets.remove(*i);
_keys.remove(k);
_map.remove(k);
CHECK_KEYS;
return true;
}
template <typename K, typename V> bool MultiMap<K, V>::remove(const K& k, const V& v)
{
int* i = _map.at2(k);
if (!i)
{
return false;
}
RedBlackSet<V>& set = *_sets[*i];
set.remove(v);
if (set.size() < 1)
{
_sets.remove(*i);
_keys.remove(k);
_map.remove(k);
}
CHECK_KEYS;
return true;
}
template <typename K, typename V> void MultiMap<K, V>::copy(MultiMap<K, V>& target) const
{
_copy(target, false);
}
template <typename K, typename V> void MultiMap<K, V>::invert(MultiMap<V, K>& target) const
{
_copy(target, true);
}
template <typename K, typename V> void MultiMap<K, V>::clear()
{
_map.clear();
_keys.clear();
_sets.clear();
}
template <typename K, typename V> const RedBlackSet<K>& MultiMap<K, V>::keys() const
{
return _keys;
}
template <typename K, typename V> int MultiMap<K, V>::size() const
{
return _map.size();
}
template <typename K, typename V> const RedBlackSet<V>& MultiMap<K, V>::operator[](const K& k) const
{
return get(k);
}
template <typename K, typename V> RedBlackSet<V>& MultiMap<K, V>::_provide_set(const K& k)
{
int* i = _map.at2(k);
if (i)
{
return *_sets[*i];
}
_keys.insert(k);
_map.insert(k, _sets.size());
CHECK_KEYS;
return _sets.add(new RedBlackSet<V>());
}
#endif
|