09c714811cd3bb515ff58e4d144714611cb3ea34
[pspp-builds.git] / src / libpspp / sparse-array.h
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 /* Sparse array data structure.
20
21    Implements a dictionary that associates a "unsigned long int"
22    key with fixed-size values (elements).
23
24    The implementation allocates elements in groups of moderate
25    size, so it achieves maximum space efficiency when elements
26    are clustered into groups of consecutive keys.  For the same
27    reason, elements should be kept relatively small, perhaps a
28    few pointer elements in size.
29
30    The implementation is slightly more efficient both in time and
31    space when indexes are kept small.  Thus, for example, if the
32    indexes in use start from some fixed base value, consider
33    using the offset from that base as the index value. */
34
35 #ifndef LIBPSPP_SPARSE_ARRAY_H
36 #define LIBPSPP_SPARSE_ARRAY_H 1
37
38 #include <stddef.h>
39 #include <stdbool.h>
40
41 #include <libpspp/hash.h>
42
43 struct sparse_array *sparse_array_create (size_t elem_size);
44 struct sparse_array *sparse_array_create_pool (struct pool *,
45                                                size_t elem_size);
46 void sparse_array_destroy (struct sparse_array *);
47
48 unsigned long int sparse_array_count (const struct sparse_array *);
49
50 void *sparse_array_insert (struct sparse_array *, unsigned long int key);
51 void *sparse_array_get (const struct sparse_array *, unsigned long int key);
52 bool sparse_array_remove (struct sparse_array *, unsigned long int key);
53
54 void *sparse_array_scan (const struct sparse_array *,
55                          unsigned long int *skip,
56                          unsigned long int *key);
57
58 #endif /* libpspp/sparse-array.h */