blob: 063395bba65b610705282150c6eac97310fd1821 (
plain) (
blame)
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
|
#include "mem_block.h"
class ptr_list
{
private:
mem_block_t<void *> data;
int count;
protected:
void * get_raw_ptr() {return data.get_ptr();}
public:
ptr_list() {count=0;}
int get_count() const {return count;}
void * get_item(int n) const
{
if (n>=0 && n<count) return data[n];
else return 0;
}
int add_item(void * ptr)//returns index
{
count++;
data.check_size(count);
data[count-1] = ptr;
return count-1;
}
int find_item(void * ptr)//returns index, -1 if not found
{
int n;
for(n=0;n<count;n++)
{
if (data[n] == ptr) return n;
}
return -1;
}
bool have_item(void * ptr) {return find_item(ptr)>=0;}
void remove_item(void * ptr)
{
int idx = find_item(ptr);
if (idx>=0) remove_by_idx(idx);
}
void * remove_by_idx(int idx)
{
void * ptr = 0;
if (idx>=0 && idx<count)
{
ptr = data[idx];
int n;
count--;
for(n=idx;n<count;n++)
{
data[n] = data[n+1];
}
}
return ptr;
}
void remove_all()
{
count=0;
}
int insert_item(void * ptr,int idx) //returns index
{
if (idx>count || idx<0) idx = count;
count++;
data.check_size(count);
int n;
for(n=count-1;n>idx;n--)
{
data[n]=data[n-1];
}
data[idx] = ptr;
return idx;
}
void * operator[](int idx) const {return get_item(idx);}
};
template<class T>
class ptr_list_t : protected ptr_list
{
public:
int get_count() const {return ptr_list::get_count();}
T * get_item(int n) const {return static_cast<T*>(ptr_list::get_item(n));}
int add_item(T * ptr) {return ptr_list::add_item(static_cast<void*>(ptr));}
int find_item(T * ptr) {return ptr_list::find_item(static_cast<void*>(ptr));}
bool have_item(T * ptr) {return ptr_list::have_item(static_cast<void*>(ptr));}
void remove_item(T * ptr) {ptr_list::remove_item(static_cast<void*>(ptr));}
T * remove_by_idx(int idx) {return static_cast<T*>(ptr_list::remove_by_idx(idx));}
void remove_all() {ptr_list::remove_all();}
void * operator[](int idx) const {return get_item(idx);}
int insert_item(int idx,T* ptr) {return ptr_list::insert_item(idx,static_cast<void*>(ptr));}
void delete_item(T * ptr)
{
remove_item(ptr);
delete ptr;
}
void delete_by_idx(int idx)
{
T * ptr = remove_by_idx(idx);
if (ptr) delete ptr;
}
void delete_all()
{
int n,max=get_count();
for(n=0;n<max;n++)
{
T * ptr = get_item(n);
if (ptr) delete ptr;
}
remove_all();
}
void sort(int (__cdecl *compare )(const T ** elem1, const T** elem2 ) )
{
qsort(get_raw_ptr(),get_count(),sizeof(void*),(int (__cdecl *)(const void *, const void *) )compare);
}
};
|