blob: cd6682da2312d7589497b2cc7e50f64cfaca0e70 [file] [log] [blame]
njn641d5cc2005-05-12 04:37:27 +00001
2/*--------------------------------------------------------------------*/
3/*--- SkipList: a skiplist implementaiton. pub_tool_skiplist.h ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
10 Copyright (C) 2000-2005 Julian Seward
11 jseward@acm.org
12
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29*/
30
31#ifndef __PUB_TOOL_SKIPLIST_H
32#define __PUB_TOOL_SKIPLIST_H
33
34/*
35 The idea here is that the skiplist puts its per-element data at the
36 end of the structure. When you initialize the skiplist, you tell
37 it what structure your list elements are going to be. Then you
38 should allocate them with VG_(SkipNode_Alloc), which will allocate
39 enough memory for the extra bits.
40 */
41
42typedef struct _SkipList SkipList;
43typedef struct _SkipNode SkipNode;
44
45typedef Int (*SkipCmp_t)(const void *key1, const void *key2);
46
47struct _SkipList {
48 const Short arena; // allocation arena
49 const UShort size; // structure size (excluding SkipNode)
50 const UShort keyoff; // key offset
51 const SkipCmp_t cmp; // compare two keys
52 Char * (*strkey)(void *); // stringify a key (for debugging)
53 SkipNode *head; // list head
54};
55
njnb15785d2005-07-03 20:22:07 +000056/* Use this macro to initialize your skiplist head:
njn641d5cc2005-05-12 04:37:27 +000057 _type is the type of your element structure
58 _key is the field within that type which you want to use as the key
njnb15785d2005-07-03 20:22:07 +000059 _cmp is the comparison function for keys - it gets two typeof(_key)
60 pointers as args
61 _strkey is a function which can return a string of your key - it's only
62 used for debugging
njn641d5cc2005-05-12 04:37:27 +000063 _arena is the arena to use for allocation - -1 is the default
64 */
65#define VG_SKIPLIST_INIT(_type, _key, _cmp, _strkey, _arena) \
66 { \
67 .arena = _arena, \
68 .size = sizeof(_type), \
69 .keyoff = offsetof(_type, _key), \
70 .cmp = _cmp, \
71 .strkey = _strkey, \
72 .head = NULL, \
73 }
74
75/* List operations:
76 SkipList_Find_* search a list. The 3 variants are:
77 Before: returns a node which is <= key, or NULL if none
78 Exact: returns a node which is == key, or NULL if none
79 After: returns a node which is >= key, or NULL if none
80 SkipList_Insert inserts a new element into the list. Duplicates are
njnb15785d2005-07-03 20:22:07 +000081 forbidden. The element must have been created with SkipNode_Alloc!
njn641d5cc2005-05-12 04:37:27 +000082 SkipList_Remove removes an element from the list and returns it. It
83 doesn't free the memory.
84*/
85extern void *VG_(SkipList_Find_Before) (const SkipList *l, void *key);
86extern void *VG_(SkipList_Find_Exact) (const SkipList *l, void *key);
87extern void *VG_(SkipList_Find_After) (const SkipList *l, void *key);
88extern void VG_(SkipList_Insert) ( SkipList *l, void *data);
89extern void *VG_(SkipList_Remove) ( SkipList *l, void *key);
90
91/* Some useful standard comparisons */
92extern Int VG_(cmp_Addr) (const void *a, const void *b);
93extern Int VG_(cmp_Int) (const void *a, const void *b);
94extern Int VG_(cmp_UInt) (const void *a, const void *b);
95extern Int VG_(cmp_string)(const void *a, const void *b);
96
97/* Node (element) operations:
98 SkipNode_Alloc: allocate memory for a new element on the list. Must be
99 used before an element can be inserted! Returns NULL if not enough
100 memory.
101 SkipNode_Free: free memory allocated above
102 SkipNode_First: return the first element on the list
103 SkipNode_Next: return the next element after "data" on the list -
104 NULL for none
105
106 You can iterate through a SkipList like this:
107
njnb15785d2005-07-03 20:22:07 +0000108 for (x = VG_(SkipNode_First)(&list);
109 x != NULL;
110 x = VG_(SkipNode_Next)(&list, x)) { ... }
njn641d5cc2005-05-12 04:37:27 +0000111*/
112extern void *VG_(SkipNode_Alloc) (const SkipList *l);
113extern void VG_(SkipNode_Free) (const SkipList *l, void *p);
114extern void *VG_(SkipNode_First) (const SkipList *l);
115extern void *VG_(SkipNode_Next) (const SkipList *l, void *data);
116
117
118#endif // __PUB_TOOL_SKIPLIST_H
119
120/*--------------------------------------------------------------------*/
121/*--- end ---*/
122/*--------------------------------------------------------------------*/