[14852] | 1 | /* Copyright 1999 by the Massachusetts Institute of Technology. |
---|
| 2 | * |
---|
| 3 | * Permission to use, copy, modify, and distribute this |
---|
| 4 | * software and its documentation for any purpose and without |
---|
| 5 | * fee is hereby granted, provided that the above copyright |
---|
| 6 | * notice appear in all copies and that both that copyright |
---|
| 7 | * notice and this permission notice appear in supporting |
---|
| 8 | * documentation, and that the name of M.I.T. not be used in |
---|
| 9 | * advertising or publicity pertaining to distribution of the |
---|
| 10 | * software without specific, written prior permission. |
---|
| 11 | * M.I.T. makes no representations about the suitability of |
---|
| 12 | * this software for any purpose. It is provided "as is" |
---|
| 13 | * without express or implied warranty. |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | static const char rcsid[] = "$Id: array.c,v 1.1 2000-06-19 03:53:32 ghudson Exp $"; |
---|
| 17 | |
---|
| 18 | #include <stdio.h> |
---|
| 19 | #include <stdlib.h> |
---|
| 20 | #include "array.h" |
---|
| 21 | |
---|
| 22 | extern char *progname; |
---|
| 23 | |
---|
| 24 | Array *array_new() |
---|
| 25 | { |
---|
| 26 | Array *array; |
---|
| 27 | |
---|
| 28 | array = malloc(sizeof(Array)); |
---|
| 29 | if (array == NULL) |
---|
| 30 | { |
---|
| 31 | fprintf(stderr, "%s: out of memory\n", progname); |
---|
| 32 | exit(1); |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | array->elements = malloc(BLOCKSIZE * sizeof(void *)); |
---|
| 36 | if (array->elements == NULL) |
---|
| 37 | { |
---|
| 38 | fprintf(stderr, "%s: out of memory\n", progname); |
---|
| 39 | exit(1); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | array->allocated = BLOCKSIZE; |
---|
| 43 | array->size = 0; |
---|
| 44 | array->compar = NULL; |
---|
| 45 | |
---|
| 46 | return array; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | void array_add(Array *array, const void *element) |
---|
| 50 | { |
---|
| 51 | if (array->size == array->allocated) |
---|
| 52 | { |
---|
| 53 | array->allocated += BLOCKSIZE; |
---|
| 54 | array->elements = realloc(array->elements, |
---|
| 55 | sizeof (void *) * array->allocated); |
---|
| 56 | if (array->elements == NULL) |
---|
| 57 | { |
---|
| 58 | fprintf(stderr, "%s: out of memory\n", progname); |
---|
| 59 | exit(1); |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | array->elements[array->size] = (void *) element; |
---|
| 64 | array->size++; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | void *array_read(const Array *array, int index) |
---|
| 68 | { |
---|
| 69 | if (index < array->size) |
---|
| 70 | return array->elements[index]; |
---|
| 71 | |
---|
| 72 | return NULL; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /* Set the sort comparison function for an array, and sort it. */ |
---|
| 76 | void array_sort(Array *array, int (*compar)(const void *, const void *)) |
---|
| 77 | { |
---|
| 78 | qsort(array->elements, array->size, sizeof(void *), compar); |
---|
| 79 | array->compar = compar; |
---|
| 80 | return; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /* Search a sorted array for the given key, returning a pointer to |
---|
| 84 | * the matching element. Returns NULL if not found, or if the |
---|
| 85 | * array was not previously sorted via ArraySort(). It is the |
---|
| 86 | * caller's responsibility to ensure that the array is sorted. |
---|
| 87 | */ |
---|
| 88 | void *array_search(const Array *array, const void *key) |
---|
| 89 | { |
---|
| 90 | if (array->compar == NULL) |
---|
| 91 | return NULL; |
---|
| 92 | |
---|
| 93 | return bsearch(&key, array->elements, array->size, sizeof(void *), |
---|
| 94 | array->compar); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | int array_size(const Array *array) |
---|
| 98 | { |
---|
| 99 | return array->size; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | void array_free(Array *array) |
---|
| 103 | { |
---|
| 104 | free(array->elements); |
---|
| 105 | free(array); |
---|
| 106 | } |
---|