sl@0: /* Copyright (c) 2009 The Khronos Group Inc. sl@0: * sl@0: * Permission is hereby granted, free of charge, to any person obtaining a sl@0: * copy of this software and/or associated documentation files (the sl@0: * "Materials"), to deal in the Materials without restriction, including sl@0: * without limitation the rights to use, copy, modify, merge, publish, sl@0: * distribute, sublicense, and/or sell copies of the Materials, and to sl@0: * permit persons to whom the Materials are furnished to do so, subject to sl@0: * the following conditions: sl@0: * sl@0: * The above copyright notice and this permission notice shall be included sl@0: * in all copies or substantial portions of the Materials. sl@0: * sl@0: * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, sl@0: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF sl@0: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. sl@0: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY sl@0: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, sl@0: * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE sl@0: * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. sl@0: */ sl@0: sl@0: sl@0: #ifdef __cplusplus sl@0: extern "C" sl@0: { sl@0: #endif sl@0: sl@0: sl@0: #include sl@0: #include sl@0: sl@0: #include "owfarray.h" sl@0: #include "owftypes.h" sl@0: #include "owfdebug.h" sl@0: sl@0: sl@0: sl@0: #define MINIMUM_CAPACITY 8 sl@0: sl@0: /* switch debug messages off (1) or off (0) */ sl@0: #if 1 sl@0: #ifdef DPRINT sl@0: #undef DPRINT sl@0: #endif sl@0: #define DPRINT(x) sl@0: #endif sl@0: sl@0: #define SHIFT(a,f,t,n) memmove(&(a)->items[t], \ sl@0: &(a)->items[f], \ sl@0: (n) * sizeof(OWF_ARRAY_ITEM)) sl@0: sl@0: sl@0: void sl@0: OWF_Array_Initialize(OWF_ARRAY* array) sl@0: { sl@0: OWF_ASSERT(array); sl@0: sl@0: memset(array, 0, sizeof(OWF_ARRAY)); sl@0: } sl@0: sl@0: void sl@0: OWF_Array_Reset(OWF_ARRAY* array) sl@0: { sl@0: OWF_ASSERT(array); sl@0: sl@0: array->length = 0; sl@0: memset(array->items, 0, sizeof(OWF_ARRAY_ITEM) * array->capacity); sl@0: sl@0: } sl@0: sl@0: void sl@0: OWF_Array_Destroy(OWF_ARRAY* array) sl@0: { sl@0: OWF_ASSERT(array); sl@0: sl@0: free(array->items); sl@0: OWF_Array_Initialize(array); sl@0: } sl@0: sl@0: static OWFboolean sl@0: OWF_Array_Enlarge(OWF_ARRAY* array) sl@0: { sl@0: OWF_ARRAY_ITEM* temp = NULL; sl@0: OWFint newcapacity = 0; sl@0: sl@0: OWF_ASSERT(array); sl@0: sl@0: DPRINT(("OWF_Array_Enlarge\n")); sl@0: DPRINT((" capacity = %d, length = %d\n", array->capacity, array->length)); sl@0: sl@0: /* sl@0: newcapacity = MAX(MINIMUM_CAPACITY, sl@0: array->capacity + (array->capacity >> 1)); sl@0: */ sl@0: newcapacity = MAX(MINIMUM_CAPACITY, 2 * array->capacity); sl@0: sl@0: temp = (OWF_ARRAY_ITEM*) realloc(array->items, sl@0: sizeof(OWF_ARRAY_ITEM) * newcapacity); sl@0: sl@0: if (!temp) sl@0: { sl@0: return OWF_FALSE; sl@0: } sl@0: sl@0: DPRINT((" new capacity = %d\n", newcapacity)); sl@0: sl@0: array->items = temp; sl@0: array->capacity = newcapacity; sl@0: sl@0: return OWF_TRUE; sl@0: } sl@0: sl@0: OWFboolean sl@0: OWF_Array_AppendItem(OWF_ARRAY* array, sl@0: OWF_ARRAY_ITEM item) sl@0: { sl@0: OWF_ASSERT(array); sl@0: sl@0: DPRINT(("OWF_Array_AppendItem\n")); sl@0: sl@0: if (array->length >= array->capacity) sl@0: { sl@0: if (!OWF_Array_Enlarge(array)) sl@0: { sl@0: return OWF_FALSE; sl@0: } sl@0: } sl@0: sl@0: DPRINT((" item[%d] is now %p\n", array->length, item)); sl@0: sl@0: array->items[array->length] = item; sl@0: ++array->length; sl@0: sl@0: return OWF_TRUE; sl@0: } sl@0: sl@0: OWFboolean sl@0: OWF_Array_InsertItem(OWF_ARRAY* array, sl@0: OWFint position, sl@0: OWF_ARRAY_ITEM item) sl@0: { sl@0: OWF_ASSERT(array); sl@0: sl@0: DPRINT(("bounds check\n")); sl@0: sl@0: /* check bounds */ sl@0: if (position < 0 || position > array->length) sl@0: { sl@0: return OWF_FALSE; sl@0: } sl@0: sl@0: DPRINT(("enlarge\n")); sl@0: sl@0: if (array->length >= array->capacity) sl@0: { sl@0: if (!OWF_Array_Enlarge(array)) sl@0: { sl@0: return OWF_FALSE; sl@0: } sl@0: } sl@0: sl@0: DPRINT(("new capacity = %d\n", array->capacity)); sl@0: sl@0: /* shift forward (obs! memmove because src & dst overlap) */ sl@0: SHIFT(array, position, position + 1, array->length - position); sl@0: sl@0: DPRINT((" item[%d] is now %p\n", array->length, item)); sl@0: sl@0: /* put */ sl@0: array->items[position] = item; sl@0: ++array->length; sl@0: sl@0: return OWF_TRUE; sl@0: } sl@0: sl@0: OWF_ARRAY_ITEM sl@0: OWF_Array_RemoveItem(OWF_ARRAY* array, sl@0: OWF_ARRAY_ITEM item) sl@0: { sl@0: OWFint ii = 0; sl@0: OWF_ARRAY_ITEM result = NULL; sl@0: sl@0: OWF_ASSERT(array); sl@0: sl@0: for (ii = 0; ii < array->length; ii++) sl@0: { sl@0: if (array->items[ii] == item) sl@0: { sl@0: result = OWF_Array_RemoveItemAt(array, ii); sl@0: break; sl@0: } sl@0: } sl@0: return result; sl@0: } sl@0: sl@0: OWF_ARRAY_ITEM sl@0: OWF_Array_RemoveItemAt(OWF_ARRAY* array, sl@0: OWFint position) sl@0: { sl@0: OWF_ARRAY_ITEM result; sl@0: sl@0: OWF_ASSERT(array); sl@0: sl@0: /* check bounds */ sl@0: if (position < 0 || position >= array->length) sl@0: { sl@0: return NULL; sl@0: } sl@0: sl@0: --array->length; sl@0: result = array->items[position]; sl@0: SHIFT(array, position + 1, position, array->length - position); sl@0: sl@0: return result; sl@0: } sl@0: sl@0: OWF_ARRAY_ITEM sl@0: OWF_Array_GetItemAt(OWF_ARRAY* array, sl@0: OWFint position) sl@0: { sl@0: OWF_ASSERT(array); sl@0: sl@0: if (position < 0 || position >= array->length) sl@0: { sl@0: return NULL; sl@0: } sl@0: sl@0: return array->items[position]; sl@0: } sl@0: sl@0: #ifdef __cplusplus sl@0: } sl@0: #endif sl@0: