2009-12-07 Rolf Bjarne Kvinge <RKvinge@novell.com>
[moon.git] / src / list.h
blob91cf1386cfa1ec0e564ace24e0c54a8347e81fe7
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * list.h: a non-sucky linked list implementation
5 * Contact:
6 * Moonlight List (moonlight-list@lists.ximian.com)
8 * Copyright 2007 Novell, Inc. (http://www.novell.com)
10 * See the LICENSE file included with the distribution for details.
14 #ifndef __LIST_H__
15 #define __LIST_H__
17 #include <pthread.h>
20 class List {
21 public:
22 class Node {
23 public:
24 Node *next;
25 Node *prev;
27 // public
28 Node ();
29 virtual ~Node () { }
32 typedef bool (* NodeAction) (Node *node, void *data);
34 protected:
35 int length;
36 Node *head;
37 Node *tail;
39 public:
40 // constructors
41 List ();
42 virtual ~List ();
44 // properties
45 Node *First () { return head; }
46 Node *Last ();
47 bool IsEmpty ();
48 int Length ();
50 // methods
51 void Clear (bool freeNodes);
53 Node *Append (Node *node);
54 Node *Prepend (Node *node);
55 Node *Prepend (List *list);
56 Node *Insert (Node *node, int index);
57 Node *InsertAfter (Node *node, Node *after);
58 Node *InsertBefore (Node *node, Node *before);
60 Node *Replace (Node *node, int index);
62 Node *Find (NodeAction find, void *data);
63 void Remove (NodeAction find, void *data);
64 void Remove (Node *node);
65 void RemoveAt (int index);
66 void Unlink (Node *node);
68 Node *Index (int index);
70 int IndexOf (Node *node);
71 int IndexOf (NodeAction find, void *data);
73 void ForEach (NodeAction action, void *data);
76 class Queue {
77 protected:
78 pthread_mutex_t lock;
79 List *list;
81 public:
82 Queue ();
83 ~Queue ();
85 // convenience properties
86 bool IsEmpty ();
87 int Length ();
89 // convenience methods
90 void Clear (bool freeNodes);
92 void Push (List::Node *node);
93 List::Node *Pop ();
95 void Lock ();
96 void Unlock ();
98 // accessing the internal linked list directly requires manual Locking/Unlocking.
99 List *LinkedList ();
101 // copies the queue and empties the original
102 void MoveTo (Queue &queue);
105 class ArrayList {
106 private:
107 void **array;
108 int size; // size of array
109 int count; // # of items in the array
111 public:
112 ArrayList ();
113 ~ArrayList ();
115 int GetCount () { return count; }
116 void SetCount (int value);
118 int GetCapacity ();
119 void SetCapacity (int value);
121 void EnsureCapacity (int capacity);
122 int Add (void *item);
123 void *& operator [] (int index) { return array [index]; }
126 #endif /* __LIST_H__ */