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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
/****************************************************************************
**
** This file is part of GAP, a system for computational discrete algebra.
**
** Copyright of GAP belongs to its developers, whose names are too numerous
** to list here. Please refer to the COPYRIGHT file for details.
**
** SPDX-License-Identifier: GPL-2.0-or-later
**
** WARNING: This file should NOT be directly included. It is designed
** to instantiate a generic dynamic array type based on #defines that
** have to be set before including the file, and the file can be included
** repeatedly with different parameters.
*/
// Parameters:
//
// #define ELEM_TYPE type of elements
// #define COMPARE comparison function for elements (optional)
// #define ALLOC allocation function (optional)
// #define DEALLOC deallocation function (optional)
#define Array JOIN(ELEM_TYPE, Array)
#define FN(sym) JOIN(Array, sym)
#define JOIN(s1, s2) JOIN2(s1, s2)
#define JOIN2(s1, s2) s1##s2
#ifndef ALLOC
#define ALLOC(T, n) ((T *)malloc(sizeof(T) * (n)))
#endif
#ifndef DEALLOC
#define DEALLOC(p) (free(p))
#endif
typedef struct {
Int len, cap;
ELEM_TYPE * items;
} Array;
static inline Array * FN(Make)(Int cap)
{
Array * arr;
GAP_ASSERT(cap >= 0);
if (cap == 0)
cap = 1;
arr = ALLOC(Array, 1);
arr->cap = cap;
arr->len = 0;
arr->items = ALLOC(ELEM_TYPE, cap);
return arr;
}
static inline void FN(Delete)(Array * arr)
{
DEALLOC(arr->items);
DEALLOC(arr);
}
static inline void FN(ExpandTo)(Array * arr, Int minlen)
{
Int cap = arr->cap;
GAP_ASSERT(minlen >= 0);
if (minlen <= arr->cap)
return;
if (cap == 0)
cap = 1;
while (cap < minlen)
cap *= 2;
ELEM_TYPE * items = ALLOC(ELEM_TYPE, cap);
memcpy(items, arr->items, sizeof(ELEM_TYPE) * arr->len);
DEALLOC(arr->items);
arr->items = items;
arr->cap = cap;
}
static inline void FN(Shrink)(Array * arr)
{
if (arr->cap > arr->len) {
ELEM_TYPE * items = ALLOC(ELEM_TYPE, arr->len);
memcpy(items, arr->items, sizeof(ELEM_TYPE) * arr->len);
DEALLOC(arr->items);
arr->items = items;
arr->cap = arr->len;
}
}
static inline Array * FN(Clone)(Array * arr)
{
Array * clone = FN(Make)(arr->len);
clone->len = arr->len;
memcpy(clone->items, arr->items, sizeof(ELEM_TYPE) * arr->len);
return clone;
}
static inline void FN(SetLen)(Array * arr, Int len)
{
GAP_ASSERT(len <= arr->len);
if (len < arr->len)
arr->len = len;
}
static inline Int FN(Len)(Array * arr)
{
return arr->len;
}
static inline ELEM_TYPE FN(Get)(Array * arr, Int i)
{
GAP_ASSERT(i >= 0 && i < arr->len);
return arr->items[i];
}
static inline void FN(Put)(Array * arr, Int i, ELEM_TYPE item)
{
GAP_ASSERT(i >= 0 && i < arr->len);
arr->items[i] = item;
}
static inline void FN(Add)(Array * arr, ELEM_TYPE item)
{
FN(ExpandTo)(arr, arr->len + 1);
arr->items[arr->len++] = item;
}
#ifdef COMPARE
static inline void FN(Sort)(Array * arr)
{
Int len = arr->len;
if (len <= 1)
return;
ELEM_TYPE * in = ALLOC(ELEM_TYPE, len);
ELEM_TYPE * out = ALLOC(ELEM_TYPE, len);
memcpy(in, arr->items, sizeof(ELEM_TYPE) * len);
Int step = 1;
while (step < len) {
for (Int i = 0; i < len; i += step * 2) {
Int p = i, l = i, r = i + step, lmax = l + step, rmax = r + step;
if (rmax > len)
rmax = len;
if (lmax > len)
lmax = len;
while (l < lmax && r < rmax) {
int c = COMPARE(in[l], in[r]);
if (c < 0) {
out[p++] = in[l++];
}
else {
out[p++] = in[r++];
}
}
while (l < lmax) {
out[p++] = in[l++];
}
while (r < rmax) {
out[p++] = in[r++];
}
}
ELEM_TYPE * tmp = in;
in = out;
out = tmp;
step += step;
}
DEALLOC(arr->items);
DEALLOC(out);
arr->items = in;
arr->cap = len; // we allocated only len items for 'in'.
}
#endif
#undef Array
#undef FN
#undef JOIN
#undef JOIN2
#undef ELEM_TYPE
#undef COMPARE
#undef ALLOC
#undef DEALLOC
|