sewardj | 78b7ecf | 2008-12-06 22:07:35 +0000 | [diff] [blame] | 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 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame] | 11 | Copyright (C) 2008-2017 OpenWorks Ltd |
sewardj | 78b7ecf | 2008-12-06 22:07:35 +0000 | [diff] [blame] | 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 | |
florian | 535fb1b | 2013-09-15 13:54:34 +0000 | [diff] [blame] | 35 | #include "pub_tool_basics.h" // UWord |
| 36 | |
sewardj | 78b7ecf | 2008-12-06 22:07:35 +0000 | [diff] [blame] | 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 | |
| 51 | typedef struct _SparseWA SparseWA; /* opaque */ |
| 52 | |
florian | fc252b5 | 2014-09-13 22:39:31 +0000 | [diff] [blame] | 53 | // Create a new one, using the specified allocator/deallocator. |
| 54 | // Never returns NULL. |
florian | 54fe202 | 2012-10-27 23:07:42 +0000 | [diff] [blame] | 55 | SparseWA* VG_(newSWA) ( void*(*alloc_nofail)(const HChar* cc, SizeT), |
| 56 | const HChar* cc, |
sewardj | 78b7ecf | 2008-12-06 22:07:35 +0000 | [diff] [blame] | 57 | void(*dealloc)(void*) ); |
| 58 | |
| 59 | // Delete one, and free all associated storage |
| 60 | void 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. |
| 64 | Bool VG_(addToSWA) ( SparseWA* swa, UWord key, UWord val ); |
| 65 | |
philippe | 40648e2 | 2015-04-11 11:42:22 +0000 | [diff] [blame] | 66 | // Delete key from swa, returning val if found. |
| 67 | // Returned Bool is True iff the key was actually bound in the mapping. |
sewardj | 78b7ecf | 2008-12-06 22:07:35 +0000 | [diff] [blame] | 68 | Bool VG_(delFromSWA) ( SparseWA* swa, |
philippe | 40648e2 | 2015-04-11 11:42:22 +0000 | [diff] [blame] | 69 | /*OUT*/UWord* oldV, |
sewardj | 78b7ecf | 2008-12-06 22:07:35 +0000 | [diff] [blame] | 70 | UWord key ); |
| 71 | |
| 72 | // Indexes swa at 'key' (or, if you like, looks up 'key' in the |
philippe | 40648e2 | 2015-04-11 11:42:22 +0000 | [diff] [blame] | 73 | // mapping), and returns the associated value, if any, in *valP. |
| 74 | // Returned Bool is True iff a binding for 'key' actually existed. |
florian | ee0d0e9 | 2014-10-18 16:17:13 +0000 | [diff] [blame] | 75 | Bool VG_(lookupSWA) ( const SparseWA* swa, |
philippe | 40648e2 | 2015-04-11 11:42:22 +0000 | [diff] [blame] | 76 | /*OUT*/UWord* valP, |
sewardj | 78b7ecf | 2008-12-06 22:07:35 +0000 | [diff] [blame] | 77 | UWord key ); |
| 78 | |
| 79 | // Set up 'swa' for iteration. |
| 80 | void VG_(initIterSWA) ( SparseWA* swa ); |
| 81 | |
| 82 | // Get the next key/val pair. Behaviour undefined (highly likely |
| 83 | // to segfault) if 'swa' has been modified since initIterSWA was |
| 84 | // called. Returned Bool is False iff there are no more pairs |
| 85 | // that can be extracted. |
| 86 | Bool VG_(nextIterSWA)( SparseWA* swa, |
| 87 | /*OUT*/UWord* keyP, /*OUT*/UWord* valP ); |
| 88 | |
sewardj | f35dbb8 | 2008-12-06 23:34:52 +0000 | [diff] [blame] | 89 | // How many elements are there in 'swa'? NOTE: dangerous in the |
| 90 | // sense that this is not an O(1) operation but rather O(N), |
| 91 | // since it involves walking the whole tree. |
florian | ee0d0e9 | 2014-10-18 16:17:13 +0000 | [diff] [blame] | 92 | UWord VG_(sizeSWA) ( const SparseWA* swa ); |
sewardj | 78b7ecf | 2008-12-06 22:07:35 +0000 | [diff] [blame] | 93 | |
| 94 | #endif // __PUB_TOOL_SPARSEWA_H |
| 95 | |
| 96 | /*--------------------------------------------------------------------*/ |
| 97 | /*--- end pub_tool_sparsewa.h ---*/ |
| 98 | /*--------------------------------------------------------------------*/ |