LibRCG  3.1.1
array.c
Go to the documentation of this file.
1 
9 #include <stdlib.h>
10 #include "array.h"
11 
12 Array newArray(int size)
13 {
14  Array array=NULL;
15  if(size>0)
16  {
17  array=malloc(sizeof(SArray));
18  if(array)
19  {
20  array->capacity=size;
21  array->size=0;
22  array->array=calloc(size,sizeof(void*));
23  if(!array->array)
24  {
25  free(array);
26  array=NULL;
27  }
28  }
29  }
30  return array;
31 }
32 
33 //==============================================================================
34 
35 void arrayDelete(Array array)
36 {
37  free(array->array);
38  free(array);
39 }
40 
41 //==============================================================================
42 
43 int arrayInsert(Array array,int index,void* elem,int replace)
44 {
45  int result=0;
46  if(index<0) result=2;
47  else if(index>=array->capacity)
48  {
49  if(arrayResize(array,index+1)) result=3;
50  else
51  {
52  array->size++;
53  array->array[index]=elem;
54  }
55  }
56  else if(array->array[index])
57  {
58  if(replace) array->array[index]=elem;
59  result=1;
60  }
61  else
62  {
63  array->size++;
64  array->array[index]=elem;
65  }
66  return result;
67 }
68 
69 //==============================================================================
70 
71 int arrayRemove(Array array,int index,void** elem)
72 {
73  int result=0;
74  if(index<0||index>=array->capacity)
75  {
76  if(elem) *elem=NULL;
77  result=1;
78  }
79  else if(!array->array[index])
80  {
81  if(elem) *elem=NULL;
82  result=1;
83  }
84  else
85  {
86  if(elem) *elem=array->array[index];
87  array->array[index]=NULL;
88  array->size--;
89  }
90  return result;
91 }
92 
93 //==============================================================================
94 
95 int arrayAt(Array array,int index,void** elem)
96 {
97  int result=0;
98  if(index<0||index>=array->capacity)
99  {
100  *elem=NULL;
101  result=1;
102  }
103  else if(!array->array[index])
104  {
105  *elem=NULL;
106  result=1;
107  }
108  else *elem=array->array[index];
109  return result;
110 }
111 
112 //==============================================================================
113 
114 int arrayResize(Array array,int size)
115 {
116  int i,result=0;
117  void** newarray;
118  if(array->capacity>=size) result=2;
119  else
120  {
121  newarray=realloc(array->array,size*sizeof(void*));
122  if(!newarray) result=1;
123  else
124  {
125  for(i=array->capacity;i<size;i++) newarray[i]=NULL;
126  array->array=newarray;
127  array->capacity=size;
128  }
129  }
130  return result;
131 }
132 
133 //==============================================================================
134 
135 int arraySize(Array array)
136 {
137  return array->size;
138 }
139 
140 //==============================================================================
141 
143 {
144  return array->capacity;
145 }
146 
147 //==============================================================================
148 
149 int arrayMap(Array array,void(*fun)(void*))
150 {
151  int i,j,result=0;
152  if(!array->size) result=1;
153  else
154  {
155  for(i=0,j=0;i<array->capacity&&j<array->size;i++)
156  {
157  if(array->array[i])
158  {
159  fun(array->array[i]);
160  j++;
161  }
162  }
163  }
164  return result;
165 }
166 
167 //==============================================================================
168 
170 {
171  int ctrl,i,j;
172  Iterator it;
173  it=newIt(array->size);
174  for(ctrl=0,i=0,j=0;i<array->capacity&&j<array->size&&!ctrl;i++)
175  {
176  if(array->array[i])
177  {
178  ctrl=itAdd(it,array->array[i]);
179  j++;
180  }
181  }
182  if(ctrl)
183  {
184  itDelete(it);
185  it=NULL;
186  }
187  return it;
188 }
void arrayDelete(Array array)
Deletes an array.
Definition: array.c:35
Array newArray(int size)
Creates an empty array, with the specified initial capacity.
Definition: array.c:12
Iterator arrayIterator(Array array)
Creates an iterator from an array.
Definition: array.c:169
Implementation of a dynamic array.
Iterator structure.
Definition: iterator.h:17
void ** array
Array of pointers to this array's elements.
Definition: array.h:30
int arrayAt(Array array, int index, void **elem)
Provides the element at the specified position of an array.
Definition: array.c:95
void itDelete(Iterator it)
Deletes an iterator.
Definition: iterator.c:36
int arrayRemove(Array array, int index, void **elem)
Removes the element at the specified position of an array.
Definition: array.c:71
int itAdd(Iterator it, void *val)
Adds an element to an iterator.
Definition: iterator.c:44
int arrayMap(Array array, void(*fun)(void *))
Applies a function to the elements of an array.
Definition: array.c:149
int size
Number of element of this array.
Definition: array.h:28
int capacity
Current capacity of this array.
Definition: array.h:26
int arrayCapacity(Array array)
Return the capacity of an array.
Definition: array.c:142
int arraySize(Array array)
Returns the size of an array.
Definition: array.c:135
Array structure.
Definition: array.h:23
int arrayResize(Array array, int size)
Increases the capacity of an array.
Definition: array.c:114
int arrayInsert(Array array, int index, void *elem, int replace)
Inserts an new element at the specified position of an array.
Definition: array.c:43
Iterator newIt(int size)
Creates an iterator.
Definition: iterator.c:12

LibRCG © 2004-2015   Rui Carlos Gonçalves