tommyds/tommyarray.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Andrea Mazzoleni. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  *
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00025  * POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00040 #ifndef __TOMMYARRAY_H
00041 #define __TOMMYARRAY_H
00042 
00043 #include "tommytypes.h"
00044 
00045 #include <assert.h> /* for assert */
00046 
00047 /******************************************************************************/
00048 /* array */
00049 
00054 #define TOMMY_ARRAY_BIT 6
00055 
00059 #define TOMMY_ARRAY_BIT_MAX 32
00060 
00065 typedef struct tommy_array_struct {
00066         void** bucket[TOMMY_ARRAY_BIT_MAX]; 
00067         tommy_uint_t bucket_bit; 
00068         tommy_count_t bucket_max; 
00069         tommy_count_t count; 
00070 } tommy_array;
00071 
00075 void tommy_array_init(tommy_array* array);
00076 
00080 void tommy_array_done(tommy_array* array);
00081 
00086 void tommy_array_grow(tommy_array* array, tommy_count_t size);
00087 
00093 tommy_inline void** tommy_array_ref(tommy_array* array, tommy_count_t pos)
00094 {
00095         tommy_uint_t bsr;
00096 
00097         assert(pos < array->count);
00098 
00099         /* get the highest bit set, in case of all 0, return 0 */
00100         bsr = tommy_ilog2_u32(pos | 1);
00101 
00102         return &array->bucket[bsr][pos];
00103 }
00104 
00110 tommy_inline void tommy_array_set(tommy_array* array, tommy_count_t pos, void* element)
00111 {
00112         *tommy_array_ref(array, pos) = element;
00113 }
00114 
00120 tommy_inline void* tommy_array_get(tommy_array* array, tommy_count_t pos)
00121 {
00122         return *tommy_array_ref(array, pos);
00123 }
00124 
00128 tommy_inline void tommy_array_insert(tommy_array* array, void* element)
00129 {
00130         tommy_count_t pos = array->count;
00131 
00132         tommy_array_grow(array, pos + 1);
00133 
00134         tommy_array_set(array, pos, element);
00135 }
00136 
00140 tommy_inline tommy_count_t tommy_array_size(tommy_array* array)
00141 {
00142         return array->count;
00143 }
00144 
00148 tommy_size_t tommy_array_memory_usage(tommy_array* array);
00149 
00150 #endif
00151