Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: compiled/IntMap.h

Issue 29572731: Issue 5141 - Generalize Map class to allow non-strings as keys (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Patch Set: Use constructor inheritance Created Oct. 10, 2017, 6:30 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | compiled/Map.h » ('j') | compiled/Map.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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;
sergei 2017/10/11 10:03:21 can it be more specific, e.g. uint32_t or int32_t
Wladimir Palant 2017/10/11 18:28:30 Done. Not going to add any template parameters how
sergei 2017/10/17 12:58:06 Acknowledged.
29 typedef size_t size_type;
30
31 // The assumption here is that valid keys will always be non-negative
sergei 2017/10/11 10:03:21 I would remove these comment, having a couple of r
Wladimir Palant 2017/10/11 18:28:30 Done. However, we need sentinel values at the edge
32 static const int KEY_INVALID = -1;
sergei 2017/10/11 10:03:21 the type should be key_type, not int.
Wladimir Palant 2017/10/11 18:28:30 Done.
33 static const int KEY_DELETED = -2;
hub 2017/10/10 20:37:53 Do these two const need to be public?
Wladimir Palant 2017/10/11 18:28:31 Done.
34
35 int first;
sergei 2017/10/11 10:03:21 it should be key_type
Wladimir Palant 2017/10/11 18:28:30 Done.
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
sergei 2017/10/11 10:03:21 Maybe it's a premature generalization but I would
sergei 2017/10/11 10:05:11 IntSetEntry is of course from IntMap_internal.
Wladimir Palant 2017/10/11 18:28:30 Yes, probably premature generalization. Trouble is
sergei 2017/10/17 12:58:06 Acknowledged.
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>;
OLDNEW
« no previous file with comments | « no previous file | compiled/Map.h » ('j') | compiled/Map.h » ('J')

Powered by Google App Engine
This is Rietveld