2 /*--------------------------------------------------------------------+
4 |--------------------------------------------------------------------|
6 |--------------------------------------------------------------------|
7 | First version: 03/04/2012 |
8 +--------------------------------------------------------------------+
10 +--------------------------------------------------------------------------+
11 | / __)( ) /__\ ( \/ ) |
12 | ( (__ )(__ /(__)\ \ / Chunky Loop Alteration wizardrY |
13 | \___)(____)(__)(__)(__) |
14 +--------------------------------------------------------------------------+
15 | Copyright (C) 2012 University of Paris-Sud |
17 | This library is free software; you can redistribute it and/or modify it |
18 | under the terms of the GNU Lesser General Public License as published by |
19 | the Free Software Foundation; either version 2.1 of the License, or |
20 | (at your option) any later version. |
22 | This library is distributed in the hope that it will be useful but |
23 | WITHOUT ANY WARRANTY; without even the implied warranty of |
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser |
25 | General Public License for more details. |
27 | You should have received a copy of the GNU Lesser General Public License |
28 | along with this software; if not, write to the Free Software Foundation, |
29 | Inc., 51 Franklin Street, Fifth Floor, |
30 | Boston, MA 02110-1301 USA |
32 | Clay, the Chunky Loop Alteration wizardrY |
33 | Written by Joel Poudroux, joel.poudroux@u-psud.fr |
34 +--------------------------------------------------------------------------*/
39 #include <clay/macros.h>
40 #include <clay/array.h>
44 * clay_array_malloc function:
45 * \return new struct array
47 clay_array_p
clay_array_malloc() {
49 CLAY_malloc(arr
, clay_array_p
, sizeof(clay_array_t
));
50 CLAY_malloc(arr
->data
, int*, sizeof(int)*CLAY_ARRAY_INIT_SIZE
);
52 arr
->available
= CLAY_ARRAY_INIT_SIZE
;
58 * clay_array_add function:
59 * Push i at the end of arr
63 void clay_array_add(clay_array_p arr
, int i
) {
64 if (arr
->size
>= arr
->available
) {
66 CLAY_realloc(arr
->data
, int*, sizeof(int) * arr
->available
);
68 arr
->data
[arr
->size
] = i
;
74 * clay_array_remove_last function:
75 * Remove last element of the array if exists.
76 * \param[in,out] array A Clay array
78 void clay_array_remove_last(clay_array_p array
) {
79 if (array
!= NULL
&& array
->size
> 0) { --(array
->size
); }
84 * clay_array_free function:
87 void clay_array_free(clay_array_p arr
) {
98 * clay_array_print function:
99 * Print the array like this : [0,1,2,3,4,5,6]
100 * \param[in] out file where to print
102 * \param[in] cr if 1, it will print a \n at the end
104 void clay_array_print(FILE *out
, clay_array_p arr
, int cr
) {
106 fprintf(out
, "NULL\n");
111 for (i
= 0 ; i
< arr
->size
-1 ; i
++) {
112 fprintf(out
, "%d,", arr
->data
[i
]);
115 fprintf(out
, "%d", arr
->data
[i
]);
124 * clay_array_clone function:
126 * \return cloned array
128 clay_array_p
clay_array_clone(clay_array_p arr
) {
129 clay_array_p newarr
= clay_array_malloc();
131 for (i
= 0 ; i
< arr
->size
; i
++) {
132 clay_array_add(newarr
, arr
->data
[i
]);
139 * clay_array_concat function:
144 void clay_array_concat(clay_array_p a1
, clay_array_p a2
) {
146 for (i
= 0 ; i
< a2
->size
; i
++) {
147 clay_array_add(a1
, a2
->data
[i
]);
153 * clay_array_equal function:
159 int clay_array_equal(clay_array_p a1
, clay_array_p a2
) {
160 if (a1
->size
!= a2
->size
)
164 for (i
= 0 ; i
< a1
->size
; i
++)
165 if (a1
->data
[i
] != a2
->data
[i
])