os/graphics/graphicscomposition/openwfcompositionengine/common/src/owfarray.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* Copyright (c) 2009 The Khronos Group Inc.
     2  *
     3  * Permission is hereby granted, free of charge, to any person obtaining a
     4  * copy of this software and/or associated documentation files (the
     5  * "Materials"), to deal in the Materials without restriction, including
     6  * without limitation the rights to use, copy, modify, merge, publish,
     7  * distribute, sublicense, and/or sell copies of the Materials, and to
     8  * permit persons to whom the Materials are furnished to do so, subject to
     9  * the following conditions:
    10  *
    11  * The above copyright notice and this permission notice shall be included
    12  * in all copies or substantial portions of the Materials.
    13  *
    14  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    20  * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
    21  */
    22 
    23 
    24 #ifdef __cplusplus
    25 extern "C"
    26 {
    27 #endif
    28 
    29 
    30 #include <string.h>
    31 #include <stdlib.h>
    32 
    33 #include "owfarray.h"
    34 #include "owftypes.h"
    35 #include "owfdebug.h"
    36 
    37 
    38 
    39 #define MINIMUM_CAPACITY    8
    40 
    41 /* switch debug messages off (1) or off (0) */
    42 #if 1
    43 #ifdef DPRINT
    44 #undef DPRINT
    45 #endif
    46 #define DPRINT(x)
    47 #endif
    48 
    49 #define SHIFT(a,f,t,n)        memmove(&(a)->items[t], \
    50                                     &(a)->items[f], \
    51                                     (n) * sizeof(OWF_ARRAY_ITEM))
    52 
    53 
    54 void
    55 OWF_Array_Initialize(OWF_ARRAY* array)
    56 {
    57     OWF_ASSERT(array);
    58 
    59     memset(array, 0, sizeof(OWF_ARRAY));
    60 }
    61 
    62 void
    63 OWF_Array_Reset(OWF_ARRAY* array)
    64 {
    65     OWF_ASSERT(array);
    66 
    67     array->length = 0;
    68     memset(array->items, 0, sizeof(OWF_ARRAY_ITEM) * array->capacity);
    69 
    70 }
    71 
    72 void
    73 OWF_Array_Destroy(OWF_ARRAY* array)
    74 {
    75     OWF_ASSERT(array);
    76 
    77     free(array->items);
    78     OWF_Array_Initialize(array);
    79 }
    80 
    81 static OWFboolean
    82 OWF_Array_Enlarge(OWF_ARRAY* array)
    83 {
    84     OWF_ARRAY_ITEM*         temp        = NULL;
    85     OWFint                  newcapacity = 0;
    86 
    87     OWF_ASSERT(array);
    88 
    89     DPRINT(("OWF_Array_Enlarge\n"));
    90     DPRINT(("  capacity = %d, length = %d\n", array->capacity, array->length));
    91 
    92     /*
    93     newcapacity = MAX(MINIMUM_CAPACITY,
    94                       array->capacity + (array->capacity >> 1));
    95     */
    96     newcapacity = MAX(MINIMUM_CAPACITY, 2 * array->capacity);
    97 
    98     temp = (OWF_ARRAY_ITEM*) realloc(array->items,
    99                                      sizeof(OWF_ARRAY_ITEM) * newcapacity);
   100 
   101     if (!temp)
   102     {
   103         return OWF_FALSE;
   104     }
   105 
   106     DPRINT(("  new capacity = %d\n", newcapacity));
   107 
   108     array->items = temp;
   109     array->capacity = newcapacity;
   110 
   111     return OWF_TRUE;
   112 }
   113 
   114 OWFboolean
   115 OWF_Array_AppendItem(OWF_ARRAY* array,
   116                       OWF_ARRAY_ITEM item)
   117 {
   118     OWF_ASSERT(array);
   119 
   120     DPRINT(("OWF_Array_AppendItem\n"));
   121 
   122     if (array->length >= array->capacity)
   123     {
   124         if (!OWF_Array_Enlarge(array))
   125         {
   126             return OWF_FALSE;
   127         }
   128     }
   129 
   130     DPRINT(("  item[%d] is now %p\n", array->length, item));
   131 
   132     array->items[array->length] = item;
   133     ++array->length;
   134 
   135     return OWF_TRUE;
   136 }
   137 
   138 OWFboolean
   139 OWF_Array_InsertItem(OWF_ARRAY*    array,
   140                      OWFint position,
   141                      OWF_ARRAY_ITEM item)
   142 {
   143     OWF_ASSERT(array);
   144 
   145     DPRINT(("bounds check\n"));
   146 
   147     /* check bounds */
   148     if (position < 0 || position > array->length)
   149     {
   150         return OWF_FALSE;
   151     }
   152 
   153     DPRINT(("enlarge\n"));
   154 
   155     if (array->length >= array->capacity)
   156     {
   157         if (!OWF_Array_Enlarge(array))
   158         {
   159             return OWF_FALSE;
   160         }
   161     }
   162 
   163     DPRINT(("new capacity = %d\n", array->capacity));
   164 
   165     /* shift forward (obs! memmove because src & dst overlap) */
   166     SHIFT(array, position, position + 1, array->length - position);
   167 
   168     DPRINT(("  item[%d] is now %p\n", array->length, item));
   169 
   170     /* put */
   171     array->items[position] = item;
   172     ++array->length;
   173 
   174     return OWF_TRUE;
   175 }
   176 
   177 OWF_ARRAY_ITEM
   178 OWF_Array_RemoveItem(OWF_ARRAY* array,
   179                      OWF_ARRAY_ITEM item)
   180 {
   181     OWFint                    ii = 0;
   182     OWF_ARRAY_ITEM            result = NULL;
   183 
   184     OWF_ASSERT(array);
   185 
   186     for (ii = 0; ii < array->length; ii++)
   187     {
   188         if (array->items[ii] == item)
   189         {
   190             result = OWF_Array_RemoveItemAt(array, ii);
   191             break;
   192         }
   193     }
   194     return result;
   195 }
   196 
   197 OWF_ARRAY_ITEM
   198 OWF_Array_RemoveItemAt(OWF_ARRAY* array,
   199                           OWFint position)
   200 {
   201     OWF_ARRAY_ITEM            result;
   202 
   203     OWF_ASSERT(array);
   204 
   205     /* check bounds */
   206     if (position < 0 || position >= array->length)
   207     {
   208         return NULL;
   209     }
   210 
   211     --array->length;
   212     result = array->items[position];
   213     SHIFT(array, position + 1, position, array->length - position);
   214 
   215     return result;
   216 }
   217 
   218 OWF_ARRAY_ITEM
   219 OWF_Array_GetItemAt(OWF_ARRAY* array,
   220                     OWFint position)
   221 {
   222     OWF_ASSERT(array);
   223 
   224     if (position < 0 || position >= array->length)
   225     {
   226         return NULL;
   227     }
   228 
   229     return array->items[position];
   230 }
   231 
   232 #ifdef __cplusplus
   233 }
   234 #endif
   235