-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtlist.h
175 lines (144 loc) · 5.78 KB
/
tlist.h
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*******************************************************************************
* wirbelscan: A plugin for the Video Disk Recorder
* See the README file for copyright information and how to reach the author.
******************************************************************************/
#pragma once
#include <vector>
#include <mutex>
#include <algorithm>
/*******************************************************************************
* returns true if Item1 should be sorted before Item2 in a TList.
******************************************************************************/
typedef bool (*TListSortCompare)(void* Item1, void* Item2);
/*******************************************************************************
* class TList
******************************************************************************/
template<class T> class TList {
private:
std::mutex m;
protected:
std::vector<T> v;
public:
TList(void) {} // constructor
TList(const TList<T>& other) : v(other.v) {} // non-swap copy constructor
~TList() { v.clear(); }
void Add(T p) { // Add a new item to the list.
const std::lock_guard<std::mutex> lock(m);
v.push_back(p);
if (v.size() == v.capacity())
v.reserve(v.capacity() << 1);
}
int Capacity(void) { // returns max number of items
const std::lock_guard<std::mutex> lock(m);
return v.capacity();
}
void Capacity(size_t newCap) { // sets max number of items
const std::lock_guard<std::mutex> lock(m);
v.reserve(newCap);
}
void Clear(void) { // Clears the list.
const std::lock_guard<std::mutex> lock(m);
v.clear();
}
int Count(void) { // Current number of items.
const std::lock_guard<std::mutex> lock(m);
return v.size();
}
void Delete(size_t Index) { // Removes items from list.
const std::lock_guard<std::mutex> lock(m);
v.erase(v.begin()+Index);
}
void Exchange(size_t Index1, size_t Index2) { // Exchanges two items
const std::lock_guard<std::mutex> lock(m);
T p1 = v[Index1];
T p2 = v[Index2];
v[Index1] = p2;
v[Index2] = p1;
}
TList<T> Expand(void) { // Increases the capacity of the list if needed.
const std::lock_guard<std::mutex> lock(m);
if (v.size() == v.capacity())
v.resize(v.capacity() << 1);
return *this;
}
T First(void) { // Returns the first non-nil pointer in the list.
const std::lock_guard<std::mutex> lock(m);
return v.front();
}
T Last(void) { // Returns the last non-nil pointer in the list.
const std::lock_guard<std::mutex> lock(m);
return v.back();
}
int IndexOf(T Item) { // Returns the index of a given item.
const std::lock_guard<std::mutex> lock(m);
for(size_t i=0; i<v.size(); i++) {
if (v[i] == Item)
return i;
}
return -1;
}
void Insert(size_t Index, T Item) { // Inserts a new pointer in the list at a given position.
const std::lock_guard<std::mutex> lock(m);
v.insert(v.begin() + Index , Item);
}
void Move(size_t CurIndex, size_t NewIndex) { // Moves a pointer from one position in the list to another.
if (CurIndex == NewIndex)
return;
const std::lock_guard<std::mutex> lock(m);
T& item = v[CurIndex];
Delete(CurIndex);
if (CurIndex < NewIndex)
Insert(NewIndex-1, item);
else
Insert(NewIndex , item);
}
T& operator[](int const& Index) { // Provides access to Items (pointers) in the list.
const std::lock_guard<std::mutex> lock(m);
return v[Index];
}
TList<T>& operator=(const TList<T>& other) { // copy assignment operator.
if (this != &other) {
const std::lock_guard<std::mutex> lock(m);
v = other.v;
}
return *this;
}
T& Items(size_t const& Index) {
const std::lock_guard<std::mutex> lock(m);
return v[Index];
}
int Remove(T Item) { // Removes a value from the list & returns it's index before removal
const std::lock_guard<std::mutex> lock(m);
for(size_t i=0; i<v.size(); i++) {
if (v[i] == Item) {
v.erase(v.begin()+i);
return i;
}
}
return -1;
}
void Sort(TListSortCompare Compare) { // Sorts the items in the list using a function.
const std::lock_guard<std::mutex> lock(m);
std::sort(v.begin(), v.end(), Compare);
}
void Sort(void) { // Sorts the items in the list using operator '<'.
const std::lock_guard<std::mutex> lock(m);
std::sort(v.begin(), v.end());
}
void Assign(TList<T>& from) { // Copy the contents of other lists.
const std::lock_guard<std::mutex> lock(m);
v.assign(from.v.begin(), from.v.end());
}
void AddList(TList<T>& aList) { // Add all items from another list
const std::lock_guard<std::mutex> lock(m);
v.insert(v.end(),aList.v.begin(),aList.v.end());
}
T* List(void) { // Returns the items in an array.
const std::lock_guard<std::mutex> lock(m);
return v.data();
}
void Pack(void) { // Removes nullptr's from the list and frees unused memory.
const std::lock_guard<std::mutex> lock(m);
v.shrink_to_fit();
}
};