00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
#ifndef OS_UTIL_MNLIST_H
00025
#define OS_UTIL_MNLIST_H
00026
00027
#include <assert.h>
00028
#include <string.h>
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
#ifdef __cplusplus
00039
00040
#ifndef ASSERT_NON_NULL
00041
#define ASSERT_NON_NULL(a)
00042
#endif
00043
00044
00045
00046
00047
00048
template <
class T,
class M>
00049 inline void*
MNForall_assign(T& r,
void* p,
const M& l) {
00050
if(p)
00051 {
00052 r = l.inf(p);
00053 ASSERT_NON_NULL(r);
00054 }
00055
return p;
00056 }
00057
00058
#define MNForall_items(r,l) \
00059
for ( r = l.first(); \
00060
r != NULL; \
00061
r = l.succ(r) )
00062
00063
#define MNForall_var MNForall_runner##__LINE__
00064
00065
#define MNForall(r,l) \
00066
for ( void* \
00067
MNForall_var = MNForall_assign(r,l.first(),l); \
00068
MNForall_var != NULL; \
00069
MNForall_var = MNForall_assign(r,l.succ(MNForall_var),l) )
00070
00071
00072
00073
00074
00075
00076
00077
00078 class MNVPListItem
00079 {
00080
MNVPListItem* _succ;
00081
MNVPListItem* _pred;
00082
public:
00083
MNVPListItem( );
00084 virtual ~MNVPListItem() { }
00085
00086
MNVPListItem*
succ() const;
00087
MNVPListItem* pred() const;
00088
void setPred(
MNVPListItem* pred );
00089
void setSucc(
MNVPListItem* succ );
00090
void validityCheck();
00091
00092 virtual
MNVPListItem* clone() const = 0;
00093 };
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 class
MNVPList
00104 {
00105
private:
00106
int _len;
00107
MNVPListItem* _first;
00108
MNVPListItem* _last;
00109
00110
protected:
00111
void clone (
const MNVPList& orig );
00112
00113
public:
00114 MNVPList (
const MNVPList& orig );
00115 MNVPList ( );
00116
00117 ~MNVPList ( );
00118
00119 MNVPList& operator=(
const MNVPList& orig );
00120
00121
MNVPListItem* first()
const;
00122
MNVPListItem* last()
const;
00123
MNVPListItem*
pred(
MNVPListItem* item)
const;
00124
MNVPListItem*
succ(
MNVPListItem* item)
const;
00125
bool empty()
const;
00126
int length()
const;
00127
00128
void del_item(
MNVPListItem* x );
00129
void del_back();
00130
void del_front();
00131
void push_front(
MNVPListItem* x );
00132
void push_back(
MNVPListItem* x );
00133
void ins_before (
MNVPListItem* x,
MNVPListItem* y );
00134
void ins_after (
MNVPListItem* x,
MNVPListItem* y );
00135
void clear ( );
00136 };
00137
00138
00139
00140
00141
00142 template <
class T>
class MNList
00143 {
00144
MNVPList l;
00145
private:
00146
00147
00148
00149
00150
class Item :
public MNVPListItem
00151 {
00152
public:
00153 T _data;
00154 Item(
const T& t) : _data(t) {
00155 ASSERT_NON_NULL ( _data );
00156 }
00157
00158
virtual ~Item() { }
00159
00160
virtual const T*
data()
const {
00161
return &_data;
00162 }
00163
00164
virtual MNVPListItem*
clone()
const {
00165 ASSERT_NON_NULL ( _data );
00166
return new Item(_data);
00167 }
00168 };
00169
00170
public:
00171 MNList (
const MNList<T>& orig ) : l(orig.l) {
00172 }
00173 MNList ( ) {
00174 }
00175
00176 ~
MNList ( ) { }
00177
00178 MNList<T>& operator=(
const MNList<T>& orig ) {
00179 l=orig.
l;
00180
return *
this;
00181 }
00182
00183 inline void* first()
const {
return l.
first(); }
00184 inline void* last()
const {
return l.last(); }
00185 inline void*
pred(
void* item)
const {
return l.
pred((
MNVPListItem*)item); }
00186 inline void*
succ(
void* item)
const {
return l.
succ((
MNVPListItem*)item); }
00187 inline bool empty()
const {
return l.empty(); }
00188 inline int length()
const {
return l.length(); }
00189
00190 inline const T& peek_front()
const {
return ((Item*)l.first())->_data; }
00191 inline T peek_front() {
return ((Item*)l.first())->_data; }
00192 inline const T& peek_back()
const {
return ((Item*)l.last())->_data; }
00193 inline T peek_back() {
return ((Item*)l.last())->_data; }
00194 inline const T& peek_here(
void* r)
const {
return ((Item*)r)->_data; }
00195
00196 inline const T& inf(
void* r)
const {
00197 assert ( r );
00198 Item* rc = (Item*)r;
00199 ASSERT_NON_NULL ( rc->_data );
00200
00201
return *(rc->data());
00202 }
00203 inline T inf(
void* r) {
00204 assert ( r );
00205 Item* rc = (Item*)r;
00206 ASSERT_NON_NULL ( rc->_data );
00207
00208
return *(rc->data());
00209 }
00210 inline void del_item(
void* item) { l.del_item((
MNVPListItem*)item); }
00211 inline void del_front() { l.del_front(); }
00212 inline void del_back() { l.del_back(); }
00213 inline void clear() { l.clear(); }
00214
00215 inline T pop() {
00216 T ret = peek_front();
00217 l.del_front();
00218 ASSERT_NON_NULL ( ret );
00219
return ret;
00220 }
00221
00222 inline void push_front( T x ) {
00223 ASSERT_NON_NULL ( x );
00224 l.push_front(
new Item(x));
00225 }
00226 inline void prepend( T x ) {
00227 ASSERT_NON_NULL ( x );
00228 l.push_front(
new Item(x));
00229 }
00230 inline void push_back( T x ) {
00231 ASSERT_NON_NULL ( x );
00232 l.push_back(
new Item(x));
00233 }
00234 inline void append( T x ) {
00235 ASSERT_NON_NULL ( x );
00236 l.push_back(
new Item(x));
00237 }
00238 inline void ins_before(
void* x, T y ) {
00239 ASSERT_NON_NULL ( y );
00240 l.ins_before((
MNVPListItem*)x,
new Item(y));
00241 }
00242 inline void ins_after(
void* x, T y ) {
00243 ASSERT_NON_NULL ( y );
00244 l.ins_after((
MNVPListItem*)x,
new Item(y));
00245 }
00246 };
00247
00248
#endif
00249
00250
#endif
00251