summaryrefslogtreecommitdiffstats
path: root/public/sdk/inc/dlink.hxx
blob: f239d990c865e9d4a9842226bd6067a1fdbcdada (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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#if !defined __DLINK_HXX__
#define __DLINK_HXX__
//+---------------------------------------------------------------------------
//
//  File:   DLINK.HXX
//
//  Contents:   Parametrized doubly linked list and iterators
//
//  History:    15-Jun-92  BartoszM Created.
//
//----------------------------------------------------------------------------

//+---------------------------------------------------------------------------
//
//  Class:      CDoubleLink
//
//  Purpose:    Linked element 
//
//  History:    15-Jun-92   BartoszM    Created.
//
//  Notes:      Use as base for your class. No need to override anything.
//
//              class CFoo: public CDoubleLink
//              {
//                  // your data and code goes here
//              };
//
//----------------------------------------------------------------------------

class CDoubleLink
{
public:
    
    CDoubleLink* Next() { return _next; }

    CDoubleLink* Prev() { return _prev; }

    void Close() { _next = this; _prev = this; }

    BOOL IsSingle() const { return _next == this; }
    
    void Unlink()
    {
        _next->_prev = _prev;
        _prev->_next = _next;
    }

    void InsertBefore ( CDoubleLink* pAfter )
    {
        CDoubleLink* pBefore = pAfter->_prev;
        _next = pAfter;
        _prev = pBefore;
        pAfter->_prev  = this;
        pBefore->_next = this;
    }
    
    void InsertAfter ( CDoubleLink* pBefore )
    {
        CDoubleLink* pAfter = pBefore->_next;
        _next = pAfter;
        _prev = pBefore;
        pAfter->_prev  = this;
        pBefore->_next = this;
    }
    
protected:

    CDoubleLink* _next;
    CDoubleLink* _prev;
};

//+---------------------------------------------------------------------------
//
//  Class:      CDoubleList
//
//  Purpose:    Linked list of indexes
//
//  History:    15-Jun-92   BartoszM    Created.
//              15-Dec-94   SuChang     Added _End() routine
//
//  Notes:      Use as base for your own class.
//              Add methods as needed.
//              To implement searches use forward and backward
//              iterators described below. For instance, if you implement
//              a SORTED list:
//
//  Foo* CFooList::Insert ( CFoo* pFoo )
//  {
//      for ( CBackFooIter it(*this); !AtEnd(it); BackUp(it) )
//      {
//          if ( it->Size() <= pFoo->Size() ) // overloaded operator ->
//          {
//              pFoo->InsertAfter(it.GetFoo());
//              return;
//          }
//      }
//      // end of list
//      Push(pFoo);
//  }
//
//----------------------------------------------------------------------------

class CDoubleList
{
    friend class CForwardIter;
    friend class CBackwardIter;
    friend class CDoubleIter;
    
public:

    class CDoubleIter
    {
        friend class CDoubleList;
    protected:
        CDoubleIter ( CDoubleLink* pLink ) : _pLinkCur(pLink) {}
        CDoubleLink*  _pLinkCur;
    };


    CDoubleList()
    {
        _root.Close();
    }

    BOOL         IsEmpty() const { return _root.IsSingle(); }

    void         Advance ( CDoubleIter& it );
    
    void         BackUp  ( CDoubleIter& it );

    BOOL         AtEnd ( CDoubleIter& it);
    
    // In derived class you can add your own Pop(), Top(), etc.
    // that will cast the results of  _Pop(), _Top(), etc...

protected:

    CDoubleLink* _Top() { return IsEmpty()? 0: _root.Next(); }
    CDoubleLink* _End() { return IsEmpty()? 0: _root.Prev(); }

    BOOL         _IsRoot ( CDoubleLink* pLink ) const
                 { return pLink == &_root; }
    
    void         _Push ( CDoubleLink* pLink );
    
    void         _Queue ( CDoubleLink* pLink );
    
    CDoubleLink* _Pop ( void );

    CDoubleLink  _root;
};

//+---------------------------------------------------------------------------
//
//  Class:      CDoubleIter
//
//  Purpose:    Linked list iterator
//
//  History:    17-Jun-92   BartoszM    Created.
//
//  Notes:      Auxiliary class. See iterators below.
//
//----------------------------------------------------------------------------

//+---------------------------------------------------------------------------
//
//  Class:      CForwardIter
//
//  Purpose:    Linked list iterator
//
//  History:    17-Jun-92   BartoszM    Created.
//
//  Notes:      Use as base for your own forward iterator.
//              Notice the overloading of operator ->
//              -------------------------------------
//              It lets you use iterator like a pointer to Foo.
//
//  class CForFooIter : public CForwardIter
//  {
//  public:
//        
//      CForFooIter ( CFooList& list ) : CForwardIter(list) {}
//    
//      CFoo* operator->() { return (CFoo*) _pLinkCur; }
//      CFoo* GetFoo() { return (CFoo*) _pLinkCur; }
//  };
//
//  Example of usage:
//  ----------------
//
//  for ( CForFooIter it(fooList); !fooList.AtEnd(it); fooList.Advance(it))
//  {
//      it->FooMethod();  // operator ->
//  }
//
//----------------------------------------------------------------------------

class CForwardIter: public CDoubleList::CDoubleIter
{
public:
    CForwardIter ( CDoubleList& list ): CDoubleIter(list._root.Next()) {}
};

//+---------------------------------------------------------------------------
//
//  Class:      CBackwardIter
//
//  Purpose:    Linked list iterator
//
//  History:    17-Jun-92   BartoszM    Created.
//
//  Notes:      See above.
//
//----------------------------------------------------------------------------

class CBackwardIter: public CDoubleList::CDoubleIter
{
public:
    CBackwardIter ( CDoubleList& list ): CDoubleIter(list._root.Prev()) {}
};



//+---------------------------------------------------------------------------
//
//  Member:     CDoubleList::AtEnd, public
//
//  Arguments:  [it] -- iterator
//
//  Returns:    TRUE if iterator at end of list
//
//  History:    17-Jun-92   BartoszM       Created.
//
//  Notes:      Works for both iterators (forward and backward)
//
//----------------------------------------------------------------------------

inline BOOL CDoubleList::AtEnd ( CDoubleIter& it)
{
    return _IsRoot( it._pLinkCur );
}
    
//+---------------------------------------------------------------------------
//
//  Member:     CDoubleList::Advance, public
//
//  Synopsis:   Advances an iterator
//
//  Arguments:  [it] -- iterator
//
//  History:    17-Jun-92   BartoszM       Created.
//
//----------------------------------------------------------------------------

inline void CDoubleList::Advance ( CDoubleIter& it )
{
    Win4Assert ( !_IsRoot(it._pLinkCur) );
    it._pLinkCur = it._pLinkCur->Next();
}

//+---------------------------------------------------------------------------
//
//  Member:     CDoubleList::BackUp, public
//
//  Synopsis:   Backs up an iterator
//
//  Arguments:  [it] -- iterator
//
//  History:    17-Jun-92   BartoszM       Created.
//
//----------------------------------------------------------------------------

inline void CDoubleList::BackUp ( CDoubleIter& it )
{
    Win4Assert ( !_IsRoot(it._pLinkCur) );
    it._pLinkCur = it._pLinkCur->Prev();
}

//+---------------------------------------------------------------------------
//
//  Member:     CDoubleList::Queue, private
//
//  Arguments:  [pLink] -- link to be queued
//
//  History:    17-Mar-93   WadeR       Created. (based on Push, below)
//
//  Notest:     Override, accept only derived class (type safety!), e.g.
//              
//              void CFooList::Queue ( CFoo* pFoo )
//              {
//                  _Queue ( pFoo );
//              }
//
//----------------------------------------------------------------------------

inline void CDoubleList::_Queue ( CDoubleLink* pLink )
{
    pLink->InsertBefore ( &_root );
}

//+---------------------------------------------------------------------------
//
//  Member:     CDoubleList::Push, private
//
//  Arguments:  [pLink] -- link to be pushed
//
//  History:    17-Jun-92   BartoszM       Created.
//
//  Notest:     Override, accept only derived class (type safety!), e.g.
//              
//              void CFooList::Push ( CFoo* pFoo )
//              {
//                  _Push ( pFoo );
//              }
//
//----------------------------------------------------------------------------

inline void CDoubleList::_Push ( CDoubleLink* pLink )
{
    pLink->InsertAfter ( &_root );
}

//+---------------------------------------------------------------------------
//
//  Member:     CDoubleList::_Pop, private
//
//  History:    17-Jun-92   BartoszM       Created.
//
//  Notes:      Override: cast the result
//
//              CFoo* CFooList::Pop()
//              {
//                  return (CFoo*) _Pop();
//              }
//
//----------------------------------------------------------------------------

inline CDoubleLink* CDoubleList::_Pop ( void )
{
    CDoubleLink* pLink = 0;
    if ( !IsEmpty() )
    {
        pLink = _root.Next();
        pLink->Unlink();
    }
    return pLink;
}
    
#endif