-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorderedSet.d
executable file
·113 lines (106 loc) · 3.62 KB
/
orderedSet.d
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
/* Interface for the various structures tested: avl, Btrees etc implement this interface
* Two functions are defined below to standardize tests on ordered structures. These functions are used for
* timing of insertions and removals.
* Copyright (c) 2013 Sébastien KUNZ-JACQUES
*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* See <http://www.gnu.org/licenses/>. */
module orderedSet;
import std.stdio;
import std.random;
import std.datetime;
interface OrderedSet(T)
{
bool insert(T value);
bool remove(T value);
bool check();
void setDebugFlag(bool);
}
/* Insert numElts chosen at random between 0 and numElts. With numElts large, there are redundancies with
* overwhelming probability. Track elements presence in a regular boolean array; check that an element is inserted
* exactly in the cases when it was not already present.
* The self-consistency check method check() is optionally called after each insertion.
*/
void orderedSetInsertTest(OrderedSet!(uint) set, uint numElts, bool doCheck)
{
uint [] values = new uint[numElts];
bool [] state = new bool[numElts];
auto rnd = Random(1);
for(int i = 0; i < numElts; i++)
{
values[i] = uniform(0, numElts, rnd);
state[i] = false;
}
StopWatch sw;
writeln("Inserting ", numElts, " elements in random order");
sw.start();
ulong count;
for(int i=0; i < numElts; i++)
{
bool inserted = set.insert(values[i]);
if(doCheck) assert(set.check());
if(!state[values[i]] && !inserted)
{
writeln(i, ": value ", values[i], " absent but not inserted");
assert(false);
}
if(state[values[i]] && inserted)
{
writeln(i, ": value ", values[i], " already present but inserted");
assert(false);
}
if(!state[values[i]])
{
count++;
}
state[values[i]] = true;
}
sw.stop();
writeln("Done inserting ", numElts, " elements with ", count, " distinct values. Time: ", sw.peek().msecs,
" milliseconds.");
}
/* Removes the numElts elements inserted by orderedSetInsertTest.
* check that actual removals are done exactly when the element removed was supposed to be stored.
* The self-consistency check method check() is optionally called after each removal.
*/
void orderedSetRemoveTest(OrderedSet!(uint) set, uint numElts, bool doCheck)
{
uint [] values = new uint[numElts];
bool [] state = new bool[numElts];
auto rnd = Random(1);
for(int i = 0; i < numElts; i++)
{
values[i] = uniform(0, numElts, rnd);
state[values[i]] = true;
}
StopWatch sw;
writeln("Removing values: ");
sw.reset();
sw.start();
for(int i = 0; i < numElts; i++)
{
bool removed = set.remove(values[i]);
if(doCheck) assert(set.check());
if(state[values[i]] && !removed)
{
writeln(i, ": Value present but not removed: ", values[i]);
assert(false);
}
if(!state[values[i]] && removed)
{
writeln(i, ": Value absent but removed: ", values[i]);
assert(false);
}
state[values[i]] = false;
}
sw.stop();
writeln("Done removing ", numElts, " elements. Time: ", sw.peek().msecs, " milliseconds.");
}