]> rtime.felk.cvut.cz Git - ulut.git/blob - ulut/ul_gsacust.c
Added uLan Utilities (ulut) and uLan C interface (ulcintf) libraries
[ulut.git] / ulut / ul_gsacust.c
1 /*******************************************************************
2   uLan Utilities Library - C library of basic reusable constructions
3
4   ul_gsacust.c  - generic sorted arrays
5
6   (C) Copyright 2001 by Pavel Pisa - Originator
7
8   The uLan utilities library can be used, copied and modified under
9   next licenses
10     - GPL - GNU General Public License
11     - LGPL - GNU Lesser General Public License
12     - MPL - Mozilla Public License
13     - and other licenses added by project originators
14   Code can be modified and re-distributed under any combination
15   of the above listed licenses. If contributor does not agree with
16   some of the licenses, he/she can delete appropriate line.
17   Warning, if you delete all lines, you are not allowed to
18   distribute source code and/or binaries utilizing code.
19   
20   See files COPYING and README for details.
21
22  *******************************************************************/
23
24 #include <string.h>
25 #include "ul_utmalloc.h"
26 #include "ul_gsa.h"
27
28 #define GSA_ALLOC_STEP 8
29 #define GSA_DEALLOC_STEP 32
30
31 void
32 gsa_cust_init_array_field(gsa_array_field_t *array)
33 {
34   array->count=0;
35   array->alloc_count=0;
36   array->items=NULL;
37 }
38
39
40 int
41 gsa_cust_insert_at(gsa_array_field_t *array, void *item, unsigned where)
42 {
43   unsigned acnt=array->alloc_count;
44   unsigned cnt=array->count;
45   void **items, **p;
46   if(where>cnt) where=cnt;
47   if((cnt+1>=acnt)||!array->items)
48   {
49     acnt+=GSA_ALLOC_STEP;
50     if(!array->items)
51       items=malloc(acnt*sizeof(void*));
52      else
53       items=realloc(array->items,acnt*sizeof(void*));
54     if(!items) return -1;
55     array->alloc_count=acnt;
56     array->items=items;
57   }
58   else items=array->items;
59   p=items+where;
60   memmove(p+1,p,(char*)(items+cnt)-(char*)p);
61   array->count=cnt+1;
62   *p=item;
63   return 0;
64 }
65
66 int
67 gsa_cust_delete_at(gsa_array_field_t *array, unsigned indx)
68 {
69   unsigned acnt=array->alloc_count;
70   unsigned cnt=array->count;
71   void **items=array->items;
72   void **p;
73   if(indx>=cnt) return -1;
74   p=items+indx;
75   array->count=--cnt;
76   memmove(p,p+1,(items+cnt-p)*sizeof(void *));
77   if(acnt-cnt>GSA_DEALLOC_STEP+GSA_ALLOC_STEP)
78   {
79     acnt-=GSA_DEALLOC_STEP;
80     items=realloc(array->items,acnt*sizeof(void*));
81     if(items){
82       array->alloc_count=acnt;
83       array->items=items;
84     }
85   }
86   return 0;
87 }