summaryrefslogtreecommitdiff
path: root/src/inc/slist.h
blob: 2b81f9ba902bf6fee2060a97ada4ee78c390e799 (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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// 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.
//-----------------------------------------------------------------------------
// @File: slist.h
//

//
// @commn: Bunch of utility classes
//     
// HISTORY:
//   02/03/98:  	created helper classes
//						SLink, link node for singly linked list, every class that is intrusively
//								linked should have a data member of this type
//						SList, template linked list class, contains only inline 
//								methods for fast list operations, with proper type checking
//						
//						see below for futher info. on how to use these template classes
//
//-----------------------------------------------------------------------------

//#ifndef _H_UTIL
//#error I am a part of util.hpp Please don't include me alone !
//#endif


#ifndef _H_SLIST_
#define _H_SLIST_

//------------------------------------------------------------------
// struct SLink, to use a singly linked list 
// have a data member m_Link of type SLink in your class
// and instantiate the template SList class
//--------------------------------------------------------------------

struct SLink;
typedef DPTR(struct SLink) PTR_SLink;

struct SLink
{
    PTR_SLink m_pNext;
    SLink() 
    {
        LIMITED_METHOD_CONTRACT;

        m_pNext = NULL;
    }

    void InsertAfter(SLink* pLinkToInsert)
    {
        LIMITED_METHOD_CONTRACT;
        PRECONDITION_MSG(NULL == pLinkToInsert->m_pNext, "This method does not support inserting lists");

        PTR_SLink pTemp = m_pNext;

        m_pNext = PTR_SLink(pLinkToInsert);
        pLinkToInsert->m_pNext = pTemp;
    }

    // find pLink within the list starting at pHead
    // if found remove the link from the list and return the link
    // otherwise return NULL
    static SLink* FindAndRemove(SLink *pHead, SLink* pLink, SLink ** ppPrior)
    {
        LIMITED_METHOD_CONTRACT;

	    _ASSERTE(pHead != NULL);
	    _ASSERTE(pLink != NULL);

	    SLink* pFreeLink = NULL;
        *ppPrior = NULL;

	    while (pHead->m_pNext != NULL)
	    {
		    if (pHead->m_pNext == pLink)
		    {
			    pFreeLink = pLink;
			    pHead->m_pNext = pLink->m_pNext;
                *ppPrior = pHead;
                break;
		    }
            pHead = pHead->m_pNext;
	    }
    	
	    return pFreeLink;
    }
};

//------------------------------------------------------------------
// class SList. Intrusive singly linked list.
//
// To use SList with the default instantiation, your class should
// define a data member of type SLink and named 'm_Link'. To use a
// different field name, you need to provide an explicit LinkPtr
// template argument. For example:
//   'SList<MyClass, false, MyClass*, &MyClass::m_FieldName>'
//
// SList has two different behaviours depending on boolean
// fHead variable, 
//
// if fHead is true, then the list allows only InsertHead  operations
// if fHead is false, then the list allows only InsertTail operations
// the code is optimized to perform these operations
// all methods are inline, and conditional compiled based on template
// argument 'fHead'
// so there is no actual code size increase
//--------------------------------------------------------------
template <class T, bool fHead = false, typename __PTR = T*, SLink T::*LinkPtr = &T::m_Link>
class SList
{
public:
    // typedef used by the Queue class below
    typedef T ENTRY_TYPE; 

protected:

    // used as sentinel 
    SLink  m_link; // slink.m_pNext == Null
    PTR_SLink m_pHead;
    PTR_SLink m_pTail;

    // get the list node within the object
    static SLink* GetLink (T* pLink)
    {
        LIMITED_METHOD_DAC_CONTRACT;
        return &(pLink->*LinkPtr);
    }
    
    // move to the beginning of the object given the pointer within the object
    static T* GetObject (SLink* pLink)
    {
        LIMITED_METHOD_DAC_CONTRACT;
        if (pLink == NULL)
        {
            return NULL;
        }
        else
        {
#ifdef __GNUC__
            // GCC defines offsetof to be __builtin_offsetof, which doesn't use the
            // old-school memory model trick to determine offset.
            const UINT_PTR offset = (((UINT_PTR)&(((T *)0x1000)->*LinkPtr))-0x1000);
            return (T*)__PTR(dac_cast<TADDR>(pLink) - offset);
#else
            return (T*)__PTR(dac_cast<TADDR>(pLink) - offsetof(T, *LinkPtr));         
#endif // __GNUC__
        }
    }

public:

    SList()
    {
        WRAPPER_NO_CONTRACT;
#ifndef DACCESS_COMPILE
        Init();
#endif // !defined(DACCESS_COMPILE)
    }

    void Init()
    {
        LIMITED_METHOD_CONTRACT;
        m_pHead = &m_link;
        // NOTE :: fHead variable is template argument 
        // the following code is a compiled in, only if the fHead flag
        // is set to false,
        if (!fHead)
        {
            m_pTail = &m_link;
        }
    }

    bool IsEmpty()
    {
        LIMITED_METHOD_CONTRACT;
        return m_pHead->m_pNext == NULL;
    }

#ifndef DACCESS_COMPILE

    void InsertTail(T *pObj)
    {
        LIMITED_METHOD_CONTRACT;
        // NOTE : conditional compilation on fHead template variable
        if (!fHead)
        {
            _ASSERTE(pObj != NULL);
            SLink *pLink = GetLink(pObj);

            m_pTail->m_pNext = pLink;
            m_pTail = pLink;
        }
        else 
        {// you instantiated this class asking only for InsertHead operations
            _ASSERTE(0);
        }
    }
	
    void InsertHead(T *pObj)
    {
        LIMITED_METHOD_CONTRACT;
        // NOTE : conditional compilation on fHead template variable
        if (fHead)
        {
            _ASSERTE(pObj != NULL);
            SLink *pLink = GetLink(pObj);
            
            pLink->m_pNext = m_pHead->m_pNext;
            m_pHead->m_pNext = pLink;
        }
        else
        {// you instantiated this class asking only for InsertTail operations
            _ASSERTE(0);
        }
    }

    T*	RemoveHead()
    {
        LIMITED_METHOD_CONTRACT;
        SLink* pLink = m_pHead->m_pNext;
        if (pLink != NULL)
        {
            m_pHead->m_pNext = pLink->m_pNext;
        }
        // conditionally compiled, if the instantiated class
        // uses Insert Tail operations
        if (!fHead)
        {
            if(m_pTail == pLink)
            {
                m_pTail = m_pHead;
            }
        }

        return GetObject(pLink);
    }
    
#endif // !DACCESS_COMPILE

    T*	GetHead()
    {
        WRAPPER_NO_CONTRACT;
        return GetObject(m_pHead->m_pNext);
    }
    
    T*	GetTail()
    {
        WRAPPER_NO_CONTRACT;

        // conditional compile
        if (fHead)
        {	// you instantiated this class asking only for InsertHead operations
            // you need to walk the list yourself to find the tail
            _ASSERTE(0);
        }
        return (m_pHead != m_pTail) ? GetObject(m_pTail) : NULL;
    }

    static T *GetNext(T *pObj)
    {
        WRAPPER_NO_CONTRACT;

        _ASSERTE(pObj != NULL);
        return GetObject(GetLink(pObj)->m_pNext);
    }

    T* FindAndRemove(T *pObj)
    {
        WRAPPER_NO_CONTRACT;

        _ASSERTE(pObj != NULL);
                
        SLink   *prior;
        SLink   *ret = SLink::FindAndRemove(m_pHead, GetLink(pObj), &prior);
        
        if (ret == m_pTail)
            m_pTail = prior;
        
        return GetObject(ret);
    }

    class Iterator
    {
        friend class SList;

    public:
        Iterator & operator++()
        { _ASSERTE(m_cur != NULL); m_cur = SList::GetNext(m_cur); return *this; }

        Iterator operator++(int)
        { Iterator it(m_cur); ++(*this); return it; }

        bool operator==(Iterator const & other) const
        {
            return m_cur == other.m_cur ||
                   (m_cur != NULL && other.m_cur != NULL && *m_cur == *other.m_cur);
        }

        bool operator!=(Iterator const & other) const
        { return !(*this == other); }

        T & operator*()
        { _ASSERTE(m_cur != NULL); return *m_cur; }
        
        T * operator->() const
        { return m_cur; }
        
    private:
        Iterator(SList * pList)
            : m_cur(pList->GetHead())
        { }

        Iterator(T* pObj)
            : m_cur(pObj)
        { }

        Iterator()
            : m_cur(NULL)
        { }

        T* m_cur;
    };

    Iterator begin()
    { return Iterator(GetHead()); }

    Iterator end()
    { return Iterator(); }
};

template <typename ElemT>
struct SListElem
{
    SLink m_Link;
    ElemT m_Value;

    operator ElemT const &() const
    { return m_Value; }

    operator ElemT &()
    { return m_Value; }

    ElemT const & operator*() const
    { return m_Value; }

    ElemT & operator*()
    { return m_Value; }

    ElemT const & GetValue() const
    { return m_Value; }

    ElemT & GetValue()
    { return m_Value; }

    SListElem()
        : m_Link()
        , m_Value()
    { }

    template <typename T1>
    SListElem(T1&& val)
        : m_Link()
        , m_Value(std::forward<T1>(val))
    { }

    template <typename T1, typename T2>
    SListElem(T1&& val1, T2&& val2)
        : m_Link()
        , m_Value(std::forward<T1>(val1), std::forward<T2>(val2))
    { }

    template <typename T1, typename T2, typename T3>
    SListElem(T1&& val1, T2&& val2, T3&& val3)
        : m_Link()
        , m_Value(std::forward<T1>(val1), std::forward<T2>(val2), std::forward<T3>(val3))
    { }

    template <typename T1, typename T2, typename T3, typename T4>
    SListElem(T1&& val1, T2&& val2, T3&& val3, T4&& val4)
        : m_Link()
        , m_Value(std::forward<T1>(val1), std::forward<T2>(val2), std::forward<T3>(val3), std::forward<T4>(val4))
    { }
};

#endif // _H_SLIST_

// End of file: list.h