-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIpHashTableGeneric.cpp
executable file
·74 lines (70 loc) · 1.89 KB
/
IpHashTableGeneric.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
//
// Created by root on 31.07.16.
//
#include "IpHashTableGeneric.h"
template <class T>
void IpHashTableGeneric<T>::Push(const char* ip, T&& value) {
uint wholeIp = ParseIp(ip);
uint offset = wholeIp & ipBinaryMask;
ListNode** myPtr = dataPtr + offset;
if(*myPtr == 0) {
*myPtr = new ListNode(move(value), wholeIp);
}
else {
ListNode* currNode = *myPtr;
while(currNode->next != nullptr) {
if(currNode->wholeIP == wholeIp)
return;
currNode = currNode->next;
}
if(currNode->wholeIP == wholeIp)
return;
currNode->next = new ListNode(move(value), wholeIp);
}
}
template <class T>
bool IpHashTableGeneric<T>::Check(const char* ip) {
uint wholeIp = ParseIp(ip);
uint offset = wholeIp & ipBinaryMask;
ListNode** myPtr = dataPtr + offset;
if(*myPtr == 0) {
return false;
}
else {
ListNode* currNode = *myPtr;
do {
if(currNode->wholeIP == wholeIp)
return true;
currNode = currNode->next;
} while (currNode != nullptr);
}
return false;
}
template <class T>
void IpHashTableGeneric<T>::Remove(const char* ip) {
uint wholeIp = ParseIp(ip);
uint offset = wholeIp & ipBinaryMask;
ListNode** myPtr = dataPtr + offset;
ListNode* currNode = *myPtr;
if(currNode == 0)
return;
if(currNode->wholeIP == wholeIp)
{
*myPtr = currNode->next;
delete currNode;
}
else {
ListNode* nextNode = currNode->next;
if(nextNode == nullptr)
return;
do {
if(nextNode->wholeIP == wholeIp)
{
currNode->next = nextNode->next;
delete nextNode;
}
currNode = currNode->next;
nextNode = nextNode->next;
} while(nextNode != nullptr);
}
}