summaryrefslogtreecommitdiff
path: root/src/binder/inc/list.hpp
blob: 91ecd1752b5b60889726a65882ccd98a8d53bf49 (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
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
350
351
352
353
354
355
356
357
358
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// ============================================================
//
// List.hpp
//


//
// Defines the List class
//
// ============================================================

#ifndef __BINDER__LIST_HPP__
#define __BINDER__LIST_HPP__

#include "bindertypes.hpp"
#include "ex.h"

namespace BINDER_SPACE
{
    // Ripped from Fusion

    //
    // ListNode
    //

    typedef void *LISTNODE;

    template <class Type> class ListNode
    {
    public:
        ListNode(Type item);
        virtual ~ListNode();

        void SetNext(ListNode *pNode);
        void SetPrev(ListNode *pNode);
        Type GetItem();
        ListNode *GetNext();
        ListNode *GetPrev();

    private:
        Type       _type;
        ListNode  *_pNext;
        ListNode  *_pPrev;
    };

    //
    // List
    //

    template <class Type> class List
    {
    public:
        List();
        ~List();

        LISTNODE AddHead(const Type &item);
        LISTNODE AddTail(const Type &item);

        LISTNODE GetHeadPosition();
        LISTNODE GetTailPosition();
        void RemoveAt(LISTNODE pNode);
        void RemoveAll();
        LISTNODE Find(const Type &item);
        int GetCount();
        Type GetNext(LISTNODE &pNode);
        Type GetAt(LISTNODE pNode);
        LISTNODE AddSorted(const Type &item, LPVOID pfn);

    public:
        DWORD _dwSig;

    private:
        ListNode<Type> *_pHead;
        ListNode<Type> *_pTail;
        int             _iCount;
    };

    //
    // ListNode Implementation
    //

    template <class Type> ListNode<Type>::ListNode(Type item)
        : _type(item)
        , _pNext(NULL)
        , _pPrev(NULL)
    {
    }

    template <class Type> ListNode<Type>::~ListNode()
    {
    }

    template <class Type> void ListNode<Type>::SetNext(ListNode *pNode)
    {
        _pNext = pNode;
    }

    template <class Type> void ListNode<Type>::SetPrev(ListNode *pNode)
    {
        _pPrev = pNode;
    }

    template <class Type> Type ListNode<Type>::GetItem()
    {
        return _type;
    }

    template <class Type> ListNode<Type> *ListNode<Type>::GetNext()
    {
        return _pNext;
    }

    template <class Type> ListNode<Type> *ListNode<Type>::GetPrev()
    {
        return _pPrev;
    }


    //
    // List Implementation
    //

    template <class Type> List<Type>::List()
        : _pHead(NULL)
        , _pTail(NULL)
        , _iCount(0)
    {
        _dwSig = 0x5453494c; /* 'TSIL' */
    }

    template <class Type> List<Type>::~List()
    {
        HRESULT hr = S_OK;

        EX_TRY
        {
            RemoveAll();
        }
        EX_CATCH_HRESULT(hr);
    }

    template <class Type> LISTNODE List<Type>::AddHead(const Type &item)
    {
        ListNode<Type>                   *pNode = NULL;

        NEW_CONSTR(pNode, ListNode<Type>(item));
        if (pNode) {
            _iCount++;
            pNode->SetNext(_pHead);
            pNode->SetPrev(NULL);
            if (_pHead == NULL) {
                _pTail = pNode;
            }
            else {
                _pHead->SetPrev(pNode);
            }
            _pHead = pNode;
        }
        
        return (LISTNODE)pNode;
    }

    template <class Type> LISTNODE List<Type>::AddSorted(const Type &item, 
                                                         LPVOID pfn)
    {
        ListNode<Type>           *pNode = NULL;
        LISTNODE           pCurrNode = NULL;
        LISTNODE           pPrevNode = NULL;
        int                      i;
        Type                     curItem;
        
        LONG (*pFN) (const Type item1, const Type item2);
        
        pFN = (LONG (*) (const Type item1, const Type item2))pfn;
        
        if(_pHead == NULL) {
            return AddHead(item);
        }
        else {
            pCurrNode = GetHeadPosition();
            curItem = ((ListNode<Type> *) pCurrNode)->GetItem();
            for (i = 0; i < _iCount; i++) {
                if (pFN(item, curItem) < 1) {
                    NEW_CONSTR(pNode, ListNode<Type>(item));
                    if (pNode) {
                        pNode->SetPrev((ListNode<Type> *)pPrevNode);
                        pNode->SetNext((ListNode<Type> *)pCurrNode);
                        // update pPrevNode
                        if(pPrevNode) {
                            ((ListNode<Type> *)pPrevNode)->SetNext(pNode);
                        }
                        else {
                            _pHead = pNode;
                        }
                        // update pCurrNode
                        ((ListNode<Type> *)pCurrNode)->SetPrev(pNode);
                        _iCount++;
                    }
                    break;
                }
                pPrevNode = pCurrNode;
                GetNext(pCurrNode);
                if(i+1 == _iCount) {
                    return AddTail(item);
                }
                else {
                    _ASSERTE(pCurrNode);
                    curItem = GetAt(pCurrNode);
                }
            }
        }
        
        return (LISTNODE)pNode;
    }

    template <class Type> LISTNODE List<Type>::AddTail(const Type &item)
    {
        ListNode<Type>                   *pNode = NULL;
    
        NEW_CONSTR(pNode, ListNode<Type>(item));
        if (pNode) {
            _iCount++;
            if (_pTail) {
                pNode->SetPrev(_pTail);
                _pTail->SetNext(pNode);
                _pTail = pNode;
            }
            else {
                _pHead = _pTail = pNode;
            }
        }

        return (LISTNODE)pNode;
    }

    template <class Type> int List<Type>::GetCount()
    {
        return _iCount;
    }

    template <class Type> LISTNODE List<Type>::GetHeadPosition()
    {
        return (LISTNODE)_pHead;
    }

    template <class Type> LISTNODE List<Type>::GetTailPosition()
    {
        return (LISTNODE)_pTail;
    }

    template <class Type> Type List<Type>::GetNext(LISTNODE &pNode)
    {
        ListNode<Type> *pListNode = (ListNode<Type> *)pNode;

        // Faults if you pass NULL
        _ASSERTE(pNode);

        Type item = pListNode->GetItem();
        pNode = (LISTNODE)(pListNode->GetNext());

        return item;
    }

    template <class Type> void List<Type>::RemoveAll()
    {
        int                        i;
        LISTNODE                   listNode = NULL;
        ListNode<Type>            *pDelNode = NULL;

        listNode = GetHeadPosition();

        for (i = 0; i < _iCount; i++) {
            pDelNode = (ListNode<Type> *)listNode;
            GetNext(listNode);
            SAFE_DELETE(pDelNode);
        }
    
        _iCount = 0;
        _pHead = NULL;
        _pTail = NULL;
    }

    template <class Type> void List<Type>::RemoveAt(LISTNODE pNode)
    {
        ListNode<Type>           *pListNode = (ListNode<Type> *)pNode;
        ListNode<Type>           *pPrevNode = NULL;
        ListNode<Type>           *pNextNode = NULL;

        if (pNode) {
            pPrevNode = pListNode->GetPrev();
            pNextNode = pListNode->GetNext();

            if (pPrevNode) {
                pPrevNode->SetNext(pNextNode);
                if (pNextNode) {
                    pNextNode->SetPrev(pPrevNode);
                }
                else {
                    // We're removing the last node, so we have a new tail
                    _pTail = pPrevNode;
                }
                SAFE_DELETE(pListNode);
            }
            else {
                // No previous, so we are the head of the list
                _pHead = pNextNode;
                if (pNextNode) {
                    pNextNode->SetPrev(NULL);
                }
                else {
                    // No previous, or next. There was only one node.
                    _pHead = NULL;
                    _pTail = NULL;
                }
                SAFE_DELETE(pListNode);
            }

            _iCount--;
        }
    }
        

    template <class Type> LISTNODE List<Type>::Find(const Type &item)
    {
        int                      i;
        Type                     curItem;
        LISTNODE                 pNode = NULL;
        LISTNODE                 pMatchNode = NULL;
        ListNode<Type> *         pListNode = NULL;

        pNode = GetHeadPosition();
        for (i = 0; i < _iCount; i++) {
            pListNode = (ListNode<Type> *)pNode;
            curItem = GetNext(pNode);
            if (curItem == item) {
                pMatchNode = (LISTNODE)pListNode;
                break;
            }
        }

        return pMatchNode;
    }

    template <class Type> Type List<Type>::GetAt(LISTNODE pNode)
    {
        ListNode<Type>                *pListNode = (ListNode<Type> *)pNode;

        // Faults if you pass NULL
        _ASSERTE(pNode);

        return pListNode->GetItem();
    }
};

#endif