summaryrefslogtreecommitdiffstats
path: root/libcutils/array.c
blob: ff2c8ff12c0d3a52ea670aaf7b88c3e65ccf1974 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cutils/array.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

#define INITIAL_CAPACITY (4)

struct Array {
    void** contents;
    int size;
    int capacity;
};

Array* arrayCreate() {
    return calloc(1, sizeof(struct Array));
}

void arrayFree(Array* array) {
    assert(array != NULL);

    // Free internal array.
    free(array->contents);

    // Free the Array itself.
    free(array);
}

/** Returns 0 if successful, < 0 otherwise.. */
static int ensureCapacity(Array* array, int capacity) {
    int oldCapacity = array->capacity;
    if (capacity > oldCapacity) {
        int newCapacity = (oldCapacity == 0) ? INITIAL_CAPACITY : oldCapacity * 2;
    
        // Keep doubling capacity until we surpass necessary capacity. 
        while (newCapacity < capacity) {
            newCapacity *= 2;
        }
    
        void** newContents;
        if (array->contents == NULL) {
            // Allocate new array.
            newContents = malloc(newCapacity * sizeof(void*));
            if (newContents == NULL) {
                return -1;
            }
        } else {
            // Expand existing array.
            newContents = realloc(array->contents, sizeof(void*) * newCapacity);
            if (newContents == NULL) {
                return -1;
            }
        }

        array->capacity = newCapacity;
        array->contents = newContents;
    }

    return 0;
}

int arrayAdd(Array* array, void* pointer) {
    assert(array != NULL);
    int size = array->size;
    int result = ensureCapacity(array, size + 1);
    if (result < 0) {
        return result;
    }
    array->contents[size] = pointer;
    array->size++;
    return 0;
}

static inline void checkBounds(Array* array, int index) {
    assert(array != NULL);
    assert(index < array->size);
    assert(index >= 0);
}

void* arrayGet(Array* array, int index) {
    checkBounds(array, index);
    return array->contents[index];
}

void* arrayRemove(Array* array, int index) {
    checkBounds(array, index);

    void* pointer = array->contents[index];
    
    int newSize = array->size - 1;
    
    // Shift entries left.
    if (index != newSize) {
        memmove(array->contents + index, array->contents + index + 1, 
                (sizeof(void*)) * (newSize - index));
    }

    array->size = newSize;

    return pointer;
}

void* arraySet(Array* array, int index, void* pointer) {
    checkBounds(array, index);
    void* old = array->contents[index];
    array->contents[index] = pointer;
    return old;
}

int arraySetSize(Array* array, int newSize) {
    assert(array != NULL);
    assert(newSize >= 0);
   
    int oldSize = array->size;
    
    if (newSize > oldSize) {
        // Expand.
        int result = ensureCapacity(array, newSize);
        if (result < 0) {
            return result;
        }

        // Zero out new entries.
        memset(array->contents + sizeof(void*) * oldSize, 0,
                sizeof(void*) * (newSize - oldSize));
    }

    array->size = newSize;

    return 0;
}

int arraySize(Array* array) {
    assert(array != NULL);
    return array->size;
}

const void** arrayUnwrap(Array* array) {
    return array->contents;
}