1 /*
2 * friso dynamaic interface implemented functions file
3 * that defined in header file "friso_API.h".
4 * never use it for commercial use.
5 *
6 * @author chenxini <chenxin619315@gmail.com>
7 */
8
9 #include "friso_API.h"
10 #include <stdlib.h>
11
12 /* ********************************************
13 * friso array list static functions block *
14 **********************************************/
create_array_entries(uint_t __blocks)15 __STATIC_API__ void **create_array_entries( uint_t __blocks )
16 {
17 register uint_t t;
18 void **block = ( void ** ) FRISO_CALLOC( sizeof( void * ), __blocks );
19 if ( block == NULL ) {
20 ___ALLOCATION_ERROR___
21 }
22
23 //initialize
24 for ( t = 0; t < __blocks; t++ ) {
25 block[t] = NULL;
26 }
27
28 return block;
29 }
30
31 //resize the array. (the opacity should not be smaller than array->length)
resize_array_list(friso_array_t array,uint_t opacity)32 __STATIC_API__ friso_array_t resize_array_list(
33 friso_array_t array,
34 uint_t opacity )
35 {
36 register uint_t t;
37 void **block = create_array_entries( opacity );
38
39 for ( t = 0; t < array->length ; t++ ) {
40 block[t] = array->items[t];
41 }
42
43 FRISO_FREE( array->items );
44 array->items = block;
45 array->allocs = opacity;
46
47 return array;
48 }
49
50
51 /* ********************************************
52 * friso array list FRISO_API functions block *
53 **********************************************/
54 //create a new array list. (A macro define has replace this.)
55 //FRISO_API friso_array_t new_array_list( void ) {
56 // return new_array_list_with_opacity( __DEFAULT_ARRAY_LIST_OPACITY__ );
57 //}
58
59 //create a new array list with a given opacity.
new_array_list_with_opacity(uint_t opacity)60 FRISO_API friso_array_t new_array_list_with_opacity( uint_t opacity )
61 {
62 friso_array_t array = ( friso_array_t )
63 FRISO_MALLOC( sizeof( friso_array_entry ) );
64 if ( array == NULL ) {
65 ___ALLOCATION_ERROR___
66 }
67
68 //initialize
69 array->items = create_array_entries( opacity );
70 array->allocs = opacity;
71 array->length = 0;
72
73 return array;
74 }
75
76 /*
77 * free the given friso array.
78 * and its items, but never where its items item pointed to .
79 */
free_array_list(friso_array_t array)80 FRISO_API void free_array_list( friso_array_t array )
81 {
82 //free the allocation that all the items pointed to
83 //register int t;
84 //if ( flag == 1 ) {
85 // for ( t = 0; t < array->length; t++ ) {
86 // if ( array->items[t] == NULL ) continue;
87 // FRISO_FREE( array->items[t] );
88 // array->items[t] = NULL;
89 // }
90 //}
91
92 FRISO_FREE( array->items );
93 FRISO_FREE( array );
94 }
95
96 //add a new item to the array.
array_list_add(friso_array_t array,void * value)97 FRISO_API void array_list_add( friso_array_t array, void *value )
98 {
99 //check the condition to resize.
100 if ( array->length == array->allocs ) {
101 resize_array_list( array, array->length * 2 + 1 );
102 }
103 array->items[array->length++] = value;
104 }
105
106 //insert a new item at a specified position.
array_list_insert(friso_array_t array,uint_t idx,void * value)107 FRISO_API void array_list_insert(
108 friso_array_t array,
109 uint_t idx,
110 void *value )
111 {
112 register uint_t t;
113
114 if ( idx <= array->length ) {
115 //check the condition to resize the array.
116 if ( array->length == array->allocs ) {
117 resize_array_list( array, array->length * 2 + 1 );
118 }
119
120 //move the elements after idx.
121 //for ( t = idx; t < array->length; t++ ) {
122 // array->items[t+1] = array->items[t];
123 //}
124 for ( t = array->length - 1; t >= idx; t-- ) {
125 array->items[t+1] = array->items[t];
126 }
127
128 array->items[idx] = value;
129 array->length++;
130 }
131 }
132
133 //get the item at a specified position.
array_list_get(friso_array_t array,uint_t idx)134 FRISO_API void *array_list_get( friso_array_t array, uint_t idx )
135 {
136 if ( idx < array->length ) {
137 return array->items[idx];
138 }
139 return NULL;
140 }
141
142 //set the value of the item at a specified position.
143 //this will return the old value.
array_list_set(friso_array_t array,uint_t idx,void * value)144 FRISO_API void * array_list_set(
145 friso_array_t array,
146 uint_t idx,
147 void * value )
148 {
149 void * oval = NULL;
150 if ( idx < array->length ) {
151 oval = array->items[idx];
152 array->items[idx] = value;
153 }
154 return oval;
155 }
156
157 //remove the item at a specified position.
158 //this will return the value of the removed item.
array_list_remove(friso_array_t array,uint_t idx)159 FRISO_API void * array_list_remove(
160 friso_array_t array, uint_t idx )
161 {
162 register uint_t t;
163 void *oval = NULL;
164
165 if ( idx < array->length ) {
166 oval = array->items[idx];
167 //move the elements after idx.
168 for ( t = idx; t < array->length - 1; t++ ) {
169 array->items[t] = array->items[ t + 1 ];
170 }
171 array->items[array->length - 1] = NULL;
172 array->length--;
173 }
174
175 return oval;
176 }
177
178 /*trim the array list*/
array_list_trim(friso_array_t array)179 FRISO_API friso_array_t array_list_trim( friso_array_t array )
180 {
181 if ( array->length < array->allocs ) {
182 return resize_array_list( array, array->length );
183 }
184 return array;
185 }
186
187 /*
188 * clear the array list.
189 * this function will free all the allocations that the pointer pointed.
190 * but will not free the point array allocations,
191 * and will reset the length of it.
192 */
array_list_clear(friso_array_t array)193 FRISO_API friso_array_t array_list_clear( friso_array_t array )
194 {
195 register uint_t t;
196 //free all the allocations that the array->length's pointer pointed.
197 for ( t = 0; t < array->length; t++ ) {
198 /*if ( array->items[t] == NULL ) continue;
199 FRISO_FREE( array->items[t] ); */
200 array->items[t] = NULL;
201 }
202 //attribute reset.
203 array->length = 0;
204
205 return array;
206 }
207
208 //get the size of the array list. (A macro define has replace this.)
209 //FRISO_API uint_t array_list_size( friso_array_t array ) {
210 // return array->length;
211 //}
212
213 //return the allocations of the array list.(A macro define has replace this)
214 //FRISO_API uint_t array_list_allocs( friso_array_t array ) {
215 // return array->allocs;
216 //}
217
218 //check if the array is empty.(A macro define has replace this.)
219 //FRISO_API int array_list_empty( friso_array_t array )
220 //{
221 // return ( array->length == 0 );
222 //}
223