1
/* $Id: list.hpp 2394 2008-12-23 17:27:53Z bennylp $ */
3
* Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#ifndef __PJPP_LIST_HPP__
21
#define __PJPP_LIST_HPP__
24
#include <pj++/pool.hpp>
31
// List_Node must have public member next and prev. Normally
32
// it will be declared like:
36
// PJ_DECL_LIST_MEMBER(struct my_node);
41
template <class List_Node>
42
class Pj_List : public Pj_Object
46
// List const_iterator.
54
const_iterator(const List_Node *nd)
55
: node_((List_Node*)nd)
57
const List_Node * operator *()
61
const List_Node * operator -> ()
65
const_iterator operator++()
67
return const_iterator((const List_Node *)node_->next);
69
bool operator==(const const_iterator &rhs)
71
return node_ == rhs.node_;
73
bool operator!=(const const_iterator &rhs)
75
return node_ != rhs.node_;
85
class iterator : public const_iterator
90
iterator(List_Node *nd)
93
List_Node * operator *()
97
List_Node * operator -> ()
101
iterator operator++()
103
return iterator((List_Node*)node_->next);
105
bool operator==(const iterator &rhs)
107
return node_ == rhs.node_;
109
bool operator!=(const iterator &rhs)
111
return node_ != rhs.node_;
116
// Default constructor.
120
pj_list_init(&root_);
121
if (0) compiletest();
125
// You can cast Pj_List to pj_list
129
return (pj_list&)root_;
131
operator const pj_list&()
133
return (const pj_list&)root_;
137
// You can cast Pj_List to pj_list* too
141
return (pj_list*)&root_;
143
operator const pj_list*()
145
return (const pj_list*)&root_;
149
// Check if list is empty.
153
return pj_list_empty(&root_);
157
// Get first element.
161
return iterator(root_.next);
165
// Get first element.
167
const_iterator begin() const
169
return const_iterator(root_.next);
173
// Get end-of-element
175
const_iterator end() const
177
return const_iterator((List_Node*)&root_);
181
// Get end-of-element
185
return iterator((List_Node*)&root_);
191
void insert_before (iterator &pos, List_Node *node)
193
pj_list_insert_before( *pos, node );
199
void insert_after(iterator &pos, List_Node *node)
201
pj_list_insert_after(*pos, node);
207
void merge_first(List_Node *list2)
209
pj_list_merge_first(&root_, list2);
215
void merge_last(Pj_List *list)
217
pj_list_merge_last(&root_, &list->root_);
223
void insert_nodes_before(iterator &pos, Pj_List *list2)
225
pj_list_insert_nodes_before(*pos, &list2->root_);
231
void insert_nodes_after(iterator &pos, Pj_List *list2)
233
pj_list_insert_nodes_after(*pos, &list2->root_);
239
void erase(iterator &it)
245
// Get first element.
253
// Get first element.
255
const List_Node *front() const
261
// Remove first element.
265
pj_list_erase(root_.next);
279
const List_Node *back() const
285
// Remove last element.
289
pj_list_erase(root_.prev);
295
iterator find(List_Node *node)
297
List_Node *n = pj_list_find_node(&root_, node);
298
return n ? iterator(n) : end();
304
const_iterator find(List_Node *node) const
306
List_Node *n = pj_list_find_node(&root_, node);
307
return n ? const_iterator(n) : end();
311
// Insert a node in the back.
313
void push_back(List_Node *node)
315
pj_list_insert_after(root_.prev, node);
319
// Insert a node in the front.
321
void push_front(List_Node *node)
323
pj_list_insert_before(root_.next, node);
327
// Remove all elements.
338
PJ_DECL_LIST_MEMBER(List_Node);
343
// If you see error in this line,
344
// it's because List_Node is not derived from Pj_List_Node.
345
List_Node *n = (List_Node*)0;
346
n = (List_Node *)n->next; n = (List_Node *)n->prev;
351
#endif /* __PJPP_LIST_HPP__ */