pub_tool_sparsewa.h revision fc252b5716c04f66bcf2a72033b006e6993975ce
1
2/*--------------------------------------------------------------------*/
3/*--- An sparse array (of words) implementation.                   ---*/
4/*---                                          pub_tool_sparsewa.h ---*/
5/*--------------------------------------------------------------------*/
6
7/*
8   This file is part of Valgrind, a dynamic binary instrumentation
9   framework.
10
11   Copyright (C) 2008-2013 OpenWorks Ltd
12      info@open-works.co.uk
13
14   This program is free software; you can redistribute it and/or
15   modify it under the terms of the GNU General Public License as
16   published by the Free Software Foundation; either version 2 of the
17   License, or (at your option) any later version.
18
19   This program is distributed in the hope that it will be useful, but
20   WITHOUT ANY WARRANTY; without even the implied warranty of
21   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   General Public License for more details.
23
24   You should have received a copy of the GNU General Public License
25   along with this program; if not, write to the Free Software
26   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27   02111-1307, USA.
28
29   The GNU General Public License is contained in the file COPYING.
30*/
31
32#ifndef __PUB_TOOL_SPARSEWA_H
33#define __PUB_TOOL_SPARSEWA_H
34
35#include "pub_tool_basics.h"   // UWord
36
37//--------------------------------------------------------------------
38// PURPOSE: (see coregrind/pub_core_sparsewa.h for details)
39//--------------------------------------------------------------------
40
41/////////////////////////////////////////////////////////
42//                                                     //
43// SparseWA: Interface                                 //
44//                                                     //
45/////////////////////////////////////////////////////////
46
47// This interface is a very cut-down version of WordFM.
48// If you understand how to use WordFM then it should be
49// trivial to use SparseWA.
50
51typedef  struct _SparseWA  SparseWA; /* opaque */
52
53// Create a new one, using the specified allocator/deallocator.
54// Never returns NULL.
55SparseWA* VG_(newSWA) ( void*(*alloc_nofail)(const HChar* cc, SizeT),
56                        const HChar* cc,
57                        void(*dealloc)(void*) );
58
59// Delete one, and free all associated storage
60void VG_(deleteSWA) ( SparseWA* swa );
61
62// Add the binding key -> val to this swa.  Any existing binding is
63// overwritten.  Returned Bool is True iff a previous binding existed.
64Bool VG_(addToSWA) ( SparseWA* swa, UWord key, UWord val );
65
66// Delete key from swa, returning associated key and val if found.
67// Note: returning associated key is stupid (it can only be the
68// key you just specified).  This behaviour is retained to make it
69// easier to migrate from WordFM.  Returned Bool is True iff
70// the key was actually bound in the mapping.
71Bool VG_(delFromSWA) ( SparseWA* swa,
72                       /*OUT*/UWord* oldK, /*OUT*/UWord* oldV,
73                       UWord key );
74
75// Indexes swa at 'key' (or, if you like, looks up 'key' in the
76// mapping), and returns the associated value, if any, in *valP.  For
77// compatibility with WordFM, 'key' is also returned in *keyP.  Returned
78// Bool is True iff a binding for 'key' actually existed.
79Bool VG_(lookupSWA) ( SparseWA* swa,
80                      /*OUT*/UWord* keyP, /*OUT*/UWord* valP,
81                      UWord key );
82
83// Set up 'swa' for iteration.
84void VG_(initIterSWA) ( SparseWA* swa );
85
86// Get the next key/val pair.  Behaviour undefined (highly likely
87// to segfault) if 'swa' has been modified since initIterSWA was
88// called.  Returned Bool is False iff there are no more pairs
89// that can be extracted.
90Bool VG_(nextIterSWA)( SparseWA* swa,
91                       /*OUT*/UWord* keyP, /*OUT*/UWord* valP );
92
93// How many elements are there in 'swa'?  NOTE: dangerous in the
94// sense that this is not an O(1) operation but rather O(N),
95// since it involves walking the whole tree.
96UWord VG_(sizeSWA) ( SparseWA* swa );
97
98#endif   // __PUB_TOOL_SPARSEWA_H
99
100/*--------------------------------------------------------------------*/
101/*--- end                                      pub_tool_sparsewa.h ---*/
102/*--------------------------------------------------------------------*/
103