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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
|
/*
* ara.c -- a resizable array
*
* Copyright 2022 EPIC Software Labs
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. All redistributions, whether of source code or in binary form must
* retain the the above copyright notice, the above paragraph (the one
* permitting redistribution), this list of conditions, and the following
* disclaimer.
* 2. The names of the author(s) may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "irc.h"
#include "ara.h"
#include "ircaux.h"
#include "output.h"
static void ara_insert (ara *a, int location);
static void ara_delete (ara *a, int location);
static void ara_enlarge_list (ara *a, size_t new_size);
/*
* Make 'location' available, by moving (location, end) to (location+1, end+1)
* Does not change (0, location - 1)
* Changes (location) to NULL
*
* If 'a' is "full" it is enlarged by 10 spots first.
* If 'location' is invalid
*/
static void ara_insert (ara *a, int location)
{
size_t i;
if (location < 0 || (size_t)location >= a->size)
return;
else if (a->size == 0 || a->list == NULL)
ara_enlarge_list(a, a->size + 10);
else if (a->type == ARA_TYPE_DATA && a->list[a->size - 1].d != NULL)
ara_enlarge_list(a, a->size + 10);
else if (a->list[a->size - 1].f != NULL)
ara_enlarge_list(a, a->size + 10);
for (i = a->size; i > (size_t)location; i--)
a->list[i] = a->list[i - 1];
i = location;
if (a->type == ARA_TYPE_DATA)
a->list[i].d = NULL;
else
a->list[i].f = NULL;
}
/*
* Fill in a gap at 'location' by moving (location + 1, end) to (location, end-1)
* Does not change (0, location - 1)
* Obviously it overwites (location)
*/
static void ara_delete (ara *a, int location)
{
if (location < 0 || (size_t)location >= a->size)
return;
if (a->size == 0 || a->list == NULL)
return;
/* XXX TODO XXX */
}
static void ara_enlarge_list (ara *a, size_t new_size)
{
size_t r;
AraItem *new_list;
AraItem *old_list;
new_list = (AraItem *)new_malloc(sizeof(AraItem) * new_size);
if (a->type == ARA_TYPE_DATA)
{
for (r = 0; r < a->size; r++)
new_list[r].d = a->list[r].d;
for (r = a->size; r < new_size; r++)
new_list[r].d = NULL;
}
else
{
for (r = 0; r < a->size; r++)
new_list[r].f = a->list[r].f;
for (r = a->size; r < new_size; r++)
new_list[r].f = NULL;
}
old_list = a->list;
a->list = new_list;
a->size = new_size;
if (old_list)
new_free((char **)&old_list);
}
/*
* Copy 'one' to 'two'.
*/
static void ara_copy (ara *one, ara *two)
{
size_t i;
if (two->size < one->size)
ara_enlarge_list(two, one->size + 10);
for (i = 0; i < one->size; i++)
two[i] = one[i];
}
void init_ara (ara *a, int type, size_t maximum_location)
{
a->list = NULL;
a->size = 0;
a->type = type;
ara_enlarge_list(a, maximum_location + 1);
}
/*
* Set the 'location'th element in 'a' to 'data'
* If errcode is not NULL:
* 1 - The value was successfully set
* 0 - The value was not successfully set because there was already a value there
* -1 - 'a' was NULL or the value of 'location' was invalid (not successful)
*/
void ara_set_item (ara *a, int location, AraItem data, int *errorcode)
{
size_t idx;
if (!a || location < 0)
{
*errorcode = -1;
return;
}
idx = (size_t)location;
if (idx >= a->size)
ara_enlarge_list(a, location + 10);
if (a->type == ARA_TYPE_DATA)
{
if (a->list[idx].d != NULL)
{
*errorcode = 0;
}
else
{
a->list[idx].d = data.d;
*errorcode = 1;
}
}
else
{
if (a->list[idx].f != NULL)
{
*errorcode = 0;
}
else
{
a->list[idx].f = data.f;
*errorcode = 1;
}
}
}
/*
* Return the 'location'th element in 'a'
* If errcode is not NULL:
* 1 - The value 'location' is valid and contains a value (returned)
* 0 - The value 'location' is valid but it had no value (returned NULL)
* -1 - 'a' was NULL or the value of 'location' was invalid (returned NULL)
*/
AraItem ara_get_item (ara *a, int location, int *errorcode)
{
size_t idx;
AraItem nothing;
if (!a || location < 0 || (size_t)location >= a->size)
{
*errorcode = -1;
if (!a || a->type == ARA_TYPE_DATA)
nothing.d = NULL;
else
nothing.f = NULL;
return nothing;
}
idx = (size_t)location;
if (a->type == ARA_TYPE_DATA)
{
if (a->list[idx].d == NULL)
*errorcode = 0;
else
*errorcode = 1;
}
else
{
if (a->list[idx].f == NULL)
*errorcode = 0;
else
*errorcode = 1;
}
return a->list[idx];
}
/*
* Set the 'location'th element in 'a' to 'data' and return the previous value
* If errcode is not NULL:
* 1 - The value was successfully updated (previous value returned)
* 0 - <Reserved>
* -1 - 'a' was NULL or the value of 'location' was invalid
*/
AraItem ara_update_item (ara *a, int location, AraItem data, int *errcode)
{
size_t idx;
AraItem retval;
if (!a || location < 0 || (size_t)location >= a->size)
{
*errcode = -1;
if (!a || a->type == ARA_TYPE_DATA)
retval.d = NULL;
else
retval.f = NULL;
return retval;
}
idx = (size_t)location;
if (a->type == ARA_TYPE_DATA)
{
retval.d = a->list[idx].d;
a->list[idx].d = data.d;
}
else
{
retval.f = a->list[idx].f;
a->list[idx].f = data.f;
}
*errcode = 1;
return retval;
}
/*
* Clear the 'location'th element in 'a' and return the previous value
* If errcode is not NULL:
* 1 - The value was successfully removed (previous value returned)
* 0 - The value was not cleared because there was no previous value (returned NULL)
* -1 - 'a' was NULL or the value of 'location' was invalid
*/
AraItem ara_remove_item (ara *a, int location, int *errcode)
{
AraItem retval;
retval = ara_get_item(a, location, errcode);
if (*errcode <= 0)
{
if (a->type == ARA_TYPE_DATA)
retval.d = NULL;
else
retval.f = NULL;
}
else
{
if (a->type == ARA_TYPE_DATA)
a->list[location].d = NULL;
else
a->list[location].f = NULL;
*errcode = 1;
}
return retval;
}
/*
* Return the next element in 'ara' after 'location'
* If *location == -1 -> begin at ara location 0 (to start iterating)
* Otherwise -> begin at *location + 1
* Returns:
* -1 - 'a' was NULL or 'location' was NULL
* 0 - There were no further values (*location is not changed - do not use)
* 1 - The value in "*location" was set -- keep calling!
*/
int traverse_all_ara_items (ara *a, int *location)
{
if (!a || a->size <= 0 || !a->list || (*location) < -1)
return -1;
for (;;)
{
if ((size_t)*location >= a->size - 1)
return 0;
(*location)++;
if (a->type == ARA_TYPE_DATA)
{
if (a->list[*location].d != NULL)
return 1;
}
else
{
if (a->list[*location].f != NULL)
return 1;
}
}
}
/*
* Return the maximum valid value for 'location' in 'a'
* Returns:
* -1 - 'a' is NULL, or there are no valid locations in 'a'
* >= 0 - The maximum value you may pass as 'location'
* (Note: This is not the number of items in the
* ara! If there is only one item, then its
* location is '0'!)
*/
int ara_max (ara *a)
{
size_t idx;
if (!a || a->size == 0 || a->list == NULL)
return -1;
for (idx = a->size - 1; ; idx--)
{
if (a->type == ARA_TYPE_DATA && a->list[idx].d != NULL)
return idx;
else if (a->type == ARA_TYPE_FUNC && a->list[idx].f != NULL)
return idx;
if (idx == 0)
return -1;
}
return -1;
}
/*
* Return the number of valid values in 'a'
* Returns:
* -1 - 'a' is NULL
* >0 - The number of non-null 'locations' in 'a' that have been set
*/
int ara_number (ara *a)
{
size_t idx;
size_t count = 0;
if (!a)
return -1;
for (idx = count = 0; idx < a->size; idx++)
{
if (a->type == ARA_TYPE_DATA && a->list[idx].d != NULL)
count++;
else if (a->type == ARA_TYPE_FUNC && a->list[idx].f != NULL)
count++;
}
return count;
}
/* Return the location of 'item' in ara, or -1 if not found */
int ara_find_item (ara *a, AraItem item)
{
size_t idx;
if (!a)
return -1;
for (idx = 0; idx < a->size; idx++)
{
if (a->type == ARA_TYPE_DATA && a->list[idx].d == item.d)
return (int)idx;
else if (a->type == ARA_TYPE_FUNC && a->list[idx].f == item.f)
return (int)idx;
}
return -1;
}
/* Add 'new_item' to location 0 in ara; push everything else up one */
int ara_unshift (ara *a, AraItem new_item)
{
int errorcode = 0;
ara_insert(a, 0);
ara_set_item(a, 0, new_item, &errorcode);
return errorcode;
}
/* Remove+return location 0 and shift everything down one */
AraItem ara_shift (ara *a)
{
AraItem retval;
int errcode = 0;
retval = ara_get_item(a, 0, &errcode);
ara_delete(a, 0);
return retval;
}
/* Add 'new_item' to the end of ara */
int ara_push (ara *a, AraItem new_item)
{
int location;
int errcode = 0;
if ((location = ara_max(a)) < 0)
{
ara_enlarge_list(a, 10);
ara_set_item(a, 0, new_item, &errcode);
}
else
ara_set_item(a, location, new_item, &errcode);
return errcode;
}
/* Remove+return the last item in the ara */
AraItem ara_pop (ara *a)
{
int location;
AraItem retval;
if ((location = ara_max(a)) < 0)
{
if (a->type == ARA_TYPE_DATA)
retval.d = NULL;
else
retval.f = NULL;
return retval;
}
else
return ara_pop_item(a, location);
}
/* Remove+return the 'location'th item in the ara */
AraItem ara_pop_item (ara *a, int location)
{
AraItem retval;
int errcode = 0;
retval = ara_get_item(a, location, &errcode);
ara_delete(a, location);
return retval;
}
/* Insert 'new_item' to 'location'th spot, move everything after location down one spot */
int ara_insert_item (ara *a, int location, AraItem new_item)
{
int errcode = 0;
ara_insert(a, location);
ara_set_item(a, location, new_item, &errcode);
return errcode;
}
/* Insert 'new_item' before 'existing_item', moving everything down one spot */
int ara_insert_before_item (ara *a, AraItem existing_item, AraItem new_item)
{
int errcode = 0;
int location;
if ((location = ara_find_item(a, existing_item)) < 0)
return -1;
ara_insert(a, location);
ara_set_item(a, location, new_item, &errcode);
return errcode;
}
/* Insert 'new_item' after 'existing_item', moving everything down one spot */
int ara_insert_after_item (ara *a, AraItem existing_item, AraItem new_item)
{
int errcode = 0;
int location;
if ((location = ara_find_item(a, existing_item)) < 0)
return -1;
ara_insert(a, location + 1);
ara_set_item(a, location + 1, new_item, &errcode);
return errcode;
}
/* Find any gaps in the ara and fill them in */
int ara_collapse (ara *a)
{
size_t idx;
if (!a)
return -1;
for (idx = 0; idx < a->size; idx++)
{
if (a->type == ARA_TYPE_DATA)
{
if (a->list[idx].d == NULL)
ara_delete(a, idx);
}
else
{
if (a->list[idx].f == NULL)
ara_delete(a, idx);
}
}
return 1;
}
|