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
60typedef struct tommy_array_struct {
61 void** bucket[TOMMY_SIZE_BIT];
66
70TOMMY_API void tommy_array_init(tommy_array* array);
71
75TOMMY_API void tommy_array_done(tommy_array* array);
76
81TOMMY_API void tommy_array_grow(tommy_array* array, tommy_size_t size);
82
88tommy_inline void** tommy_array_ref(tommy_array* array, tommy_size_t pos)
89{
90 tommy_uint_t bsr;
91
92 assert(pos < array->count);
93
94 /* get the highest bit set, in case of all 0, return 0 */
95 bsr = tommy_ilog2(pos | 1);
96
97 return &array->bucket[bsr][pos];
98}
99
105tommy_inline void tommy_array_set(tommy_array* array, tommy_size_t pos, void* element)
106{
107 *tommy_array_ref(array, pos) = element;
108}
109
115tommy_inline void* tommy_array_get(tommy_array* array, tommy_size_t pos)
116{
117 return *tommy_array_ref(array, pos);
118}
119
123tommy_inline void tommy_array_insert(tommy_array* array, void* element)
124{
125 tommy_size_t pos = array->count;
126
127 tommy_array_grow(array, pos + 1);
128
129 tommy_array_set(array, pos, element);
130}
131
136{
137 return array->count;
138}
139
144
145#endif
Array container type.
Definition: tommyarray.h:60
tommy_size_t count
Number of initialized elements in the array.
Definition: tommyarray.h:63
void ** bucket[TOMMY_SIZE_BIT]
Dynamic array of buckets.
Definition: tommyarray.h:61
tommy_uint_t bucket_bit
Bits used in the bit mask.
Definition: tommyarray.h:64
tommy_size_t bucket_max
Number of buckets.
Definition: tommyarray.h:62
TOMMY_API void tommy_array_grow(tommy_array *array, tommy_size_t size)
Grows the size up to the specified value.
void tommy_array_set(tommy_array *array, tommy_size_t pos, void *element)
Sets the element at the specified position.
Definition: tommyarray.h:105
TOMMY_API tommy_size_t tommy_array_memory_usage(tommy_array *array)
Gets the size of allocated memory.
void * tommy_array_get(tommy_array *array, tommy_size_t pos)
Gets the element at the specified position.
Definition: tommyarray.h:115
void ** tommy_array_ref(tommy_array *array, tommy_size_t pos)
Gets a reference of the element at the specified position.
Definition: tommyarray.h:88
void tommy_array_insert(tommy_array *array, void *element)
Grows and inserts a new element at the end of the array.
Definition: tommyarray.h:123
TOMMY_API void tommy_array_done(tommy_array *array)
Deinitializes the array.
tommy_size_t tommy_array_size(tommy_array *array)
Gets the initialized size of the array.
Definition: tommyarray.h:135
struct tommy_array_struct tommy_array
Array container type.
TOMMY_API void tommy_array_init(tommy_array *array)
Initializes the array.
Generic types.
uint64_t tommy_size_t
Generic size_t type.
Definition: tommytypes.h:60
tommy_uint32_t tommy_uint_t
Generic unsigned integer type.
Definition: tommytypes.h:80