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