2009-10-09 Chris Toshok <toshok@ximian.com>
[moon.git] / src / list.h
blobe3f84e8f6b70d18c20e1a2e45c4d8306323743fd
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 ();
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 *Insert (Node *node, int index);
56 Node *InsertAfter (Node *node, Node *after);
57 Node *InsertBefore (Node *node, Node *before);
59 Node *Replace (Node *node, int index);
61 Node *Find (NodeAction find, void *data);
62 void Remove (NodeAction find, void *data);
63 void Remove (Node *node);
64 void RemoveAt (int index);
65 void Unlink (Node *node);
67 Node *Index (int index);
69 int IndexOf (Node *node);
70 int IndexOf (NodeAction find, void *data);
72 void ForEach (NodeAction action, void *data);
75 class Queue {
76 protected:
77 pthread_mutex_t lock;
78 List *list;
80 public:
81 Queue ();
82 ~Queue ();
84 // convenience properties
85 bool IsEmpty ();
86 int Length ();
88 // convenience methods
89 void Clear (bool freeNodes);
91 void Push (List::Node *node);
92 List::Node *Pop ();
94 void Lock ();
95 void Unlock ();
97 // accessing the internal linked list directly requires manual Locking/Unlocking.
98 List *LinkedList ();
101 class ArrayList {
102 private:
103 void **array;
104 int size; // size of array
105 int count; // # of items in the array
107 public:
108 ArrayList ();
109 ~ArrayList ();
111 int GetCount ();
112 void SetCount (int value);
114 int GetCapacity ();
115 void SetCapacity (int value);
117 void EnsureCapacity (int capacity);
118 int Add (void *item);
119 void *& operator [] (int index);
122 #endif /* __LIST_H__ */