-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslist.h
More file actions
190 lines (154 loc) · 4.17 KB
/
slist.h
File metadata and controls
190 lines (154 loc) · 4.17 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
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
/*
IL : Intlog Language
Object Oriented Prolog Project
Copyright (C) 1992-2021 - Ing. Capelli Carlo
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef SLIST_H_
#define SLIST_H_
//--------------------
// single linked list
//--------------------
#include "iafx.h"
class slist;
class e_slist;
class slist_iter;
class slist_scan;
class e_slistvptr;
class slistvptr;
class slistvptr_iter;
#define SLIST_INVPOS unsigned(-1)
class e_slist {
public:
e_slist() { next = nullptr; }
virtual ~e_slist();
unsigned offlast() const;
protected:
e_slist* next;
friend class slist;
friend class slist_iter;
friend class slist_scan;
};
class IAFX_API slist {
public:
slist() { first = nullptr; }
virtual ~slist();
unsigned append(e_slist *);
void insert(e_slist *, unsigned);
void clear();
void remove(unsigned);
unsigned numel() const;
// linear searching
unsigned seek(void*) const;
e_slist* seekptr(void*) const;
virtual int match(e_slist *e1, void *e2) const {
return e1 == e2;
}
e_slist* get(unsigned) const;
e_slist* get_first() const {
return first;
}
private:
friend class slist_iter;
friend class slist_scan;
e_slist* first;
};
//------------------
// forward iterator
//------------------
class IAFX_API slist_iter {
public:
slist_iter() { e = nullptr; }
slist_iter(const slist& sl) { set(sl); }
slist_iter(const slist* sl) { set(*sl); }
void set(const slist& sl) { e = sl.first; }
void set(const slist* sl) { e = sl? sl->first : nullptr; }
e_slist* curr() const { return e; }
e_slist* next() {
if (e) {
e_slist *n = e;
e = e->next;
return n;
}
return nullptr;
}
protected:
e_slist* e;
};
//-----------------------
// iterator with editing
//-----------------------
class IAFX_API slist_scan : public slist_iter {
public:
slist_scan(slist &l) : slist_iter(l), lref(l) {}
void delitem();
void swap(e_slist *);
private:
slist &lref;
};
//-----------------------
// list of void pointers
//-----------------------
class e_slistvptr : public e_slist {
void *vptr;
e_slistvptr(void *p);
friend class slistvptr;
friend class slistvptr_iter;
};
class IAFX_API slistvptr : public slist {
public:
// append a pointer
void append(void *p) {
slist::append(new e_slistvptr(p));
}
// insert a pointer
void insert(void *p, unsigned ix) {
slist::insert(new e_slistvptr(p), ix);
}
// return indexed pointer
void* get(unsigned index) const {
e_slistvptr* e = static_cast<e_slistvptr*>(slist::get(index));
return e? e->vptr : nullptr;
}
// matching condition
int match(e_slist *e1, void *e2) const {
return static_cast<e_slistvptr*>(e1)->vptr == reinterpret_cast<e_slistvptr*>(e2)->vptr;
}
};
class slistvptr_iter {
public:
slistvptr_iter() { e = nullptr; }
slistvptr_iter(const slistvptr& sl) {
set(sl);
}
void set(const slistvptr& sl) {
e = static_cast<e_slistvptr*>(sl.get_first());
}
void set(const slistvptr* sl) {
e = sl? static_cast<e_slistvptr*>(sl->get_first()) : nullptr;
}
void* curr() const {
return e? e->vptr : nullptr;
}
void* next() {
if (e) {
e_slistvptr *n = e;
e = static_cast<e_slistvptr*>(e->next);
return n->vptr;
}
return nullptr;
}
protected:
e_slistvptr* e;
};
#endif