1 /* darray.h -- dynamic arrays handling
2    Copyright (c) 1988, 89, 90, 91, 92, 93 Miguel Santana
3    Copyright (c) 1995, 96, 97, 98 Akim Demaille, Miguel Santana
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18 
19 /* Author: Akim Demaille <demaille@inf.enst.fr> */
20 
21 #ifndef DARRAY_H_
22 # define DARRAY_H_
23 
24 /* Support of prototyping when possible */
25 #ifndef PARAMS
26 #  if PROTOTYPES
27 #    define PARAMS(protos) protos
28 #  else /* no PROTOTYPES */
29 #    define PARAMS(protos) ()
30 #  endif /* no PROTOTYPES */
31 #endif
32 
33 enum da_growth
34 {
35   da_steady,			/* i.e. you're responsible	*/
36   da_linear,			/* i.e. 1 2 3 4...		*/
37   da_geometrical		/* i.e. 1 2 4 8...		*/
38 };
39 
40 typedef void (*da_map_func_t) PARAMS ((void *));
41 typedef void (*da_maparg_func_t) PARAMS ((void *, void *));
42 typedef void (*da_print_func_t) PARAMS ((const void *, FILE * stream));
43 typedef int (*da_cmp_func_t) PARAMS ((const void * k1, const void * k2));
44 typedef int (*da_cmp_arg_func_t) PARAMS ((const void * k1,
45 					  const void * k2,
46 					  const void * arg));
47 
48 struct darray
49 {
50   const char * name;
51   size_t size;
52   size_t original_size;		/* The size with which it has been created */
53   enum da_growth growth;
54   size_t increment;
55   size_t len;		/* assert (len + 1 <= size) */
56   void * * content;
57   da_print_func_t self_print;
58   da_cmp_func_t cmp;
59 };
60 
61 extern int da_exit_error;		/* exit value when encounters	*
62 					 * an error (default is 1)	*/
63 
64 /*
65  * Maintaining
66  */
67 struct darray *
68 da_new PARAMS ((const char * name, size_t size,
69 		enum da_growth growth, size_t increment,
70 		da_print_func_t self_print,
71 		da_cmp_func_t cmp));
72 void da_free_content PARAMS ((struct darray * arr, da_map_func_t free_func));
73 void da_free PARAMS ((struct darray * arr, da_map_func_t free_func));
74 void da_erase PARAMS ((struct darray * arr));
75 void da_print_stats PARAMS ((struct darray * arr, FILE * stream));
76 void da_resize PARAMS ((struct darray * arr, size_t size));
77 void da_grow PARAMS ((struct darray * arr));
78 
79 
80 /*
81  * Copying
82  */
83 struct darray * da_clone PARAMS ((struct darray * arr));
84 
85 /*
86  * Testing
87  */
88 int da_is_full PARAMS ((struct darray * arr));
89 int da_is_sorted PARAMS ((struct darray * arr));
90 #define da_is_full(da) ((da)->len + 1 >= (da)->size)
91 #define da_is_empty(da) ((da)->len == 0)
92 /* Do they have same size, and same pointers in content? */
93 int da_equal PARAMS ((struct darray * ar1, struct darray * ar2));
94 /* Do they have same size, and equal contents arcording to ar1->cmp? */
95 int da_cmp_equal PARAMS ((struct darray * ar1, struct darray * ar2));
96 int da_where PARAMS ((struct darray * arr, const void * stuff));
97 int da_includes PARAMS ((struct darray * arr, const void * stuff));
98 
99 /*
100  * Adding/removing
101  */
102 void da_append PARAMS ((struct darray * arr, void * elem));
103 void da_insert_at PARAMS ((struct darray * arr, void * elem, size_t where));
104 void da_remove_at PARAMS ((struct darray * arr,
105 			   size_t where, da_map_func_t free_func));
106 void da_concat PARAMS ((struct darray * arr, struct darray * arr2));
107 void da_prefix PARAMS ((struct darray * arr, struct darray * arr2));
108 
109 /*
110  * Mapped treatement on elements
111  */
112 void da_qsort PARAMS ((struct darray * arr));
113 void da_qsort_with_arg PARAMS ((struct darray * arr,
114 				da_cmp_arg_func_t cmp,
115 				const void * arg));
116 void da_self_print PARAMS ((struct darray * arr, FILE * stream));
117 void da_unique PARAMS ((struct darray * arr, da_map_func_t free_func));
118 
119 /* In case of equality, keep the first, or the second? */
120 enum da_include_policy {
121   da_1_wins, da_2_wins
122 };
123 
124 /* Merge A2 in A1, according to the POLICY, and free not retained
125  * items by FREE_FUNC if not NULL */
126 void da_merge PARAMS ((struct darray * a1, struct darray * a2,
127 		       da_map_func_t free_func,
128 		       enum da_include_policy policy));
129 
130 
131 void da_map PARAMS ((struct darray * arr, da_map_func_t func));
132 void da_maparg PARAMS ((struct darray * arr,
133 			da_maparg_func_t func, void * arg));
134 
135 /*
136  * ready to use auxiliary functions
137  */
138 int da_str_cmp PARAMS ((const char * s1, const char * s2));
139 void da_str_print PARAMS ((const char * s1, FILE * stream));
140 void da_str_printnl PARAMS ((const char * s1, FILE * stream));
141 
142 #endif
143