-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cc
153 lines (114 loc) · 3.55 KB
/
main.cc
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdio.h>
#include <iostream>
#include <iterator>
#include <vector>
#include <functional>
#include <set>
#include "skiplist.h"
// turn off debug statements
#define printf
using namespace std;
using namespace nonstd;
class MyDataItem
{
public:
MyDataItem() {
mData = new int[10];
printf("Got created\n");
}
MyDataItem(MyDataItem&& other) {
mData = other.mData;
id = other.id;
other.mData = NULL;
printf("Got moved\n");
}
MyDataItem(const MyDataItem& other) {
mData = new int(10);
id = other.id;
printf("Got copy constructed.\n");
}
MyDataItem& operator=(const MyDataItem& other) {
if (this != &other) {
int* newData = new int[10];
std::copy(other.mData, other.mData + 10, newData);
delete [] mData;
mData = newData;
id = other.id;
printf("Got assigned.\n");
}
return *this;
}
MyDataItem& operator=(MyDataItem&& other) {
if (this != &other) {
delete [] mData;
mData = other.mData;
id = other.id;
other.mData = NULL;
printf("Got move assigned\n");
}
return *this;
}
~MyDataItem() {
delete mData;
mData = NULL;
}
bool operator< (const MyDataItem& other) const {
printf("comparing less %d and %d\n", id, other.id);
return id < other.id;
}
bool operator> (const MyDataItem& other) const {
printf("comparing greater %d and %d\n", id, other.id);
return id > other.id;
}
friend ostream& operator<< (ostream& os, const MyDataItem& myDataItem) {
return os << myDataItem.id;
}
int id;
private:
int* mData;
};
int main() {
less<MyDataItem> myLess;
skiplist<MyDataItem> myList(myLess);
MyDataItem item1; item1.id = 1;
MyDataItem item2; item2.id = 2;
MyDataItem item3; item3.id = 3;
myList.insert(item1);
myList.insert(item3);
myList.insert(item2);
auto it = myList.find(item2);
cout << (it == myList.end()) << endl;
cout << it->id << endl;
cout << (*it).id << endl;
for (auto it2 = myList.begin(); it2 != myList.end(); ++it2)
cout << it2->id << endl;
cout << "next should be three after deleting two: " << myList.erase(it)->id << endl;
for (auto it2 = myList.begin(); it2 != myList.end(); ++it2)
cout << it2->id << endl;
cout << "size is now: " << myList.size() << endl;
cout << "last element is now: " << myList[myList.size() - 1].id << endl;
auto allocator = myList.get_allocator();
skiplist<MyDataItem> myList2(myList);
cout << "last element in copy is now: " << myList2[myList2.size() - 1].id << endl;
// greater
greater<MyDataItem> myGreater;
skiplist<MyDataItem, greater<MyDataItem>> myList3(myGreater);
myList3.insert(item1);
myList3.insert(item3);
myList3.insert(item2);
for (auto it = myList3.begin(); it != myList3.end(); ++it)
cout << it->id << endl;
skiplist<MyDataItem> myList6;
myList6.insert(item1);
myList6.insert(item2);
myList6.insert(item3);
copy(myList6.begin(), myList6.end(), ostream_iterator<MyDataItem>(cout, " "));
cout << endl;
MyDataItem item4; item4.id = 4;
myList2.insert(item4);
cout << "last element in copy is now: " << myList2[myList2.size() - 1].id << endl;
cout << "first list now contains " << myList.size() << " elements" << endl;
swap(myList, myList2);
cout << "first list now contains " << myList.size() << " elements" << endl;
// TODO write a speed test that creates a random set of operatios, and then applies this to the skip list as well as std set
}