blob: 597a2b413e2010a921ee1792a0ef4c64eab1103b (
plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
/*
Facebook plugin for Miranda Instant Messenger
_____________________________________________
Copyright © 2009-11 Michal Zelinka, 2011-12 Robert Pösel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
namespace List
{
template< typename T > class Item
{
public:
std::string key;
T* data;
Item< T >* prev;
Item< T >* next;
Item( )
{
this->data = NULL;
this->prev = NULL;
this->next = NULL;
}
~Item( )
{
delete this->data;
}
};
template< typename T > class List
{
private:
Item< T >* first;
Item< T >* last;
unsigned int count;
public:
List( )
{
this->first = this->last = NULL;
this->count = 0;
}
~List( )
{
this->clear( );
}
Item< T >* begin( )
{
return first;
}
Item< T >* end( )
{
return last;
}
unsigned int size( )
{
return count;
}
bool empty( )
{
return ( this->first == NULL );
}
void insert( Item< T >* item )
{
if ( this->empty( ) )
{
this->first = this->last = item;
this->count = 1;
} else { // TODO: key sorting/comparation
item->next = this->first;
this->first->prev = item;
this->first = item;
this->count++;
}
}
void insert( std::pair< std::string, T* > item )
{
Item<T>* ins = new Item<T>;
ins->key = item.first;
ins->data = item.second;
this->insert( ins );
}
void erase( std::string key )
{
Item< T >* help = this->first;
while ( help != NULL )
{
if ( help->key.compare( key ) != 0 )
help = help->next;
else
{
if ( help == this->first )
{
this->first = help->next;
if ( this->first != NULL )
this->first->prev = NULL;
else
this->last = NULL;
}
else if ( help == this->last )
{
this->last = help->prev;
if ( this->last != NULL )
this->last->next = NULL;
else
this->first = NULL;
}
else
{
help->prev->next = help->next;
help->next->prev = help->prev;
}
if (help != NULL)
{
this->count--;
delete help;
}
break;
}
}
}
void erase( Item< T >* item )
{
if (item != NULL)
erase( item->key );
}
T* find( std::string key )
{
Item< T >* help = this->begin( );
while ( help != NULL )
{
if ( help->key.compare( key ) != 0 )
help = help->next;
else
return help->data;
}
return NULL;
}
T* at( const unsigned int item )
{
if (item >= this->count)
return NULL;
Item< T >* help = this->begin( );
for ( unsigned int i = 0; i < item; i++ )
help = help->next;
return help->item;
}
T* operator[]( const unsigned int item )
{
return at( item );
}
void clear( )
{
Item< T >* help;
while ( this->first != NULL )
{
help = this->first;
this->first = this->first->next;
delete help;
}
this->last = NULL;
this->count = 0;
}
};
};
|