File indexing completed on 2022-09-20 14:06:40

0001 /*
0002   This file is part of kdev-pg
0003   Copyright 2005, 2006 Roberto Raggi <roberto@kdevelop.org>
0004 
0005   Permission to use, copy, modify, distribute, and sell this software and its
0006   documentation for any purpose is hereby granted without fee, provided that
0007   the above copyright notice appear in all copies and that both that
0008   copyright notice and this permission notice appear in supporting
0009   documentation.
0010 
0011   The above copyright notice and this permission notice shall be included in
0012   all copies or substantial portions of the Software.
0013 
0014   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
0017   KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
0018   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0019   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0020 */
0021 
0022 //krazy:excludeall=inline
0023 #ifndef KDEV_PG_LIST_H
0024 #define KDEV_PG_LIST_H
0025 
0026 #include "kdev-pg-memory-pool.h"
0027 
0028 namespace KDevPG
0029 {
0030 
0031 template <typename _Tp>
0032 struct ListNode
0033 {
0034   _Tp element;
0035   int index;
0036   mutable const ListNode<_Tp> *next;
0037 
0038   static ListNode *create(const _Tp &element, MemoryPool *p)
0039   {
0040     ListNode<_Tp> *node = new (p->allocate(sizeof(ListNode))) ListNode();
0041     node->element = element;
0042     node->index = 0;
0043     node->next = node;
0044 
0045     return node;
0046   }
0047 
0048   static ListNode *create(const ListNode *n1, const _Tp &element, MemoryPool *p)
0049   {
0050     ListNode<_Tp> *n2 = ListNode::create(element, p);
0051 
0052     n2->index = n1->index + 1;
0053     n2->next = n1->next;
0054     n1->next = n2;
0055 
0056     return n2;
0057   }
0058 
0059   inline const ListNode<_Tp> *at(int index) const
0060   {
0061     const ListNode<_Tp> *node = this;
0062     while (index != node->index)
0063       node = node->next;
0064 
0065     return node;
0066   }
0067 
0068   inline bool hasNext() const
0069   { return index < next->index; }
0070 
0071   inline int count() const
0072   { return 1 + back()->index; }
0073 
0074   inline const ListNode<_Tp> *front() const
0075   { return back()->next; }
0076 
0077   inline const ListNode<_Tp> *back() const
0078   {
0079     const ListNode<_Tp> *node = this;
0080     while (node->hasNext())
0081       node = node->next;
0082 
0083     return node;
0084   }
0085 };
0086 
0087 template <class _Tp>
0088 inline const ListNode<_Tp> *snoc(const ListNode<_Tp> *list,
0089                                   const _Tp &element, MemoryPool *p)
0090 {
0091   if (!list)
0092     return ListNode<_Tp>::create(element, p);
0093 
0094   return ListNode<_Tp>::create(list->back(), element, p);
0095 }
0096 
0097 }
0098 
0099 #endif // KDEV_PG_LIST_H
0100 
0101