blob: 9f1882082bd61c514811a9bf98cf0d1c0a79f5cf [file] [log] [blame]
njne1b2b962005-08-14 22:13:00 +00001
2/*--------------------------------------------------------------------*/
3/*--- OSet: a fast data structure with no dups. pub_tool_oset.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_OSET_H
32#define __PUB_TOOL_OSET_H
33
34// This module implements an ordered set, a data structure with fast
35// (eg. amortised log(n) or better) insertion, lookup and deletion of
36// elements. It does not allow duplicates, and will assert if you insert a
37// duplicate to an OSet.
38//
39// The structure is totally generic. The user provides the allocation and
40// deallocation functions. Also, each element has a key, which the lookup
41// is done with. The key may be the whole element (eg. in an OSet of
42// integers, each integer serves both as an element and a key), or it may be
43// only part of it (eg. if the key is a single field in a struct). The user
44// can provide a function that compares an element with a key; this is very
45// flexible, and with the right comparison function even a (non-overlapping)
46// interval list can be created. But the cost of calling a function for
47// every comparison can be high during lookup. If no comparison function is
48// provided, we assume that keys are (signed or unsigned) words, and that
49// the key is the first word in each element. This fast comparison is
50// suitable for an OSet of Ints, or an OSet containing structs where the
51// first element is an Addr, for example.
52//
53// Each OSet also has an iterator, which makes it simple to traverse all the
54// nodes in order. Note that the iterator maintains state and so is
55// non-reentrant.
56//
57// Note that once you insert an element into an OSet, if you modify any part
58// of it looked at by your cmp() function, this may cause incorrect
59// behaviour as the sorted order maintained will be wrong.
60
61/*--------------------------------------------------------------------*/
62/*--- Types ---*/
63/*--------------------------------------------------------------------*/
64
65typedef struct _OSet OSet;
66typedef struct _OSetNode OSetNode;
67
68typedef Int (*OSetCmp_t) ( void* key, void* elem );
69typedef void* (*OSetAlloc_t) ( SizeT szB );
70typedef void (*OSetFree_t) ( void* p );
71
72/*--------------------------------------------------------------------*/
73/*--- Creating and destroying OSets and OSet members ---*/
74/*--------------------------------------------------------------------*/
75
njna103e142005-11-18 21:32:18 +000076// * Create: allocates and initialises the OSet. Arguments:
njne1b2b962005-08-14 22:13:00 +000077// - keyOff The offset of the key within the element.
njne1b2b962005-08-14 22:13:00 +000078// - cmp The comparison function between keys and elements, or NULL
79// if the OSet should use fast comparisons.
80// - alloc The allocation function used for allocating the OSet itself;
81// it's also called for each invocation of VG_(OSet_AllocNode)().
82// - free The deallocation function used by VG_(OSet_FreeNode)() and
83// VG_(OSet_Destroy)().
84//
85// If cmp is NULL, keyOff must be zero. This is checked.
86//
87// * Destroy: frees all nodes in the table, plus the memory used by
88// the table itself.
89//
90// * AllocNode: Allocate and zero memory for a node to go into the OSet.
91// Uses the alloc function given to VG_(OSet_Create)() to allocated a node
92// which is big enough for both an element and the OSet metadata.
93// Not all elements in one OSet have to be the same size.
94//
95// Note that the element allocated will be at most word-aligned, which may
96// be less aligned than the element type would normally be.
97//
98// * FreeNode: Deallocate a node allocated with OSet_AllocNode(). Using
99// a deallocation function (such as VG_(free)()) directly will likely
100// lead to assertions in Valgrind's allocator.
101
102extern OSet* VG_(OSet_Create) ( OffT keyOff, OSetCmp_t cmp,
103 OSetAlloc_t alloc, OSetFree_t free );
104extern void VG_(OSet_Destroy) ( OSet* os );
105extern void* VG_(OSet_AllocNode) ( OSet* os, SizeT elemSize );
106extern void VG_(OSet_FreeNode) ( OSet* os, void* elem );
107
108/*--------------------------------------------------------------------*/
109/*--- Operations on OSets ---*/
110/*--------------------------------------------------------------------*/
111
njnc438e082005-10-15 17:50:02 +0000112// In everything that follows, the parameter 'key' is always the *address*
113// of the key, and 'elem' is *address* of the elem, as are the return values
114// of the functions that return elems.
115//
njne1b2b962005-08-14 22:13:00 +0000116// * Size: The number of elements in the set.
117//
118// * Contains: Determines if any element in the OSet matches the key.
119//
120// * Lookup: Returns a pointer to the element matching the key, if there is
121// one, otherwise returns NULL.
122//
njnaa260e82005-08-17 21:06:07 +0000123// * LookupWithCmp: Like Lookup, but you specify the comparison function,
124// which overrides the OSet's normal one.
125//
njne1b2b962005-08-14 22:13:00 +0000126// * Insert: Inserts a new element into the list. Note that 'elem' must
127// have been allocated using VG_(OSet_AllocNode)(), otherwise you will get
128// assertion failures about "bad magic". Duplicates are forbidden, and
129// will also cause assertion failures.
130//
131// * Remove: Removes the element matching the key, if there is one. Returns
132// NULL if no element matches the key.
133//
134// * ResetIter: Each OSet has an iterator. This resets it to point to the
135// first element in the OSet.
136//
137// * Next: Returns a pointer to the element pointed to by the OSet's
138// iterator, and advances the iterator by one; the elements are visited
139// in order. Or, returns NULL if the iterator has reached the OSet's end.
140//
141// You can thus iterate in order through an OSet like this:
142//
143// VG_(OSet_ResetIter)(oset);
144// while ( (elem = VG_(OSet_Next)(oset)) ) {
145// ... do stuff with 'elem' ...
146// }
147//
148// Note that iterators are cleared any time an element is inserted or
149// removed from the OSet, to avoid possible mayhem caused by the iterator
150// getting out of sync with the OSet's contents. "Cleared" means that
151// they will return NULL if VG_(OSet_Next)() is called without an
152// intervening call to VG_(OSet_ResetIter)().
153
njnaa260e82005-08-17 21:06:07 +0000154extern Int VG_(OSet_Size) ( OSet* os );
155extern void VG_(OSet_Insert) ( OSet* os, void* elem );
156extern Bool VG_(OSet_Contains) ( OSet* os, void* key );
157extern void* VG_(OSet_Lookup) ( OSet* os, void* key );
158extern void* VG_(OSet_LookupWithCmp)( OSet* os, void* key, OSetCmp_t cmp );
159extern void* VG_(OSet_Remove) ( OSet* os, void* key );
160extern void VG_(OSet_ResetIter) ( OSet* os );
161extern void* VG_(OSet_Next) ( OSet* os );
njne1b2b962005-08-14 22:13:00 +0000162
163#endif // __PUB_TOOL_OSET_H
164
165/*--------------------------------------------------------------------*/
166/*--- end ---*/
167/*--------------------------------------------------------------------*/