-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.h
More file actions
executable file
·116 lines (96 loc) · 2.29 KB
/
Copy pathList.h
File metadata and controls
executable file
·116 lines (96 loc) · 2.29 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
#ifndef _LIST_H_
#define _LIST_H_
#define MAX_LOST_LIMIT 100
#define MAX_PREALLOC_SIZE 100
#include <cstdlib>
#include <cstring>
#include <cstdio>
template <class T> class List {
private:
T * table = NULL;
unsigned int size = 0;
unsigned int allocSize = 0;
void changeAllocSize(int addToSize);
public:
T get(unsigned int id);
void set(unsigned int id, T);
unsigned int add(T);
void remove(unsigned int id);
void searchAndRemove(T data);
unsigned int getSize();
//tests
unsigned int getAllocSize();
};
template <class T> T List<T>::get(unsigned int id)
{
if(id >= size)
{
printf("List get out of range id");
}
return table[id];
}
template <class T> void List<T>::set(unsigned int id, T data)
{
if(id >= size)
{
printf("List get out of range id");
}
table[id] = data;
}
template <class T> unsigned int List<T>::add(T data)
{
if(size+1 > allocSize) // out of slot
{
changeAllocSize(((int)(MAX_PREALLOC_SIZE)/sizeof(T))+1); // add more than enough
}
table[size] = data;
size++;
return size-1;
}
template <class T> void List<T>::searchAndRemove(T data)
{
for(unsigned int pos = 0 ; pos < size ; pos++)
{
if(table[pos] == data)
{
remove(pos);
pos--;
}
}
}
template <class T> void List<T>::remove(unsigned int id)
{
unsigned int lostSize = ((allocSize-size)+1);
memmove(table+id,table+id+1,(size-id-1)*sizeof(T));
if(lostSize*sizeof(T) > MAX_LOST_LIMIT) // too much waste
{
changeAllocSize((int)-lostSize); // remove until it's compact
}
size--;
}
template <class T> void List<T>::changeAllocSize(int countToAdd)
{
allocSize += (unsigned int)countToAdd;
if(allocSize > 0)
{
table = (T*)realloc(table,allocSize*sizeof(T));
if(table == NULL)
{
printf("List changeSize Out of memory");
}
}
else
{
free(table);
table = (T*)NULL;
}
}
template <class T> unsigned int List<T>::getSize()
{
return size;
}
template <class T> unsigned int List<T>::getAllocSize()
{
return allocSize;
}
#endif