OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 #pragma once |
| 19 |
| 20 #include <cstddef> |
| 21 |
| 22 #include "Map.h" |
| 23 |
| 24 namespace IntMap_internal |
| 25 { |
| 26 struct IntSetEntry |
| 27 { |
| 28 typedef int key_type; |
| 29 typedef size_t size_type; |
| 30 |
| 31 // The assumption here is that valid keys will always be non-negative |
| 32 static const int KEY_INVALID = -1; |
| 33 static const int KEY_DELETED = -2; |
| 34 |
| 35 int first; |
| 36 |
| 37 IntSetEntry() |
| 38 : first(KEY_INVALID) |
| 39 { |
| 40 } |
| 41 |
| 42 IntSetEntry(key_type key) |
| 43 : first(key) |
| 44 { |
| 45 } |
| 46 |
| 47 bool equals(key_type other) const |
| 48 { |
| 49 return first == other; |
| 50 } |
| 51 |
| 52 bool is_invalid() const |
| 53 { |
| 54 return first == KEY_INVALID; |
| 55 } |
| 56 |
| 57 bool is_deleted() const |
| 58 { |
| 59 return first == KEY_DELETED; |
| 60 } |
| 61 |
| 62 void erase() |
| 63 { |
| 64 first = KEY_INVALID; |
| 65 } |
| 66 |
| 67 static size_type hash(key_type key) |
| 68 { |
| 69 return key; |
| 70 } |
| 71 }; |
| 72 |
| 73 template<typename Value> |
| 74 struct IntMapEntry : IntSetEntry |
| 75 { |
| 76 typedef IntSetEntry super; |
| 77 typedef Value value_type; |
| 78 |
| 79 Value second; |
| 80 |
| 81 IntMapEntry() |
| 82 : IntSetEntry(), second() |
| 83 { |
| 84 } |
| 85 |
| 86 IntMapEntry(key_type key) |
| 87 : IntSetEntry(key), second() |
| 88 { |
| 89 } |
| 90 |
| 91 IntMapEntry(key_type key, value_type value) |
| 92 : IntSetEntry(key), second(value) |
| 93 { |
| 94 } |
| 95 |
| 96 void erase() |
| 97 { |
| 98 super::erase(); |
| 99 second = value_type(); |
| 100 } |
| 101 }; |
| 102 } |
| 103 |
| 104 using IntSet = Set<IntMap_internal::IntSetEntry>; |
| 105 |
| 106 template<typename Value> |
| 107 using IntMap = Map<IntMap_internal::IntMapEntry<Value>, Value>; |
OLD | NEW |