~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to languages/cpp/parser/listnode.h

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of KDevelop
 
2
    Copyright 2002-2005 Roberto Raggi <roberto@kdevelop.org>
 
3
 
 
4
   This library is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU Library General Public
 
6
   License version 2 as published by the Free Software Foundation.
 
7
 
 
8
   This library is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
   Library General Public License for more details.
 
12
 
 
13
   You should have received a copy of the GNU Library General Public License
 
14
   along with this library; see the file COPYING.LIB.  If not, write to
 
15
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
16
   Boston, MA 02110-1301, USA.
 
17
*/
 
18
 
 
19
#ifndef LISTNODE_H
 
20
#define LISTNODE_H
 
21
 
 
22
#include "memorypool.h"
 
23
#include <kdebug.h>
 
24
 
 
25
/**
 
26
 * @brief An intrusive list of AST nodes.
 
27
 * @note ListNodes inside AST nodes are NOT guarantied to start from the beginning, call toFront.
 
28
 */
 
29
template <typename Tp>
 
30
class ListNode
 
31
{
 
32
public:
 
33
 
 
34
  Tp element;
 
35
  int index;
 
36
  mutable const ListNode<Tp> *next;
 
37
 
 
38
  static ListNode *create(const Tp &element, pool *p)
 
39
  {
 
40
    ListNode<Tp> *node = new (p->allocate(sizeof(ListNode))) ListNode();
 
41
    node->element = element;
 
42
    node->index = 0;
 
43
    node->next = node;
 
44
 
 
45
    return node;
 
46
  }
 
47
 
 
48
  static ListNode *create(const ListNode *n1, const Tp &element, pool *p)
 
49
  {
 
50
    ListNode<Tp> *n2 = ListNode::create(element, p);
 
51
 
 
52
    n2->index = n1->index + 1;
 
53
    n2->next = n1->next;
 
54
    n1->next = n2;
 
55
 
 
56
    return n2;
 
57
  }
 
58
 
 
59
  inline const ListNode<Tp> *at(int index) const
 
60
  {
 
61
    const ListNode<Tp> *node = this;
 
62
    while (index != node->index)
 
63
      node = node->next;
 
64
 
 
65
    return node;
 
66
  }
 
67
 
 
68
  inline bool hasNext() const
 
69
  { return ( next && index < next->index ); }
 
70
 
 
71
  inline int count() const
 
72
  { return 1 + toBack()->index; }
 
73
 
 
74
  inline const ListNode<Tp> *toFront() const
 
75
  { return toBack()->next; }
 
76
 
 
77
  inline const ListNode<Tp> *toBack() const
 
78
  {
 
79
    const ListNode<Tp> *node = this;
 
80
    while (node->hasNext())
 
81
      node = node->next;
 
82
 
 
83
    return node;
 
84
  }
 
85
};
 
86
 
 
87
template <class Tp>
 
88
inline const ListNode<Tp> *snoc(const ListNode<Tp> *list,
 
89
                                const Tp &element, pool *p)
 
90
{
 
91
 
 
92
  if (!list)
 
93
    return ListNode<Tp>::create(element, p);
 
94
 
 
95
  return ListNode<Tp>::create(list->toBack(), element, p);
 
96
}
 
97
 
 
98
#endif // FASTLIST_H
 
99
 
 
100