blob: ef9abfb7e059c57e76bad49b54166567d552c89c [file] [log] [blame]
sewardjb4112022007-11-09 22:49:28 +00001
2/*--------------------------------------------------------------------*/
3/*--- An AVL tree based finite map for word keys and word values. ---*/
4/*--- Inspired by Haskell's "FiniteMap" library. ---*/
sewardj896f6f92008-08-19 08:38:52 +00005/*--- pub_tool_wordfm.h ---*/
sewardjb4112022007-11-09 22:49:28 +00006/*--------------------------------------------------------------------*/
7
8/*
sewardj896f6f92008-08-19 08:38:52 +00009 This file is part of Valgrind, a dynamic binary instrumentation
10 framework.
sewardjb4112022007-11-09 22:49:28 +000011
sewardj4d474d02008-02-11 11:34:59 +000012 Copyright (C) 2007-2008 Julian Seward
sewardjb4112022007-11-09 22:49:28 +000013 jseward@acm.org
14
15 This code is based on previous work by Nicholas Nethercote
16 (coregrind/m_oset.c) which is
17
sewardj4d474d02008-02-11 11:34:59 +000018 Copyright (C) 2005-2008 Nicholas Nethercote
sewardjb4112022007-11-09 22:49:28 +000019 njn@valgrind.org
20
21 which in turn was derived partially from:
22
23 AVL C library
24 Copyright (C) 2000,2002 Daniel Nagy
25
26 This program is free software; you can redistribute it and/or
27 modify it under the terms of the GNU General Public License as
28 published by the Free Software Foundation; either version 2 of
29 the License, or (at your option) any later version.
30 [...]
31
32 (taken from libavl-0.4/debian/copyright)
33
34 This program is free software; you can redistribute it and/or
35 modify it under the terms of the GNU General Public License as
36 published by the Free Software Foundation; either version 2 of the
37 License, or (at your option) any later version.
38
39 This program is distributed in the hope that it will be useful, but
40 WITHOUT ANY WARRANTY; without even the implied warranty of
41 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
42 General Public License for more details.
43
44 You should have received a copy of the GNU General Public License
45 along with this program; if not, write to the Free Software
46 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
47 02111-1307, USA.
48
49 The GNU General Public License is contained in the file COPYING.
50*/
51
sewardj896f6f92008-08-19 08:38:52 +000052#ifndef __PUB_TOOL_WORDFM_H
53#define __PUB_TOOL_WORDFM_H
sewardjb4112022007-11-09 22:49:28 +000054
55//------------------------------------------------------------------//
56//--- WordFM ---//
57//--- Public interface ---//
58//------------------------------------------------------------------//
59
sewardjfc4b63a2008-02-17 11:46:58 +000060/* As of r7409 (15 Feb 08), all these word-based abstractions (WordFM,
61 WordSet, WordBag) now operate on unsigned words (UWord), whereas
62 they previously operated on signed words (Word). This became a
63 problem, when using unboxed comparisons (when kCmp == NULL), with
sewardj896f6f92008-08-19 08:38:52 +000064 the introduction of VG_(initIterAtFM), which allows iteration over
sewardjfc4b63a2008-02-17 11:46:58 +000065 parts of mappings. Iterating over a mapping in increasing order of
66 signed Word keys is not what callers expect when iterating through
67 maps whose keys represent addresses (Addr) since Addr is unsigned,
68 and causes logical problems and assertion failures. */
69
sewardjb4112022007-11-09 22:49:28 +000070typedef struct _WordFM WordFM; /* opaque */
71
sewardj250ec2e2008-02-15 22:02:30 +000072/* Allocate and initialise a WordFM. If kCmp is non-NULL, elements in
73 the set are ordered according to the ordering specified by kCmp,
74 which becomes obvious if you use VG_(initIterFM),
75 VG_(initIterAtFM), VG_(nextIterFM), VG_(doneIterFM) to iterate over
sewardjfc4b63a2008-02-17 11:46:58 +000076 sections of the map, or the whole thing. If kCmp is NULL then the
77 ordering used is unsigned word ordering (UWord) on the key
78 values. */
sewardj9c606bd2008-09-18 18:12:50 +000079WordFM* VG_(newFM) ( void* (*alloc_nofail)( HChar* cc, SizeT ),
80 HChar* cc,
sewardjb4112022007-11-09 22:49:28 +000081 void (*dealloc)(void*),
sewardj250ec2e2008-02-15 22:02:30 +000082 Word (*kCmp)(UWord,UWord) );
sewardjb4112022007-11-09 22:49:28 +000083
84/* Free up the FM. If kFin is non-NULL, it is applied to keys
85 before the FM is deleted; ditto with vFin for vals. */
sewardj896f6f92008-08-19 08:38:52 +000086void VG_(deleteFM) ( WordFM*, void(*kFin)(UWord), void(*vFin)(UWord) );
sewardjb4112022007-11-09 22:49:28 +000087
88/* Add (k,v) to fm. If a binding for k already exists, it is updated
89 to map to this new v. In that case we should really return the
sewardj9c606bd2008-09-18 18:12:50 +000090 previous v so that caller can finalise it. Oh well. Returns
91 True if a binding for k already exists. */
92Bool VG_(addToFM) ( WordFM* fm, UWord k, UWord v );
sewardjb4112022007-11-09 22:49:28 +000093
94// Delete key from fm, returning associated key and val if found
sewardj896f6f92008-08-19 08:38:52 +000095Bool VG_(delFromFM) ( WordFM* fm,
sewardj250ec2e2008-02-15 22:02:30 +000096 /*OUT*/UWord* oldK, /*OUT*/UWord* oldV, UWord key );
sewardjb4112022007-11-09 22:49:28 +000097
98// Look up in fm, assigning found key & val at spec'd addresses
sewardj896f6f92008-08-19 08:38:52 +000099Bool VG_(lookupFM) ( WordFM* fm,
sewardj250ec2e2008-02-15 22:02:30 +0000100 /*OUT*/UWord* keyP, /*OUT*/UWord* valP, UWord key );
sewardjb4112022007-11-09 22:49:28 +0000101
sewardj9c606bd2008-09-18 18:12:50 +0000102// Find the closest key values bracketing the given key, assuming the
103// given key is not present in the map. minKey and maxKey are the
104// minimum and maximum possible key values. The resulting bracket
105// values are returned in *kMinP and *kMaxP. It follows that if fm is
106// empty then the returned values are simply minKey and maxKey.
107//
108// If the operation was successful (that is, the given key is not
109// present), True is returned. If the given key is in fact present,
110// False is returned, and *kMinP and *kMaxP are undefined.
111Bool VG_(findBoundsFM)( WordFM* fm,
112 /*OUT*/UWord* kMinP, /*OUT*/UWord* kMaxP,
113 UWord minKey, UWord maxKey, UWord key );
114
sewardjb4112022007-11-09 22:49:28 +0000115// How many elements are there in fm?
sewardj896f6f92008-08-19 08:38:52 +0000116UWord VG_(sizeFM) ( WordFM* fm );
sewardjb4112022007-11-09 22:49:28 +0000117
118// set up FM for iteration
sewardj896f6f92008-08-19 08:38:52 +0000119void VG_(initIterFM) ( WordFM* fm );
sewardjb4112022007-11-09 22:49:28 +0000120
sewardj250ec2e2008-02-15 22:02:30 +0000121// set up FM for iteration so that the first key subsequently produced
sewardj896f6f92008-08-19 08:38:52 +0000122// by VG_(nextIterFM) is the smallest key in the map >= start_at.
sewardj250ec2e2008-02-15 22:02:30 +0000123// Naturally ">=" is defined by the comparison function supplied to
sewardj896f6f92008-08-19 08:38:52 +0000124// VG_(newFM), as documented above.
125void VG_(initIterAtFM) ( WordFM* fm, UWord start_at );
sewardjae5137e2008-01-17 23:19:54 +0000126
sewardjb4112022007-11-09 22:49:28 +0000127// get next key/val pair. Will assert if fm has been modified
sewardjae5137e2008-01-17 23:19:54 +0000128// or looked up in since initIterFM/initIterWithStartFM was called.
sewardj896f6f92008-08-19 08:38:52 +0000129Bool VG_(nextIterFM) ( WordFM* fm,
sewardj250ec2e2008-02-15 22:02:30 +0000130 /*OUT*/UWord* pKey, /*OUT*/UWord* pVal );
sewardjb4112022007-11-09 22:49:28 +0000131
132// clear the I'm iterating flag
sewardj896f6f92008-08-19 08:38:52 +0000133void VG_(doneIterFM) ( WordFM* fm );
sewardjb4112022007-11-09 22:49:28 +0000134
135// Deep copy a FM. If dopyK is NULL, keys are copied verbatim.
136// If non-null, dopyK is applied to each key to generate the
137// version in the new copy. In that case, if the argument to dopyK
138// is non-NULL but the result is NULL, it is assumed that dopyK
139// could not allocate memory, in which case the copy is abandoned
140// and NULL is returned. Ditto with dopyV for values.
sewardj896f6f92008-08-19 08:38:52 +0000141WordFM* VG_(dopyFM) ( WordFM* fm,
sewardj250ec2e2008-02-15 22:02:30 +0000142 UWord(*dopyK)(UWord), UWord(*dopyV)(UWord) );
sewardjb4112022007-11-09 22:49:28 +0000143
144//------------------------------------------------------------------//
145//--- end WordFM ---//
146//--- Public interface ---//
147//------------------------------------------------------------------//
148
149//------------------------------------------------------------------//
150//--- WordBag (unboxed words only) ---//
151//--- Public interface ---//
152//------------------------------------------------------------------//
153
154typedef struct _WordBag WordBag; /* opaque */
155
156/* Allocate and initialise a WordBag */
sewardj9c606bd2008-09-18 18:12:50 +0000157WordBag* VG_(newBag) ( void* (*alloc_nofail)( HChar* cc, SizeT ),
158 HChar* cc,
sewardjb4112022007-11-09 22:49:28 +0000159 void (*dealloc)(void*) );
160
161/* Free up the Bag. */
sewardj896f6f92008-08-19 08:38:52 +0000162void VG_(deleteBag) ( WordBag* );
sewardjb4112022007-11-09 22:49:28 +0000163
164/* Add a word. */
sewardj896f6f92008-08-19 08:38:52 +0000165void VG_(addToBag)( WordBag*, UWord );
sewardjb4112022007-11-09 22:49:28 +0000166
167/* Find out how many times the given word exists in the bag. */
sewardj896f6f92008-08-19 08:38:52 +0000168UWord VG_(elemBag) ( WordBag*, UWord );
sewardjb4112022007-11-09 22:49:28 +0000169
170/* Delete a word from the bag. */
sewardj896f6f92008-08-19 08:38:52 +0000171Bool VG_(delFromBag)( WordBag*, UWord );
sewardjb4112022007-11-09 22:49:28 +0000172
173/* Is the bag empty? */
sewardj896f6f92008-08-19 08:38:52 +0000174Bool VG_(isEmptyBag)( WordBag* );
sewardjb4112022007-11-09 22:49:28 +0000175
176/* Does the bag have exactly one element? */
sewardj896f6f92008-08-19 08:38:52 +0000177Bool VG_(isSingletonTotalBag)( WordBag* );
sewardjb4112022007-11-09 22:49:28 +0000178
179/* Return an arbitrary element from the bag. */
sewardj896f6f92008-08-19 08:38:52 +0000180UWord VG_(anyElementOfBag)( WordBag* );
sewardjb4112022007-11-09 22:49:28 +0000181
182/* How many different / total elements are in the bag? */
sewardj896f6f92008-08-19 08:38:52 +0000183UWord VG_(sizeUniqueBag)( WordBag* ); /* fast */
184UWord VG_(sizeTotalBag)( WordBag* ); /* warning: slow */
sewardjb4112022007-11-09 22:49:28 +0000185
186/* Iterating over the elements of a bag. */
sewardj896f6f92008-08-19 08:38:52 +0000187void VG_(initIterBag)( WordBag* );
188Bool VG_(nextIterBag)( WordBag*, /*OUT*/UWord* pVal, /*OUT*/UWord* pCount );
189void VG_(doneIterBag)( WordBag* );
sewardjb4112022007-11-09 22:49:28 +0000190
191//------------------------------------------------------------------//
192//--- end WordBag (unboxed words only) ---//
193//--- Public interface ---//
194//------------------------------------------------------------------//
195
sewardj896f6f92008-08-19 08:38:52 +0000196#endif /* ! __PUB_TOOL_WORDFM_H */
sewardjb4112022007-11-09 22:49:28 +0000197
198/*--------------------------------------------------------------------*/
sewardj896f6f92008-08-19 08:38:52 +0000199/*--- end pub_tool_wordfm.h ---*/
sewardjb4112022007-11-09 22:49:28 +0000200/*--------------------------------------------------------------------*/