array.c

Go to the documentation of this file.
00001 /*  Copyright (c) 2006-2007, Philip Busch <broesel@studcs.uni-sb.de>
00002  *  All rights reserved.
00003  *
00004  *  Redistribution and use in source and binary forms, with or without
00005  *  modification, are permitted provided that the following conditions are met:
00006  *
00007  *   - Redistributions of source code must retain the above copyright notice,
00008  *     this list of conditions and the following disclaimer.
00009  *   - Redistributions in binary form must reproduce the above copyright
00010  *     notice, this list of conditions and the following disclaimer in the
00011  *     documentation and/or other materials provided with the distribution.
00012  *
00013  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00014  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00015  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00016  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00017  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00018  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00019  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00020  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00021  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00022  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00023  *  POSSIBILITY OF SUCH DAMAGE.
00024  */
00025 
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <assert.h>
00030 #include "array.h"
00031 
00032 
00048 static void array_initialize(array *_a, int _from, int _to)
00049 {
00050         char *p;
00051 
00052         printf("initializing %d to %d\n", _from, _to);
00053 
00054         p = (char *)_a->data + _from;
00055         while(_from++ < _to) {
00056                 *p++ = 0;
00057         }
00058         
00059         _a->sz_init = _to;
00060 }
00061 
00071 void array_init(array *_a)
00072 {
00073         assert(_a != NULL);
00074         
00075         _a->sz_init  = 0;
00076         _a->sz_alloc = 0;
00077         _a->data     = NULL;
00078 }
00079 
00086 array *array_create()
00087 {
00088         array *a;
00089 
00090         if((a = (array *)malloc(sizeof(array))) != NULL) {
00091                 array_init(a);
00092         }
00093         
00094         return(a);
00095 }
00096 
00129 void *array_allocate(array *_a, size_t _sz, int _pos)
00130 {
00131         unsigned int size;
00132         void *tmp = NULL;
00133 
00134         if(_pos < 0)
00135                 return(NULL);
00136 
00137         if((_pos + 1) * _sz <= _a->sz_init)
00138                 return(_a->data + _pos * _sz);
00139 
00140         if((_pos + 1) * _sz <= _a->sz_alloc) {
00141                 array_initialize(_a, _a->sz_init + _sz, (_pos + 1) * _sz);
00142                 return(_a->data + _pos * _sz);
00143         }
00144 
00145         size = 2;
00146         while(size <= (_pos+1) * _sz)
00147                 size *= 2;
00148 
00149         if((tmp = realloc(_a->data, size)) != NULL) {
00150                 _a->data = tmp;
00151                 _a->sz_alloc = size;
00152                 array_initialize(_a, _a->sz_init, (_pos + 1) * _sz);
00153                 return(_a->data + _pos * _sz);
00154         }
00155 
00156         return(NULL);
00157 }
00158 
00166 static void array_print(array *_a)
00167 {
00168         unsigned int i = 0;
00169         int *p = (int *)_a->data;
00170         
00171         printf("init: %d\talloc: %d\n", _a->sz_init, _a->sz_alloc);
00172         
00173         for(i = 0; i < _a->sz_alloc / sizeof(int); i++) {
00174                 printf("%2i: %i\n", i, p[i]);
00175         }
00176 }
00177 
00190 void *array_get(array *_a, size_t _sz, int _pos)
00191 {
00192         if(_pos < 0)
00193                 return(NULL);
00194 
00195         if((_pos + 1) * _sz > _a->sz_init)
00196                 return(NULL);
00197                 
00198         return(_a->data + (_pos * _sz));
00199 }
00200 
00212 size_t array_length(array *_a, size_t _sz)
00213 {
00214         return(_a->sz_init / _sz);
00215 }
00216 
00233 void array_truncate(array *_a, size_t _sz, int _len)
00234 {
00235         if((_len < 0) || (_a->sz_init <= _len * _sz))
00236                 return;
00237                 
00238         _a->sz_init = _len * _sz;
00239 }
00240 
00249 void array_reset(array *_a)
00250 {
00251         if(_a->data != NULL)
00252                 free(_a->data);
00253         _a->sz_init  = 0;
00254         _a->sz_alloc = 0;
00255         _a->data     = NULL;
00256 }
00257 
00269 void array_delete(array *_a)
00270 {
00271         array_reset(_a);
00272         free(_a);
00273 }
00274 
00285 int array_equal(array *_a, array *_b)
00286 {
00287         unsigned int i;
00288         char *a, *b;
00289 
00290         if(_a->sz_init == _b->sz_init) {
00291                 a = (char *)_a->data;
00292                 b = (char *)_b->data;
00293                 
00294                 for(i = 0; i < _a->sz_init; i++) {
00295                         if(a[i] != b[i]) {
00296                                 return(0);
00297                         }
00298                 }
00299                 return(1);
00300         }
00301 
00302         return(0);
00303 }
00304 
00324 void array_sort(array *_a, size_t _sz, int(*_cmp)(const void *, const void *))
00325 {
00326         qsort(_a->data, array_length(_a, _sz), _sz, _cmp);
00327 }
00328 
00336 static int cmp(int *_a, int *_b)
00337 {
00338         return(*_a - *_b);
00339 }
00340 
00341 int array_cat(array *_a, array *_b)
00342 {
00343         assert(_a != NULL);
00344         assert(_b != NULL);
00345         
00346         return(array_catb(_a, _b, _b->sz_init));
00347 }
00348 
00349 int array_catb(array *_a, array *_b, size_t _len)
00350 {
00351         size_t old_size = _a->sz_init;
00352 
00353         assert(_a != NULL);
00354         assert(_b != NULL);
00355 
00356         if(_len > _b->sz_init)
00357                 return(-1);
00358 
00359         if(array_allocate(_a, sizeof(char), _a->sz_init + _len - 1) == NULL)
00360                 return(-1);
00361         
00362         memcpy(_a->data + old_size, _b->data, _len);
00363         return(0);
00364 }
00365 
00366 int main()
00367 {
00368         static array x;
00369         static array y;
00370         int *p;
00371 
00372         p = (int*)array_allocate(&x, sizeof(int), 5);
00373         *p = 17;
00374         array_print(&x);
00375 
00376         p = (int *)array_allocate(&y, sizeof(int), 3);
00377         p = (int *)array_start(&y);
00378         p[0] = 3;
00379         p[1] = 5;
00380         p[2] = 4;
00381         p[3] = 17;
00382         array_print(&y);
00383 
00384         array_cat(&x, &y);
00385         array_print(&x);
00386         
00387         
00388         system("PAUSE");
00389         return(0);
00390 }

Generated on Thu Jul 19 13:36:09 2007 for libv by  doxygen 1.5.1-p1. Thank you, SourceForge.net Logo