Merge branch 'master' of github.com:periscop/clay
[clay.git] / source / array.c
blobe2ab2093596c05d3221eec94d79dcd5b1731d10c
2 /*--------------------------------------------------------------------+
3 | Clay |
4 |--------------------------------------------------------------------|
5 | array.c |
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 |
16 | |
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. |
21 | |
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. |
26 | |
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 |
31 | |
32 | Clay, the Chunky Loop Alteration wizardrY |
33 | Written by Joel Poudroux, joel.poudroux@u-psud.fr |
34 +--------------------------------------------------------------------------*/
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <clay/macros.h>
40 #include <clay/array.h>
43 /**
44 * clay_array_malloc function:
45 * \return new struct array
47 clay_array_p clay_array_malloc() {
48 clay_array_p arr;
49 CLAY_malloc(arr, clay_array_p, sizeof(clay_array_t));
50 CLAY_malloc(arr->data, int*, sizeof(int)*CLAY_ARRAY_INIT_SIZE);
51 arr->size = 0;
52 arr->available = CLAY_ARRAY_INIT_SIZE;
53 return arr;
57 /**
58 * clay_array_add function:
59 * Push i at the end of arr
60 * \param[in,out] arr
61 * \param[in] i
63 void clay_array_add(clay_array_p arr, int i) {
64 if (arr->size >= arr->available) {
65 arr->available *= 2;
66 CLAY_realloc(arr->data, int*, sizeof(int) * arr->available);
68 arr->data[arr->size] = i;
69 (arr->size)++;
73 /**
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); }
83 /**
84 * clay_array_free function:
85 * \param[in] arr
87 void clay_array_free(clay_array_p arr) {
88 if (arr) {
89 if (arr->data) {
90 free(arr->data);
92 free(arr);
97 /**
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
101 * \param[in] arr
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) {
105 if (arr == NULL) {
106 fprintf(out, "NULL\n");
107 return;
109 int i;
110 fprintf(out, "[");
111 for (i = 0 ; i < arr->size-1 ; i++) {
112 fprintf(out, "%d,", arr->data[i]);
114 if(arr->size > 0)
115 fprintf(out, "%d", arr->data[i]);
116 fprintf(out, "]");
118 if (cr)
119 fprintf(out, "\n");
124 * clay_array_clone function:
125 * \param[in] arr
126 * \return cloned array
128 clay_array_p clay_array_clone(clay_array_p arr) {
129 clay_array_p newarr = clay_array_malloc();
130 int i;
131 for (i = 0 ; i < arr->size ; i++) {
132 clay_array_add(newarr, arr->data[i]);
134 return newarr;
139 * clay_array_concat function:
140 * a1 = a1 + a2
141 * \param[in,out] a1
142 * \param[in] a2
144 void clay_array_concat(clay_array_p a1, clay_array_p a2) {
145 int i;
146 for (i = 0 ; i < a2->size ; i++) {
147 clay_array_add(a1, a2->data[i]);
153 * clay_array_equal function:
154 * a1 == a2
155 * \param[in] a1
156 * \param[in] a2
157 * \return 0 or 1
159 int clay_array_equal(clay_array_p a1, clay_array_p a2) {
160 if (a1->size != a2->size)
161 return 0;
163 int i;
164 for (i = 0 ; i < a1->size ; i++)
165 if (a1->data[i] != a2->data[i])
166 return 0;
168 return 1;