-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEfficientMap.h
More file actions
134 lines (101 loc) · 3.42 KB
/
EfficientMap.h
File metadata and controls
134 lines (101 loc) · 3.42 KB
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
#ifndef _EFFICIENT_MAP_H
#define _EFFICIENT_MAP_H
// This is a template for a lookup table, associating a key
// with some data. For large tables, this is going to be more
// efficient than the InefficientMap template, since it makes use
// of a reasonably complex skip list data structure to obtain log-n
// inserts, lookups, and removes. However, for small tables (less
// than 20 items) it is probably going to be less efficient due to
// the extra overhead that the more complex data structure incurs.
// Also, unlike the InefficientMap, it requires the LessThan () over keys.
//
// Key requires Swap (), IsEqual (), LessThan (), CopyFrom ()
// Data requires Swap (), CopyFrom ()
//
// this constant is relatively unimportant... the data structure will have
// efficient performance up to approximately 2^MAXLEVELS items, but there is
// no reason to have it too large!
#include <cstring>
#define MAXLEVELS 64
using namespace std;
template <class Key, class Data>
class EfficientMap {
// forward declaration
struct Node;
public:
typedef Key keyType;
typedef Data dataType;
// constructor and destructor
EfficientMap ();
virtual ~EfficientMap ();
// remove all the content
void Clear(void);
// the length of the map
int Length();
// inserts the key/data pair into the structure
void Insert (Key& key, Data& data);
// eat up another map
// plays nicely and removes duplicates
void SuckUp(EfficientMap& other);
// get the content from another map (without destroying it)
void CopyFrom(EfficientMap& other);
// removes one (any) instance of the given key from the map...
// returns a 1 on success and a zero if the given key was not found
int Remove (Key& findMe, Key& putKeyHere, Data& putDataHere);
// attempts to locate the given key
// returns 1 if it is, 0 otherwise
int IsThere (Key& findMe);
// returns a reference to the data associated with the given search key
// if the key is not there, then a garbage (newly initialized) Data item is
// returned. "Plays nicely" with IsThere in the sense that if IsThere found
// an item, Find will immediately return that item w/o having to locate it
Data &Find (Key& findMe);
// swap two of the maps
void Swap (EfficientMap& withMe);
///////////// ITERATOR INTERFAACE //////////////
// look at the current item
Key& CurrentKey ();
Data& CurrentData ();
// move the current pointer position backward through the list
void Retreat ();
// move the current pointer position forward through the list
void Advance ();
// operations to consult state
bool AtStart ();
bool AtEnd ();
// operations to move the the start of end of a list
void MoveToStart ();
void MoveToFinish ();
private:
// versions of the above that work only at a specific level of the skip list
void Insert (Node* temp, int whichLevel);
void Remove (Node*& removeMe, int whichLevel);
void Advance (int whichLevel);
int AtEnd (int whichLevel);
Key& CurrentKey (int i);
Data& CurrentData (int i);
struct Node {
// data
Key key;
Data data;
// backward link
Node* previous;
// forward links
Node** next;
int numLinks;
// constructors and destructor
Node (int numLinksIn) {numLinks = numLinksIn; next = new Node*[numLinks];}
Node () {previous = next = NULL; numLinks = 0;}
virtual ~Node () {delete [] next;}
};
struct Header {
// data
Node* first;
Node* last;
Node* current;
int curDepth;
};
// the list itself is pointed to by this pointer
Header* list;
};
#endif