Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Related Pages

array.c

00001 /* 00002 00003 The BLAH library, a container library 00004 Copyright (C) 1997-2004 The CDG Team <cdg@nats.informatik.uni-hamburg.de> 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00019 00020 Contact: blah@nats.informatik.uni-hamburg.de 00021 00022 $Id: array.c,v 1.14 2004/03/23 14:21:57 micha Exp $ 00023 00024 */ 00025 00026 /* ---------------------------------------------------------------------- 00027 * @defgroup Array Arrays 00028 * 00029 * Implementation of an array container. 00030 * 00031 * An array is a matrix-like data structure with an arbitrary (but 00032 * fixed) dimension and arbitrary (but fixed) size. Items are accessed by 00033 * a tuple of indices in constant time. Some of the functions of this 00034 * module use a variable number of arguments like 00035 * - arrayNew() 00036 * - arraySetElement() and 00037 * - arrayElement(). 00038 * 00039 * The defined dimension constructing and array with arrayNew() 00040 * must match the number of indices you provide to arraySetElement() 00041 * and arrayElement(). 00042 * 00043 * @{ 00044 */ 00045 00046 /* -----INCLUDES------------------------------------------------------ */ 00047 #include <stdio.h> 00048 #include "blah.h" 00049 00050 /* ---------------------------------------------------------------------- 00051 * internal structure of an array. 00052 */ 00053 struct ArrayStruct { 00054 Vector dims; /**< vector of dimensions */ 00055 int totalNoOfEntries; /**< product of dimensions */ 00056 Pointer *entries; /**< table of entries */ 00057 }; 00058 00059 /* ---------------------------------------------------------------------- 00060 * creates and returns a new vector. 00061 * 00062 * This function constructs a new array with an arbitrary number of 00063 * dimensions. 00064 * 00065 * @param i the first array dimension 00066 * @param ... optional more dimensions 00067 * @returns a new Array 00068 */ 00069 Array arrayNew(int i, ...) 00070 { 00071 Array a; 00072 va_list ap; 00073 int counter; 00074 00075 a = (Array) memMalloc(sizeof (ArrayStruct)); 00076 a->dims = vectorNew(5); 00077 00078 if (i <= 0) { 00079 printf("PANIC: illegal array dimen %d!\n", i); 00080 abort(); 00081 } 00082 00083 counter = i; 00084 vectorAddElement(a->dims, (void *)i); 00085 00086 va_start(ap, i); 00087 while ((i = va_arg(ap, int)) != 0) { 00088 vectorAddElement(a->dims, (void *)i); 00089 counter *= i; 00090 } 00091 va_end(ap); 00092 00093 a->totalNoOfEntries = counter; 00094 a->entries = (Pointer *) memMalloc(sizeof (Pointer) * counter); 00095 for (i = 0; i < counter; i++) 00096 a->entries[i] = NULL; 00097 00098 return (a); 00099 } 00100 00101 /* ---------------------------------------------------------------------- 00102 * copy an array into a new one. 00103 * 00104 * @param a is the source for the cloning 00105 * @returns a new cloned array 00106 */ 00107 Array arrayClone(Array a) 00108 { 00109 int i; 00110 Array new; 00111 00112 new=(Array)memMalloc(sizeof(ArrayStruct)); 00113 new->dims=vectorClone(a->dims); 00114 new->totalNoOfEntries=a->totalNoOfEntries; 00115 00116 new->entries=(Pointer *)memMalloc(sizeof(Pointer)*new->totalNoOfEntries); 00117 for (i=0; i < new->totalNoOfEntries; i++) 00118 new->entries[i]=a->entries[i]; 00119 00120 return(new); 00121 } 00122 00123 /* ---------------------------------------------------------------------- 00124 * deletes an array. Any future access to the array is illegal. 00125 * Note that this function does not free the memory from the items 00126 * contained in the array. 00127 * 00128 * @param a the array to be deleted. 00129 */ 00130 void arrayDelete(Array a) 00131 { 00132 if (a == NULL) { 00133 fprintf(stderr, "ERROR: arrayDelete: argument must not be NULL\n"); 00134 abort(); 00135 } 00136 00137 vectorDelete(a->dims); 00138 memFree(a->entries); 00139 memFree(a); 00140 } 00141 00142 /* ---------------------------------------------------------------------- 00143 * sets an array element to a new value. 00144 * 00145 * @param a the array whose element should be set to a new value. 00146 * @param new the new value to which the element must be set to. 00147 * @param ... the index of the new element in the array. 00148 * @returns the old value of the element 00149 */ 00150 Pointer arraySetElement(Array a, Pointer new, ...) 00151 { 00152 va_list ap; 00153 Pointer old; 00154 int d, factor, index, i; 00155 00156 va_start(ap, new); 00157 00158 index = 0; 00159 factor = 1; 00160 for (d = 0; d < vectorSize(a->dims); d++) { 00161 i = va_arg(ap, int); 00162 00163 index += i * factor; 00164 if (i >= (int)vectorElement(a->dims, d)) { 00165 fprintf(stderr, "ERROR: arraySetElement: index %d out of range\n", i); 00166 abort(); 00167 } 00168 factor *= (int)vectorElement(a->dims, d); 00169 } 00170 va_end(ap); 00171 00172 old = a->entries[index]; 00173 a->entries[index] = new; 00174 return (old); 00175 } 00176 00177 /* ---------------------------------------------------------------------- 00178 * sets all array element to a new value 00179 * 00180 * @param a the array whose values should be set to a new value 00181 * @param new the new value to which all the elements of the array have to be set to. 00182 */ 00183 void arraySetAllElements(Array a, Pointer new) 00184 { 00185 int i; 00186 00187 for (i=0; i < a->totalNoOfEntries; i++) 00188 a->entries[i]=new; 00189 } 00190 00191 /* ---------------------------------------------------------------------- 00192 * retrieves an array element. 00193 * 00194 * @param a the array from which an element has to be retrieved. 00195 * @param ... the indices that define which element has to be retrieved. 00196 * @return the element identified by the indices. 00197 */ 00198 Pointer arrayElement(Array a, ...) 00199 { 00200 va_list ap; 00201 int d, factor, index, i, noDims=vectorSize(a->dims); 00202 00203 va_start(ap, a); 00204 00205 index=0; 00206 factor=1; 00207 for (d=0; d < noDims; d++) { 00208 i=va_arg(ap, int); 00209 index += i * factor; 00210 if (i >= (int)vectorElement(a->dims, d)) { 00211 fprintf(stderr, 00212 "arrayElement: index %d of dimension %d out of range\n", 00213 i, d); 00214 abort(); 00215 } 00216 factor *= (int)vectorElement(a->dims, d); 00217 } 00218 va_end(ap); 00219 00220 return(a->entries[index]); 00221 } 00222 00223 /* ---------------------------------------------------------------------- 00224 * returns value for dimension dim. 00225 * 00226 * @param a the array whose dimension has to be retrieved. 00227 * @param dim the dimension in which the size of the array has to be retrieved. 00228 * @returns the size of the array in the specified dimension. 00229 */ 00230 int arrayDimension(Array a, int dim) 00231 { 00232 int noDims=vectorSize(a->dims); 00233 00234 if (dim>=0 && dim<noDims) 00235 { 00236 return (int)vectorElement(a->dims, dim); 00237 } 00238 fprintf(stderr, "arrayDimension: illegal dimension %d\n", dim); 00239 abort(); 00240 } 00241 00242 /* ------------------------------------------------------------------- */ 00243 /* ------------------------------------------------------------------- */ 00244 /** @} */ 00245

BLAH 0.95 (20 Oct 2004)