-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListType.cpp
150 lines (125 loc) · 3 KB
/
ListType.cpp
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
#ifndef LISTTYPE_CPP
#define LISTTYPE_CPP
#include <iostream>
using namespace std;
template <class T>
class ListType
{
private:
static const int HASH_SPACE = 0x10; // 16 in hex
struct NodeType
{
T payload;
NodeType* next;
NodeType() { this->next = nullptr; }
};
NodeType **roots;
NodeType **envPtr;
public:
ListType();
void create();
bool isRoom();
bool isThere(T &);
void put(T &);
void remove(T &);
void print(string);
int getHashIndex(unsigned long);
};
template <class T>
ListType<T>::ListType()
{
this->roots = nullptr;
this->envPtr = nullptr;
create();
}
template <class T>
void ListType<T>::create()
{
this->roots = new NodeType*[HASH_SPACE];
for(int i = 0; i < HASH_SPACE; i++)
this->roots[ i ] = nullptr;
this->envPtr = nullptr;
}
template <class T>
bool ListType<T>::isRoom()
{
// Note: There is no standard way to do this with 100% certainty.
NodeType *n1;
n1 = new NodeType;
// Allocation failure returns nullptr
if ( n1 )
{
delete n1;
return true;
}
else return false;
}
template <class T>
bool ListType<T>::isThere(T &unit)
{
unsigned long h = unit.getHash();
int i = getHashIndex(h);
this->envPtr = &this->roots[ i ];
while( *this->envPtr && (*this->envPtr)->payload != unit )
this->envPtr = &(*this->envPtr)->next;
//cout << "Identifier: " << unit.identifier << "\tIndex: " << i << endl;
return *this->envPtr && (*this->envPtr)->payload == unit;
/*
cout<<"The long int (in hex) returned from itemType is: "
<<hex<<h<<'\n'<<"The index for "<<unit.identifier
<<" will be "<<hex<<i<<" in hex or "<<dec<<i
<<" in decimal.\n\n";
*/
}
template <class T>
void ListType<T>::put(T &unit)
{
if ( !this->envPtr )
{
cout << "envPtr == nullptr" << endl;
}
if ( !isThere(unit) )
{
NodeType *n1 = *this->envPtr;
*this->envPtr = new NodeType;
(*this->envPtr)->payload = unit;
(*this->envPtr)->next = n1;
}
}
template <class T>
void ListType<T>::remove(T &unit)
{
if ( isThere(unit) )
{
NodeType *n1;
n1 = *this->envPtr;
*this->envPtr = (*this->envPtr)->next;
n1->next = nullptr;
delete n1;
}
}
template <class T>
void ListType<T>::print(string str)
{
cout << str << endl;
cout << endl << endl << endl;
cout << "***********************" << endl;
for (int i = 0; i < HASH_SPACE; i++)
{
cout << "Bucket: " << i << endl;
this->envPtr = &roots[ i ];
while ( *this->envPtr )
{
cout << (*this->envPtr)->payload << endl;
this->envPtr = &(*this->envPtr)->next;
}
cout << endl;
}
cout << "***********************" << endl;
}
template <class T>
int ListType<T>::getHashIndex(unsigned long k)
{
return (k % HASH_SPACE);
}
#endif // LISTTYPE_CPP