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