tommyarray.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, Andrea Mazzoleni. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
40 #ifndef __TOMMYARRAY_H
41 #define __TOMMYARRAY_H
42 
43 #include "tommytypes.h"
44 
45 #include <assert.h> /* for assert */
46 
47 /******************************************************************************/
48 /* array */
49 
54 #define TOMMY_ARRAY_BIT 6
55 
59 #define TOMMY_ARRAY_BIT_MAX 32
60 
65 typedef struct tommy_array_struct {
66  void** bucket[TOMMY_ARRAY_BIT_MAX];
70 } tommy_array;
71 
75 void tommy_array_init(tommy_array* array);
76 
80 void tommy_array_done(tommy_array* array);
81 
86 void tommy_array_grow(tommy_array* array, tommy_count_t size);
87 
93 tommy_inline void** tommy_array_ref(tommy_array* array, tommy_count_t pos)
94 {
95  tommy_uint_t bsr;
96 
97  assert(pos < array->count);
98 
99  /* get the highest bit set, in case of all 0, return 0 */
100  bsr = tommy_ilog2_u32(pos | 1);
101 
102  return &array->bucket[bsr][pos];
103 }
104 
110 tommy_inline void tommy_array_set(tommy_array* array, tommy_count_t pos, void* element)
111 {
112  *tommy_array_ref(array, pos) = element;
113 }
114 
120 tommy_inline void* tommy_array_get(tommy_array* array, tommy_count_t pos)
121 {
122  return *tommy_array_ref(array, pos);
123 }
124 
128 tommy_inline void tommy_array_insert(tommy_array* array, void* element)
129 {
130  tommy_count_t pos = array->count;
131 
132  tommy_array_grow(array, pos + 1);
133 
134  tommy_array_set(array, pos, element);
135 }
136 
141 {
142  return array->count;
143 }
144 
149 
150 #endif
151 
void tommy_array_done(tommy_array *array)
Deinitializes the array.
void tommy_array_insert(tommy_array *array, void *element)
Grows and inserts a new element at the end of the array.
Definition: tommyarray.h:128
tommy_uint_t tommy_ilog2_u32(tommy_uint32_t value)
Bit scan reverse or integer log2.
Definition: tommytypes.h:331
tommy_uint32_t tommy_count_t
Generic unsigned integer for counting objects.
Definition: tommytypes.h:67
struct tommy_array_struct tommy_array
Array container type.
void tommy_array_grow(tommy_array *array, tommy_count_t size)
Grows the size up to the specified value.
tommy_uint_t bucket_bit
Bits used in the bit mask.
Definition: tommyarray.h:67
void ** bucket[TOMMY_ARRAY_BIT_MAX]
Dynamic array of buckets.
Definition: tommyarray.h:66
tommy_uint32_t tommy_uint_t
Generic unsigned integer type.
Definition: tommytypes.h:60
tommy_count_t count
Number of initialized elements in the array.
Definition: tommyarray.h:69
void tommy_array_set(tommy_array *array, tommy_count_t pos, void *element)
Sets the element at the specified position.
Definition: tommyarray.h:110
tommy_count_t tommy_array_size(tommy_array *array)
Gets the initialized size of the array.
Definition: tommyarray.h:140
void * tommy_array_get(tommy_array *array, tommy_count_t pos)
Gets the element at the specified position.
Definition: tommyarray.h:120
tommy_count_t bucket_max
Number of buckets.
Definition: tommyarray.h:68
tommy_size_t tommy_array_memory_usage(tommy_array *array)
Gets the size of allocated memory.
void tommy_array_init(tommy_array *array)
Initializes the array.
size_t tommy_size_t
Generic size_t type.
Definition: tommytypes.h:50
Array container type.
Definition: tommyarray.h:65
Generic types.
void ** tommy_array_ref(tommy_array *array, tommy_count_t pos)
Gets a reference of the element at the specified position.
Definition: tommyarray.h:93