| 1 |
|
| 2 |
// Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide. |
| 3 |
// All rights reserved. |
| 4 |
// |
| 5 |
// Redistribution and use in source and binary forms, with or without modification, |
| 6 |
// are permitted provided that the following conditions are met: |
| 7 |
// |
| 8 |
// 1. Redistributions of source code must retain the above copyright notice, |
| 9 |
// this list of conditions, and the following disclaimer. |
| 10 |
// |
| 11 |
// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 |
// this list of conditions, and the following disclaimer in the documentation |
| 13 |
// and/or other materials provided with the distribution. |
| 14 |
// |
| 15 |
// 3. Neither the names of the copyright holders nor the names of their contributors |
| 16 |
// may be used to endorse or promote products derived from this software without |
| 17 |
// specific prior written permission. |
| 18 |
// |
| 19 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 |
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 |
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 |
// THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
// SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 24 |
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR |
| 26 |
// TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS |
| 28 |
// SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES |
| 29 |
// OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE |
| 30 |
// ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES. |
| 31 |
// |
| 32 |
|
| 33 |
#include "xsarray.h" |
| 34 |
#include <stdlib.h> |
| 35 |
#include <string.h> |
| 36 |
#include "xsdebugcounters.h" |
| 37 |
|
| 38 |
/*! \class XsArray |
| 39 |
\brief Provides generic storage for data in an array and manipulation operations on that data |
| 40 |
*/ |
| 41 |
|
| 42 |
/*! \addtogroup cinterface C Interface |
| 43 |
@{ |
| 44 |
*/ |
| 45 |
|
| 46 |
/*! \cond NODOXYGEN */ |
| 47 |
#define elemSize(thisArray) (thisArray->m_descriptor->itemSize) |
| 48 |
#define elemAtX(b, i, thisArray) ((void*)(((char*) (b))+((i)*elemSize(thisArray)))) |
| 49 |
#define elemAt(b, i) elemAtX(b, i, thisArray) |
| 50 |
/*! \endcond */ |
| 51 |
|
| 52 |
#ifdef DOXYGEN |
| 53 |
/*! \brief Initializes the XsArray-derived object with space for \a count items and copies them from \a src |
| 54 |
\details This function initializes the object reserving \a count items in the buffer. \a count may |
| 55 |
be 0. If \a src is not 0, \a count items from \a src will be copied. |
| 56 |
\param count The number of items to reserve space for. When \a src is not NULL, thisArray is also the number of items copied from \a src |
| 57 |
\param src A pointer to an array of objects to copy, may be NULL, ignored when \a count is 0 |
| 58 |
*/ |
| 59 |
void XsArray_constructDerived(void* thisPtr, XsSize count, void const* src); |
| 60 |
#endif |
| 61 |
|
| 62 |
/*! \relates XsArray |
| 63 |
\brief Initializes the XsArray with space for \a count items and copies them from \a src |
| 64 |
\details This function initializes the object reserving \a count items in the buffer. \a count may |
| 65 |
be 0. If \a src is not 0, \a count items from \a src will be copied. |
| 66 |
\param descriptor The descriptor of the data in the list |
| 67 |
\param count The number of items to reserve space for. When \a src is not NULL, thisArray is also the number of items copied from \a src |
| 68 |
\param src A pointer to an array of objects to copy, may be NULL, ignored when \a count is 0 |
| 69 |
*/ |
| 70 |
void XsArray_construct(void* thisPtr, XsArrayDescriptor const* const descriptor, XsSize count, void const* src) |
| 71 |
{ |
| 72 |
XsArray* thisArray = (XsArray*) thisPtr; |
| 73 |
*((XsArrayDescriptor const**) &thisArray->m_descriptor) = descriptor; |
| 74 |
*((XsSize*) &thisArray->m_size) = count; |
| 75 |
|
| 76 |
if (thisArray->m_size) |
| 77 |
{ |
| 78 |
// init to size |
| 79 |
*((void**) &thisArray->m_data) = malloc(thisArray->m_size*elemSize(thisArray)); |
| 80 |
XsArray_incAllocCount(); |
| 81 |
|
| 82 |
// init the configurations |
| 83 |
if (src) |
| 84 |
{ |
| 85 |
if (thisArray->m_descriptor->rawCopy) |
| 86 |
{ |
| 87 |
thisArray->m_descriptor->rawCopy(thisArray->m_data, src, thisArray->m_size, thisArray->m_descriptor->itemSize); |
| 88 |
} |
| 89 |
else |
| 90 |
{ |
| 91 |
XsSize i; |
| 92 |
assert(thisArray->m_descriptor->itemCopyConstruct); |
| 93 |
for (i=0; i<thisArray->m_size; ++i) |
| 94 |
thisArray->m_descriptor->itemCopyConstruct(elemAt(thisArray->m_data, i), elemAt(src, i)); |
| 95 |
} |
| 96 |
} |
| 97 |
else if (thisArray->m_descriptor->itemConstruct) |
| 98 |
{ |
| 99 |
XsSize i; |
| 100 |
for (i=0; i<thisArray->m_size; ++i) |
| 101 |
thisArray->m_descriptor->itemConstruct(elemAt(thisArray->m_data, i)); |
| 102 |
} |
| 103 |
} |
| 104 |
else |
| 105 |
*((void**) &thisArray->m_data) = 0; |
| 106 |
*((XsSize*) &thisArray->m_reserved) = thisArray->m_size; |
| 107 |
*((XsSize*) &thisArray->m_flags) = XSDF_Managed; |
| 108 |
} |
| 109 |
|
| 110 |
/*! \relates XsArray |
| 111 |
\brief Initializes the XsArray with a copy of \a src |
| 112 |
\param src A pointer to the objects to copy. The object may be empty, but src may not be 0 |
| 113 |
*/ |
| 114 |
void XsArray_copyConstruct(void* thisPtr, void const* src) |
| 115 |
{ |
| 116 |
XsArray* thisArray = (XsArray*) thisPtr; |
| 117 |
XsArray const* srcArray = (XsArray const*) src; |
| 118 |
assert(srcArray); |
| 119 |
XsArray_construct(thisArray, srcArray->m_descriptor, srcArray->m_size, srcArray->m_data); |
| 120 |
} |
| 121 |
|
| 122 |
/*! \relates XsArray |
| 123 |
\brief Clears and frees memory allocated by the XsArray |
| 124 |
\note After XsArray_destruct is called, the object is empty but valid, |
| 125 |
ie. it can be used as if XsArray_construct has been called on it. |
| 126 |
*/ |
| 127 |
void XsArray_destruct(void* thisPtr) |
| 128 |
{ |
| 129 |
XsArray* thisArray = (XsArray*) thisPtr; |
| 130 |
if (thisArray->m_data && (thisArray->m_flags & XSDF_Managed)) |
| 131 |
{ |
| 132 |
XsSize i; |
| 133 |
// clear contents |
| 134 |
if (thisArray->m_descriptor->itemDestruct) |
| 135 |
for (i=0; i<thisArray->m_reserved; ++i) |
| 136 |
thisArray->m_descriptor->itemDestruct(elemAt(thisArray->m_data, i)); |
| 137 |
free((void*) thisArray->m_data); |
| 138 |
XsArray_incFreeCount(); |
| 139 |
} |
| 140 |
// init to 0 |
| 141 |
*((void**) &thisArray->m_data) = 0; |
| 142 |
*((XsSize*) &thisArray->m_size) = 0; |
| 143 |
*((XsSize*) &thisArray->m_reserved) = 0; |
| 144 |
*((XsSize*) &thisArray->m_flags) = (thisArray->m_flags & (XSDF_Managed | XSDF_FixedSize)); |
| 145 |
} |
| 146 |
|
| 147 |
/*! \relates XsArray |
| 148 |
\brief Reinitializes the XsArray with space for \a count items and copies them from \a src |
| 149 |
\details This function reinitializes the object reserving space for at least \a count items in the |
| 150 |
buffer. \a count may be 0. If \a src is not 0, \a count items will be copied from \a src. |
| 151 |
Previous data will be cleared automatically, but the reserved space will not be reduced. |
| 152 |
\param count the number of items in src |
| 153 |
\param src a pointer to an array of items |
| 154 |
\sa XsArray_reserve |
| 155 |
*/ |
| 156 |
void XsArray_assign(void* thisPtr, XsSize count, void const* src) |
| 157 |
{ |
| 158 |
XsSize i; |
| 159 |
XsArray* thisArray = (XsArray*) thisPtr; |
| 160 |
|
| 161 |
// check if we need to reallocate |
| 162 |
if (count > thisArray->m_reserved) |
| 163 |
{ |
| 164 |
assert(thisArray->m_flags & XSDF_Managed); |
| 165 |
|
| 166 |
if (thisArray->m_data) |
| 167 |
XsArray_destruct(thisArray); |
| 168 |
XsArray_construct(thisArray, thisArray->m_descriptor, count, src); |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
// no reallocation necessary, clear excess objects |
| 173 |
if (thisArray->m_descriptor->itemDestruct) |
| 174 |
for (i=count; i<thisArray->m_size; ++i) |
| 175 |
thisArray->m_descriptor->itemDestruct(elemAt(thisArray->m_data, i)); |
| 176 |
|
| 177 |
if (src) |
| 178 |
{ |
| 179 |
if (thisArray->m_descriptor->rawCopy) |
| 180 |
{ |
| 181 |
thisArray->m_descriptor->rawCopy(thisArray->m_data, src, count, thisArray->m_descriptor->itemSize); |
| 182 |
} |
| 183 |
else |
| 184 |
{ |
| 185 |
for (i=0; i<count; ++i) |
| 186 |
thisArray->m_descriptor->itemCopy(elemAt(thisArray->m_data, i), elemAt(src, i)); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
*((XsSize*) &thisArray->m_size) = count; |
| 191 |
} |
| 192 |
|
| 193 |
/*! \relates XsArray |
| 194 |
\brief Resizes the existing list to \a count items |
| 195 |
\details This function will keep the data of the remaining items intact. |
| 196 |
\param count the number of items the list should have |
| 197 |
\sa XsArray_reserve \sa XsArray_assign |
| 198 |
*/ |
| 199 |
void XsArray_resize(void* thisPtr, XsSize count) |
| 200 |
{ |
| 201 |
XsArray* thisArray = (XsArray*) thisPtr; |
| 202 |
if (thisArray->m_size == count) |
| 203 |
return; |
| 204 |
if (thisArray->m_size == 0) |
| 205 |
{ |
| 206 |
XsArray_assign(thisArray, count, 0); |
| 207 |
return; |
| 208 |
} |
| 209 |
if (count < thisArray->m_size) |
| 210 |
{ |
| 211 |
XsArray_erase(thisArray, count, thisArray->m_size - count); |
| 212 |
return; |
| 213 |
} |
| 214 |
if (count > thisArray->m_reserved) |
| 215 |
XsArray_reserve(thisArray, count); |
| 216 |
*((XsSize*) &thisArray->m_size) = count; |
| 217 |
} |
| 218 |
|
| 219 |
/*! \relates XsArray |
| 220 |
\brief Reserves space for \a count items |
| 221 |
\details This function reserves space for at least \a count items. The function will retain the |
| 222 |
current data in the list. |
| 223 |
\param count The number of items to reserve space for. When set to 0 or 1, the buffer is resized to the |
| 224 |
minimum required size to contain the current contents. Otherwise reserving data will only |
| 225 |
expand the buffer, never shrink it. |
| 226 |
\sa XsArray_assign |
| 227 |
*/ |
| 228 |
void XsArray_reserve(void* thisPtr, XsSize count) |
| 229 |
{ |
| 230 |
XsArray* thisArray = (XsArray*) thisPtr; |
| 231 |
XsArray tmp = { 0, thisArray->m_size, 0, XSDF_Managed, thisArray->m_descriptor }; |
| 232 |
XsSize i; |
| 233 |
|
| 234 |
if (count && count <= thisArray->m_reserved) |
| 235 |
return; |
| 236 |
|
| 237 |
if (count < thisArray->m_size) |
| 238 |
count = thisArray->m_size; |
| 239 |
|
| 240 |
if (count == thisArray->m_reserved) |
| 241 |
return; |
| 242 |
|
| 243 |
if (!(thisArray->m_flags & XSDF_Managed)) |
| 244 |
{ |
| 245 |
// attempting this on an unmanaged list is ignored silently |
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
if (!count) |
| 250 |
{ |
| 251 |
XsArray_destruct(thisArray); |
| 252 |
return; |
| 253 |
} |
| 254 |
|
| 255 |
*((XsSize*) &tmp.m_reserved) = count; |
| 256 |
|
| 257 |
// init to size |
| 258 |
*((void**) &tmp.m_data) = malloc(tmp.m_reserved*elemSize(thisArray)); |
| 259 |
assert(tmp.m_data); |
| 260 |
if (!tmp.m_data) |
| 261 |
return; |
| 262 |
XsArray_incAllocCount(); |
| 263 |
|
| 264 |
if (thisArray->m_descriptor->itemConstruct) |
| 265 |
for (i=0; i<tmp.m_reserved; ++i) |
| 266 |
thisArray->m_descriptor->itemConstruct(elemAt(tmp.m_data, i)); |
| 267 |
|
| 268 |
for (i=0; i<thisArray->m_size; ++i) |
| 269 |
thisArray->m_descriptor->itemSwap(elemAt(thisArray->m_data, i), elemAt(tmp.m_data, i)); |
| 270 |
|
| 271 |
XsArray_destruct(thisArray); |
| 272 |
XsArray_swap(thisArray, &tmp); |
| 273 |
} |
| 274 |
|
| 275 |
/*! \relates XsArray |
| 276 |
\brief Copy the contents of \a src to thisArray */ |
| 277 |
void XsArray_copy(void* thisPtr, void const* src) |
| 278 |
{ |
| 279 |
XsArray* thisArray = (XsArray*) thisPtr; |
| 280 |
XsArray const* srcArray = (XsArray const*) src; |
| 281 |
|
| 282 |
if (srcArray == thisArray) |
| 283 |
{ |
| 284 |
return; |
| 285 |
} |
| 286 |
XsArray_assign(thisArray, srcArray->m_size, srcArray->m_data); |
| 287 |
} |
| 288 |
|
| 289 |
/*! \relates XsArray |
| 290 |
\brief Appends the \a other list to thisArray list |
| 291 |
\param other The list to append to thisArray list. \a other may point to thisArray list |
| 292 |
*/ |
| 293 |
void XsArray_append(void* thisPtr, void const* other) |
| 294 |
{ |
| 295 |
XsSize i; |
| 296 |
XsArray* thisArray = (XsArray*) thisPtr; |
| 297 |
XsArray const* otherArray = (XsArray const*) other; |
| 298 |
|
| 299 |
if (otherArray->m_size == 0) |
| 300 |
return; |
| 301 |
|
| 302 |
if (otherArray == thisArray) |
| 303 |
{ |
| 304 |
if (thisArray->m_size + thisArray->m_size > thisArray->m_reserved) |
| 305 |
XsArray_reserve(thisArray, thisArray->m_size + thisArray->m_size); // maybe reserve more here? |
| 306 |
|
| 307 |
if (thisArray->m_descriptor->rawCopy) |
| 308 |
{ |
| 309 |
thisArray->m_descriptor->rawCopy(elemAt(thisArray->m_data, thisArray->m_size), thisArray->m_data, thisArray->m_size, thisArray->m_descriptor->itemSize); |
| 310 |
} |
| 311 |
else |
| 312 |
{ |
| 313 |
for (i=0; i<thisArray->m_size; ++i) |
| 314 |
thisArray->m_descriptor->itemCopy(elemAt(thisArray->m_data, i+thisArray->m_size), elemAt(thisArray->m_data, i)); |
| 315 |
} |
| 316 |
|
| 317 |
*((XsSize*) &thisArray->m_size) = thisArray->m_size + thisArray->m_size; |
| 318 |
return; |
| 319 |
} |
| 320 |
|
| 321 |
if (thisArray->m_size == 0) |
| 322 |
{ |
| 323 |
XsArray_copy(thisArray, otherArray); |
| 324 |
return; |
| 325 |
} |
| 326 |
|
| 327 |
if (thisArray->m_size + otherArray->m_size > thisArray->m_reserved) |
| 328 |
XsArray_reserve(thisArray, thisArray->m_size + otherArray->m_size); // maybe reserve more here? |
| 329 |
|
| 330 |
if (thisArray->m_descriptor->rawCopy) |
| 331 |
{ |
| 332 |
thisArray->m_descriptor->rawCopy(elemAt(thisArray->m_data, thisArray->m_size), otherArray->m_data, otherArray->m_size, thisArray->m_descriptor->itemSize); |
| 333 |
} |
| 334 |
else |
| 335 |
{ |
| 336 |
for (i=0; i<otherArray->m_size; ++i) |
| 337 |
thisArray->m_descriptor->itemCopy(elemAt(thisArray->m_data, i+thisArray->m_size), elemAt(otherArray->m_data, i)); |
| 338 |
} |
| 339 |
|
| 340 |
*((XsSize*) &thisArray->m_size) = thisArray->m_size + otherArray->m_size; |
| 341 |
} |
| 342 |
|
| 343 |
/*! \relates XsArray |
| 344 |
\brief Insert \a count items from \a src at \a index in the array |
| 345 |
\param index The index to use for inserting. Anything beyond the end of the array (ie. -1) will |
| 346 |
append to the actual end of the array. |
| 347 |
\param count The number of items to insert |
| 348 |
\param src The items to insert, may not be 0 unless count is 0 |
| 349 |
*/ |
| 350 |
void XsArray_insert(void* thisPtr, XsSize index, XsSize count, void const* src) |
| 351 |
{ |
| 352 |
XsSize s; |
| 353 |
XsArray* thisArray = (XsArray*) thisPtr; |
| 354 |
XsSize i,d = count; |
| 355 |
if (thisArray->m_size + count > thisArray->m_reserved) |
| 356 |
XsArray_reserve(thisArray, ((thisArray->m_size + count)*3)/2); // we reserve 50% more space here to handle multiple sequential insertions efficiently |
| 357 |
|
| 358 |
// fix index if beyond end of list |
| 359 |
if (index > thisArray->m_size) |
| 360 |
index = thisArray->m_size; |
| 361 |
|
| 362 |
// move items to the back by swapping |
| 363 |
for (i = thisArray->m_size-1; i >= index && i < thisArray->m_size; --i) |
| 364 |
thisArray->m_descriptor->itemSwap(elemAt(thisArray->m_data, i), elemAt(thisArray->m_data, i+d)); |
| 365 |
|
| 366 |
// copy items to the array |
| 367 |
if (thisArray->m_descriptor->rawCopy) |
| 368 |
{ |
| 369 |
thisArray->m_descriptor->rawCopy(elemAt(thisArray->m_data, index), src, count, thisArray->m_descriptor->itemSize); |
| 370 |
} |
| 371 |
else |
| 372 |
{ |
| 373 |
for (s = 0; s < count; ++s) |
| 374 |
thisArray->m_descriptor->itemCopy(elemAt(thisArray->m_data, s+index), elemAt(src, s)); |
| 375 |
} |
| 376 |
|
| 377 |
// update size |
| 378 |
*((XsSize*) &thisArray->m_size) = thisArray->m_size + count; |
| 379 |
} |
| 380 |
|
| 381 |
/*! \relates XsArray |
| 382 |
\brief Swap the contents of \a a with those of \a b |
| 383 |
\details Where possible, the pointers and administrative values are swapped. If for some reason |
| 384 |
thisArray is not possible, the lists are swapped one element at a time. |
| 385 |
\param a The list to swap with \a b |
| 386 |
\param b The list to swap with \a a |
| 387 |
*/ |
| 388 |
void XsArray_swap(void* a, void* b) |
| 389 |
{ |
| 390 |
XsArray* aArray = (XsArray*) a; |
| 391 |
XsArray* bArray = (XsArray*) b; |
| 392 |
|
| 393 |
if (!aArray->m_data && !bArray->m_data) |
| 394 |
return; |
| 395 |
if ((!aArray->m_data || (aArray->m_flags & XSDF_Managed)) && (!bArray->m_data || (bArray->m_flags & XSDF_Managed))) |
| 396 |
{ |
| 397 |
// administrative swap |
| 398 |
XsArray tmp; |
| 399 |
*((void**) &tmp.m_data) = aArray->m_data; |
| 400 |
*((void**) &aArray->m_data) = bArray->m_data; |
| 401 |
*((void**) &bArray->m_data) = tmp.m_data; |
| 402 |
|
| 403 |
*((XsSize*) &tmp.m_size) = aArray->m_size; |
| 404 |
*((XsSize*) &aArray->m_size) = bArray->m_size; |
| 405 |
*((XsSize*) &bArray->m_size) = tmp.m_size; |
| 406 |
|
| 407 |
*((XsSize*) &tmp.m_reserved) = aArray->m_reserved; |
| 408 |
*((XsSize*) &aArray->m_reserved) = bArray->m_reserved; |
| 409 |
*((XsSize*) &bArray->m_reserved) = tmp.m_reserved; |
| 410 |
|
| 411 |
*((XsSize*) &tmp.m_flags) = aArray->m_flags; |
| 412 |
*((XsSize*) &aArray->m_flags) = bArray->m_flags; |
| 413 |
*((XsSize*) &bArray->m_flags) = tmp.m_flags; |
| 414 |
} else { |
| 415 |
// elementwise swap |
| 416 |
XsSize i; |
| 417 |
assert(aArray->m_size == bArray->m_size); |
| 418 |
for (i = 0; i < aArray->m_size; ++i) |
| 419 |
aArray->m_descriptor->itemSwap(elemAtX(aArray->m_data, i, aArray), elemAtX(bArray->m_data, i, bArray)); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
/*! \relates XsArray |
| 424 |
\brief Removes a \a count items from the list starting at \a index |
| 425 |
*/ |
| 426 |
void XsArray_erase(void* thisPtr, XsSize index, XsSize count) |
| 427 |
{ |
| 428 |
XsSize i, newCount; |
| 429 |
XsArray* thisArray = (XsArray*) thisPtr; |
| 430 |
|
| 431 |
if (count == 0 || index >= thisArray->m_size) |
| 432 |
return; |
| 433 |
|
| 434 |
if (count+index > thisArray->m_size) |
| 435 |
count = thisArray->m_size - index; |
| 436 |
|
| 437 |
newCount = thisArray->m_size - count; |
| 438 |
|
| 439 |
// move items into the gap by swapping |
| 440 |
for (i = index; i < newCount; ++i) |
| 441 |
thisArray->m_descriptor->itemSwap(elemAt(thisArray->m_data, i), elemAt(thisArray->m_data, i+count)); |
| 442 |
|
| 443 |
*((XsSize*) &thisArray->m_size) = newCount; |
| 444 |
} |
| 445 |
|
| 446 |
/*! \relates XsArray |
| 447 |
\brief Returns non-zero if the lists are different, 0 if they're equal |
| 448 |
\details This function compares the two lists in-order |
| 449 |
\param a The left hand side of the comparison |
| 450 |
\param b The right hand side of the comparison |
| 451 |
\return -1 if \a a is smaller in some way than \a b, 1 if it is larger in some way |
| 452 |
and 0 if both lists are equal. Please note that not all lists have items that can be accurately |
| 453 |
tested for less than or greater than, but can be tested for (in-)equality. So the sign of the |
| 454 |
return value should be treated with knowledge of the data type in mind. |
| 455 |
\sa XsArray_compareSet \sa XsArray_comparePredicate |
| 456 |
*/ |
| 457 |
int XsArray_compare(void const* a, void const* b) |
| 458 |
{ |
| 459 |
XsSize i; |
| 460 |
XsArray const* aArray = (XsArray const*) a; |
| 461 |
XsArray const* bArray = (XsArray const*) b; |
| 462 |
|
| 463 |
if (aArray == bArray) |
| 464 |
return 0; |
| 465 |
|
| 466 |
if (aArray->m_size != bArray->m_size) |
| 467 |
return (aArray->m_size < bArray->m_size)?-1:1; |
| 468 |
|
| 469 |
assert(aArray->m_descriptor->itemCompare); |
| 470 |
// we could theoretically only check the sizes and ignore the element-comparison in thisArray case |
| 471 |
for (i = 0; i < aArray->m_size; ++i) // loop over all elements of the lists |
| 472 |
{ |
| 473 |
int r = aArray->m_descriptor->itemCompare(elemAtX(aArray->m_data, i, aArray), elemAtX(bArray->m_data, i, bArray)); |
| 474 |
if (r) |
| 475 |
return r; |
| 476 |
} |
| 477 |
return 0; |
| 478 |
} |
| 479 |
|
| 480 |
/*! \relates XsArray |
| 481 |
\brief Returns non-zero if the lists are different, 0 if they're equal |
| 482 |
\details This function compares the two lists in-order |
| 483 |
\param a The left hand side of the comparison |
| 484 |
\param b The right hand side of the comparison |
| 485 |
\param predicate The custom comparison predicate to use. It should return 0 when the items are considered equal (see return values below). |
| 486 |
\return -1 if \a a is smaller in some way than \a b, 1 if it is larger in some way |
| 487 |
and 0 if both lists are equal. Please note that not all lists have items that can be accurately |
| 488 |
tested for less than or greater than, but can be tested for (in-)equality. So the sign of the |
| 489 |
return value should be treated with knowledge of the data type in mind. |
| 490 |
\sa XsArray_compareSet \sa XsArray_compare |
| 491 |
*/ |
| 492 |
int XsArray_comparePredicate(void const* a, void const* b, XsArrayItemCompareFunc predicate) |
| 493 |
{ |
| 494 |
XsSize i; |
| 495 |
XsArray const* aArray = (XsArray const*) a; |
| 496 |
XsArray const* bArray = (XsArray const*) b; |
| 497 |
|
| 498 |
if (aArray == bArray) |
| 499 |
return 0; |
| 500 |
|
| 501 |
if (aArray->m_size != bArray->m_size) |
| 502 |
return (aArray->m_size < bArray->m_size)?-1:1; |
| 503 |
|
| 504 |
assert(predicate); |
| 505 |
// we could theoretically only check the sizes and ignore the element-comparison in thisArray case |
| 506 |
for (i = 0; i < aArray->m_size; ++i) // loop over all elements of the lists |
| 507 |
{ |
| 508 |
int r = predicate(elemAtX(aArray->m_data, i, aArray), elemAtX(bArray->m_data, i, bArray)); |
| 509 |
if (r) |
| 510 |
return r; |
| 511 |
} |
| 512 |
return 0; |
| 513 |
} |
| 514 |
|
| 515 |
/*! \relates XsArray |
| 516 |
\brief Returns -1 if \a a is smaller in some way than \a b, 1 if it is larger in some way and 0 if both lists are equal |
| 517 |
\details This function compares the two lists out-of-order |
| 518 |
\param a The left hand side of the comparison |
| 519 |
\param b The right hand side of the comparison |
| 520 |
\return -1 if \a a is smaller in some way than \a b, 1 if it is larger in some way |
| 521 |
\note It is assumed that both arguments are sets i.e. they contain no duplicates. It is the responsibility of the caller to provide sets as arguments |
| 522 |
and 0 if both lists are equal. Please note that not all lists have items that can be accurately |
| 523 |
tested for less than or greater than, but can be tested for (in-)equality. So the sign of the |
| 524 |
return value should be treated with knowledge of the data type in mind. |
| 525 |
\sa XsArray_compare \sa XsArray_comparePredicate |
| 526 |
*/ |
| 527 |
int XsArray_compareSet(void const* a, void const* b) |
| 528 |
{ |
| 529 |
XsSize n, m; |
| 530 |
XsArray const* aArray = (XsArray const*) a; |
| 531 |
XsArray const* bArray = (XsArray const*) b; |
| 532 |
|
| 533 |
if (aArray == bArray) |
| 534 |
return 0; |
| 535 |
|
| 536 |
if (aArray->m_size != bArray->m_size) |
| 537 |
return (aArray->m_size < bArray->m_size)?-1:1; |
| 538 |
|
| 539 |
for (n = 0; n < aArray->m_size; ++n) // loop over all elements of list aArray |
| 540 |
{ |
| 541 |
int found = 0; |
| 542 |
for (m = 0; m < bArray->m_size; ++m) // loop over all elements of list bArray |
| 543 |
{ |
| 544 |
if (aArray->m_descriptor->itemCompare(elemAtX(aArray->m_data, n, aArray), elemAtX(bArray->m_data, m, bArray)) == 0) |
| 545 |
{ |
| 546 |
found = 1; |
| 547 |
break; |
| 548 |
} |
| 549 |
} |
| 550 |
if (!found) |
| 551 |
return -1; |
| 552 |
} |
| 553 |
return 0; |
| 554 |
} |
| 555 |
|
| 556 |
/*! \relates XsArray |
| 557 |
\brief Returns the index of \a needle in the list or -1 if it wasn't found |
| 558 |
\details The search does not assume any kind of ordering of the items so in a worst-case scenario it |
| 559 |
will go through the entire list. |
| 560 |
\param needle A pointer to the value to search for |
| 561 |
\returns The index of where \a needle was found or -1 if it wasn't found. |
| 562 |
*/ |
| 563 |
int XsArray_find(void const* thisPtr, void const* needle) |
| 564 |
{ |
| 565 |
XsSize i; |
| 566 |
XsArray const* thisArray = (XsArray const*) thisPtr; |
| 567 |
|
| 568 |
assert(thisArray->m_descriptor->itemCompare); |
| 569 |
// we could theoretically only check the sizes and ignore the element-comparison in thisArray case |
| 570 |
for (i = 0; i < thisArray->m_size; ++i) // loop over all elements of the lists |
| 571 |
if (!thisArray->m_descriptor->itemCompare(elemAt(thisArray->m_data, i), needle)) |
| 572 |
return (int) i; |
| 573 |
return -1; |
| 574 |
} |
| 575 |
|
| 576 |
/*! \relates XsArray |
| 577 |
\brief Returns the index of \a needle in the list or -1 if it wasn't found |
| 578 |
\details The search does not assume any kind of ordering of the items so in a worst-case scenario it |
| 579 |
will go through the entire list. It will use the supplied predicate to match items against \a needle. |
| 580 |
\param needle A pointer to the value to search for |
| 581 |
\param predicate A compare function that returns 0 if the values are equal. The arguments are (const void) |
| 582 |
pointers to items, supplied as such: predicate(item, needle) |
| 583 |
\returns The index of where \a needle was found or -1 if it wasn't found. |
| 584 |
*/ |
| 585 |
int XsArray_findPredicate(void const* thisPtr, void const* needle, XsArrayItemCompareFunc predicate) |
| 586 |
{ |
| 587 |
XsSize i; |
| 588 |
XsArray const* thisArray = (XsArray const*) thisPtr; |
| 589 |
|
| 590 |
assert(predicate); |
| 591 |
// we could theoretically only check the sizes and ignore the element-comparison in thisArray case |
| 592 |
for (i = 0; i < thisArray->m_size; ++i) // loop over all elements of the lists |
| 593 |
if (!predicate(elemAt(thisArray->m_data, i), needle)) |
| 594 |
return (int) i; |
| 595 |
return -1; |
| 596 |
} |
| 597 |
|
| 598 |
/*! \relates XsArray |
| 599 |
\brief Returns a pointer to the item at the supplied \a index or a null pointer if it is out of bounds |
| 600 |
\param index The index of the item to return |
| 601 |
\returns A pointer to the item or NULL if \a index is out of bounds |
| 602 |
*/ |
| 603 |
void const* XsArray_at(void const* thisPtr, XsSize index) |
| 604 |
{ |
| 605 |
XsArray const* thisArray = (XsArray const*) thisPtr; |
| 606 |
if (index >= thisArray->m_size) |
| 607 |
return 0; |
| 608 |
return elemAt(thisArray->m_data, index); |
| 609 |
} |
| 610 |
|
| 611 |
/*! \relates XsArray |
| 612 |
\brief Returns a pointer to the item at the supplied \a index or a null pointer if it is out of bounds |
| 613 |
\param index The index of the item to return |
| 614 |
\returns A pointer to the item or NULL if \a index is out of bounds |
| 615 |
*/ |
| 616 |
void* XsArray_atIndex(void* thisPtr, XsSize index) |
| 617 |
{ |
| 618 |
XsArray* thisArray = (XsArray*) thisPtr; |
| 619 |
if (index >= thisArray->m_size) |
| 620 |
return 0; |
| 621 |
return elemAt(thisArray->m_data, index); |
| 622 |
} |
| 623 |
|
| 624 |
/*! \relates XsArray |
| 625 |
\brief Removes duplicate entries from the array, keeping only the first instance of each value |
| 626 |
*/ |
| 627 |
void XsArray_removeDuplicates(void* thisPtr) |
| 628 |
{ |
| 629 |
XsSize i,j; |
| 630 |
XsArray* thisArray = (XsArray*) thisPtr; |
| 631 |
if (thisArray->m_size > 1) |
| 632 |
{ |
| 633 |
for (i = 0; i < thisArray->m_size-1; ++i) |
| 634 |
{ |
| 635 |
for (j = thisArray->m_size-1; j > i; --j) |
| 636 |
{ |
| 637 |
if (!thisArray->m_descriptor->itemCompare(elemAt(thisArray->m_data, i), elemAt(thisArray->m_data, j))) |
| 638 |
{ |
| 639 |
XsArray_erase(thisPtr, j, 1); |
| 640 |
} |
| 641 |
} |
| 642 |
} |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
/*! \relates XsArray |
| 647 |
\brief Removes duplicate entries from the array, keeping only the first instance of each value |
| 648 |
\param predicate A compare function that returns 0 if the values are equal. The arguments are (const void) |
| 649 |
*/ |
| 650 |
void XsArray_removeDuplicatesPredicate(void* thisPtr, XsArrayItemCompareFunc predicate) |
| 651 |
{ |
| 652 |
XsSize i,j; |
| 653 |
XsArray* thisArray = (XsArray*) thisPtr; |
| 654 |
if (thisArray->m_size > 1) |
| 655 |
{ |
| 656 |
for (i = 0; i < thisArray->m_size-1; ++i) |
| 657 |
{ |
| 658 |
for (j = thisArray->m_size-1; j > i; --j) |
| 659 |
{ |
| 660 |
if (!predicate(elemAt(thisArray->m_data, i), elemAt(thisArray->m_data, j))) |
| 661 |
{ |
| 662 |
XsArray_erase(thisPtr, j, 1); |
| 663 |
} |
| 664 |
} |
| 665 |
} |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
/*! \relates XsArray |
| 670 |
\brief Returns whether the array is empty. |
| 671 |
\details This differs slightly from a straight check for size() != 0 in that it also works for fixed-size |
| 672 |
XsArrays. |
| 673 |
\returns true if the array is empty. |
| 674 |
*/ |
| 675 |
int XsArray_empty(void const* thisPtr) |
| 676 |
{ |
| 677 |
XsArray const* thisArray = (XsArray const*) thisPtr; |
| 678 |
return (thisArray->m_size == 0) || (thisArray->m_data == 0) || (thisArray->m_flags & XSDF_Empty); |
| 679 |
} |
| 680 |
|
| 681 |
/*! \relates XsArray |
| 682 |
\brief Copies items optimized in a direct way |
| 683 |
\details This function copies items directly, using memcpy. This works for simple types only. |
| 684 |
\param to Pointer to array to copy to. |
| 685 |
\param from Pointer to array to copy from. |
| 686 |
\param count The number of items to copy. |
| 687 |
\param iSize The size of an individual item |
| 688 |
*/ |
| 689 |
void XsArray_rawCopy(void* to, void const* from, XsSize count, XsSize iSize) |
| 690 |
{ |
| 691 |
memcpy(to, from, count*iSize); |
| 692 |
} |
| 693 |
|
| 694 |
/*! \relates XsArray |
| 695 |
\brief Sorts the array using the \a itemCompare func in the XsArrayDescriptor |
| 696 |
\details This function sorts using qsort |
| 697 |
*/ |
| 698 |
void XsArray_sort(void* thisPtr) |
| 699 |
{ |
| 700 |
XsArray* thisArray = (XsArray*) thisPtr; |
| 701 |
qsort(thisArray->m_data, thisArray->m_size, thisArray->m_descriptor->itemSize, thisArray->m_descriptor->itemCompare); |
| 702 |
} |
| 703 |
|
| 704 |
/*! \relates XsArray |
| 705 |
\brief Reverses the contents of the array by repeatedly using the \a itemSwap func in the XsArrayDescriptor |
| 706 |
\details This reverses the contents in-place |
| 707 |
*/ |
| 708 |
void XsArray_reverse(void* thisPtr) |
| 709 |
{ |
| 710 |
XsSize i, half; |
| 711 |
XsArray* thisArray = (XsArray*) thisPtr; |
| 712 |
half = thisArray->m_size >> 1; |
| 713 |
for (i = 0; i < half; ++i) |
| 714 |
thisArray->m_descriptor->itemSwap(elemAt(thisArray->m_data, i), elemAt(thisArray->m_data, (thisArray->m_size-1)-i)); |
| 715 |
} |
| 716 |
|
| 717 |
/*! @} */ |