3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2010 Angel Ortega <angel@triptico.com>
6 mpdm_a.c - Array management
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
38 /* sorting callback code */
39 static mpdm_t sort_cb
= NULL
;
44 mpdm_t
mpdm_new_a(int flags
, int size
)
45 /* creates a new array value */
49 /* creates and expands */
50 if ((v
= mpdm_new(flags
| MPDM_MULTIPLE
, NULL
, 0)) != NULL
)
51 mpdm_expand(v
, 0, size
);
57 static int wrap_offset(const mpdm_t a
, int offset
)
58 /* manages negative offsets */
61 offset
= mpdm_size(a
) + offset
;
67 mpdm_t
mpdm_aclone(const mpdm_t v
)
68 /* clones a multiple value */
73 /* creates a similar value */
74 w
= mpdm_new_a(v
->flags
, v
->size
);
76 /* fills each element with duplicates of the original */
77 for (n
= 0; n
< w
->size
; n
++)
78 mpdm_aset(w
, mpdm_clone(mpdm_aget(v
, n
)), n
);
87 * mpdm_expand - Expands an array.
89 * @offset: insertion offset
90 * @num: number of elements to insert
92 * Expands an array value, inserting @num elements (initialized
93 * to NULL) at the specified @offset.
96 mpdm_t
mpdm_expand(mpdm_t a
, int offset
, int num
)
108 /* expand, or fail */
109 if ((p
= (mpdm_t
*) realloc((mpdm_t
*)a
->data
, a
->size
* sizeof(mpdm_t
))) == NULL
)
112 /* moves up from top of the array */
113 for (n
= a
->size
- 1; n
>= offset
+ num
; n
--)
116 /* fills the new space with blanks */
117 for (; n
>= offset
; n
--)
127 * mpdm_collapse - Collapses an array.
129 * @offset: deletion offset
130 * @num: number of elements to collapse
132 * Collapses an array value, deleting @num elements at
133 * the specified @offset.
136 mpdm_t
mpdm_collapse(mpdm_t a
, int offset
, int num
)
145 /* don't try to delete beyond the limit */
146 if (offset
+ num
> a
->size
)
147 num
= a
->size
- offset
;
149 p
= (mpdm_t
*) a
->data
;
151 /* unrefs the about-to-be-deleted elements */
152 for (n
= offset
; n
< offset
+ num
; n
++)
155 /* array is now shorter */
158 /* moves down the elements */
159 for (n
= offset
; n
< a
->size
; n
++)
162 /* finally shrinks the memory block */
163 if ((a
->data
= realloc(p
, a
->size
* sizeof(mpdm_t
))) == NULL
)
171 * mpdm_aset - Sets the value of an array's element.
173 * @e: the element to be assigned
174 * @offset: the subscript of the element
176 * Sets the element of the array @a at @offset to be the @e value.
177 * Returns the previous element.
180 mpdm_t
mpdm_aset(mpdm_t a
, mpdm_t e
, int offset
)
185 offset
= wrap_offset(a
, offset
);
187 /* boundary checks */
191 /* if the array is shorter than offset, expand to make room for it */
192 if (offset
>= mpdm_size(a
))
193 if (mpdm_expand(a
, mpdm_size(a
), offset
- mpdm_size(a
) + 1) == NULL
)
196 p
= (mpdm_t
*) a
->data
;
198 /* assigns and references */
200 v
= mpdm_unref(p
[offset
]);
208 * mpdm_aget - Gets an element of an array.
210 * @offset: the subscript of the element
212 * Returns the element at @offset of the array @a.
215 mpdm_t
mpdm_aget(const mpdm_t a
, int offset
)
219 offset
= wrap_offset(a
, offset
);
221 /* boundary checks */
222 if (offset
< 0 || offset
>= mpdm_size(a
))
225 p
= (mpdm_t
*) a
->data
;
232 * mpdm_ins - Insert an element in an array.
234 * @e: the element to be inserted
235 * @offset: subscript where the element is going to be inserted
237 * Inserts the @e value in the @a array at @offset.
238 * Further elements are pushed up, so the array increases its size
239 * by one. Returns the inserted element.
242 mpdm_t
mpdm_ins(mpdm_t a
, mpdm_t e
, int offset
)
244 offset
= wrap_offset(a
, offset
);
246 /* open room and set value */
247 if (mpdm_expand(a
, offset
, 1))
248 mpdm_aset(a
, e
, offset
);
255 * mpdm_adel - Deletes an element of an array.
257 * @offset: subscript of the element to be deleted
259 * Deletes the element at @offset of the @a array. The array
260 * is shrinked by one. If @offset is negative, is counted from
261 * the end of the array (so a value of -1 means delete the
262 * last element of the array).
264 * Returns the deleted element.
267 mpdm_t
mpdm_adel(mpdm_t a
, int offset
)
271 if (a
== NULL
|| mpdm_size(a
) == 0)
274 offset
= wrap_offset(a
, offset
);
276 /* gets current value */
277 v
= mpdm_aget(a
, offset
);
279 /* shrinks the array */
280 mpdm_collapse(a
, offset
, 1);
287 * mpdm_shift - Extracts the first element of an array.
290 * Extracts the first element of the array. The array
291 * is shrinked by one.
293 * Returns the deleted element.
296 mpdm_t
mpdm_shift(mpdm_t a
)
298 return mpdm_adel(a
, 0);
303 * mpdm_push - Pushes a value into an array.
307 * Pushes a value into an array (i.e. inserts at the end).
310 mpdm_t
mpdm_push(mpdm_t a
, mpdm_t e
)
312 /* inserts at the end */
313 return mpdm_ins(a
, e
, mpdm_size(a
));
318 * mpdm_pop - Pops a value from an array.
321 * Pops a value from the array (i.e. deletes from the end
325 mpdm_t
mpdm_pop(mpdm_t a
)
327 /* deletes from the end */
328 return mpdm_adel(a
, -1);
333 * mpdm_queue - Implements a queue in an array.
335 * @e: the element to be pushed
336 * @size: maximum size of array
338 * Pushes the @e element into the @a array. If the array already has
339 * @size elements, the first (oldest) element is deleted from the
340 * queue and returned.
342 * Returns the deleted element, or NULL if the array doesn't have
343 * @size elements yet.
346 mpdm_t
mpdm_queue(mpdm_t a
, mpdm_t e
, int size
)
350 /* zero size is nonsense */
354 /* loop until a has the desired size */
355 while (mpdm_size(a
) > size
- 1)
363 static int seek(const mpdm_t a
, const mpdm_t k
, const wchar_t *ks
, int step
)
367 /* avoid stupid steps */
371 for (n
= 0; n
< mpdm_size(a
); n
+= step
) {
374 mpdm_t v
= mpdm_aget(a
, n
);
376 r
= ks
? mpdm_cmp_s(v
, ks
) : mpdm_cmp(v
, k
);
387 * mpdm_seek - Seeks a value in an array (sequential).
390 * @step: number of elements to step
392 * Seeks sequentially the value @k in the @a array in
393 * increments of @step. A complete search should use a step of 1.
394 * Returns the offset of the element if found, or -1 otherwise.
397 int mpdm_seek(const mpdm_t a
, const mpdm_t k
, int step
)
399 return seek(a
, k
, NULL
, step
);
404 * mpdm_seek_s - Seeks a value in an array (sequential, string version).
407 * @step: number of elements to step
409 * Seeks sequentially the value @k in the @a array in
410 * increments of @step. A complete search should use a step of 1.
411 * Returns the offset of the element if found, or -1 otherwise.
414 int mpdm_seek_s(const mpdm_t a
, const wchar_t *k
, int step
)
416 return seek(a
, NULL
, k
, step
);
420 static int bseek(const mpdm_t a
, const mpdm_t k
, const wchar_t *ks
, int step
, int *pos
)
424 /* avoid stupid steps */
429 t
= (mpdm_size(a
) - 1) / step
;
435 if ((v
= mpdm_aget(a
, n
* step
)) == NULL
)
438 c
= ks
? mpdm_cmp_s(v
, ks
) : mpdm_cmp(v
, k
);
457 * mpdm_bseek - Seeks a value in an array (binary).
458 * @a: the ordered array
460 * @step: number of elements to step
461 * @pos: the position where the element should be, if it's not found
463 * Seeks the value @k in the @a array in increments of @step.
464 * The array should be sorted to work correctly. A complete search
465 * should use a step of 1.
467 * If the element is found, returns the offset of the element
468 * as a positive number; otherwise, -1 is returned and the position
469 * where the element should be is stored in @pos. You can set @pos
470 * to NULL if you don't mind.
473 int mpdm_bseek(const mpdm_t a
, const mpdm_t k
, int step
, int *pos
)
475 return bseek(a
, k
, NULL
, step
, pos
);
480 * mpdm_bseek_s - Seeks a value in an array (binary, string version).
481 * @a: the ordered array
483 * @step: number of elements to step
484 * @pos: the position where the element should be, if it's not found
486 * Seeks the value @k in the @a array in increments of @step.
487 * The array should be sorted to work correctly. A complete search
488 * should use a step of 1.
490 * If the element is found, returns the offset of the element
491 * as a positive number; otherwise, -1 is returned and the position
492 * where the element should be is stored in @pos. You can set @pos
493 * to NULL if you don't mind.
496 int mpdm_bseek_s(const mpdm_t a
, const wchar_t *k
, int step
, int *pos
)
498 return bseek(a
, NULL
, k
, step
, pos
);
502 static int sort_cmp(const void *s1
, const void *s2
)
503 /* qsort help function */
507 /* if callback is NULL, use basic value comparisons */
509 ret
= mpdm_cmp(*(mpdm_t
*) s1
, *(mpdm_t
*) s2
);
511 /* executes the callback and converts to integer */
512 ret
= mpdm_ival(mpdm_exec_2(sort_cb
,
513 (mpdm_t
) * ((mpdm_t
*) s1
),
514 (mpdm_t
) * ((mpdm_t
*) s2
)));
522 * mpdm_sort - Sorts an array.
524 * @step: increment step
526 * Sorts the array. @step is the number of elements to group together.
528 * Returns the sorted array (the original one is left untouched).
531 mpdm_t
mpdm_sort(const mpdm_t a
, int step
)
533 return mpdm_sort_cb(a
, step
, NULL
);
538 * mpdm_sort_cb - Sorts an array with a special sorting function.
540 * @step: increment step
541 * @asort_cb: sorting function
543 * Sorts the array. @step is the number of elements to group together.
544 * For each pair of elements being sorted, the executable mpdm_t value
545 * @sort_cb is called with an array containing the two elements as
546 * argument. It must return a signed numerical mpdm_t value indicating
549 * Returns the sorted array (the original one is left untouched).
552 mpdm_t
mpdm_sort_cb(const mpdm_t a
, int step
, mpdm_t cb
)
559 /* creates a copy to be sorted */
564 /* references the array and the code, as the latter
565 can include anything (including sweeping) */
569 qsort((mpdm_t
*)n
->data
, mpdm_size(n
) / step
, sizeof(mpdm_t
) * step
, sort_cmp
);
582 * mpdm_split_s - Separates a string into an array of pieces (string version).
584 * @v: the value to be separated
586 * Separates the @v string value into an array of pieces, using @s
589 * If the separator is NULL, the string is splitted by characters.
591 * If the string does not contain the separator, an array holding
592 * the complete string is returned.
596 mpdm_t
mpdm_split_s(const wchar_t *s
, const mpdm_t v
)
603 /* nothing to split? */
609 /* NULL separator? special case: split string in characters */
611 for (ptr
= mpdm_string(v
); ptr
&& *ptr
!= '\0'; ptr
++)
612 mpdm_push(w
, MPDM_NS(ptr
, 1));
619 /* travels the string finding separators and creating new values */
621 *ptr
!= L
'\0' && (sptr
= wcsstr(ptr
, s
)) != NULL
;
623 mpdm_push(w
, MPDM_NS(ptr
, sptr
- ptr
));
626 mpdm_push(w
, MPDM_S(ptr
));
633 * mpdm_split - Separates a string into an array of pieces.
635 * @v: the value to be separated
637 * Separates the @v string value into an array of pieces, using @s
640 * If the separator is NULL, the string is splitted by characters.
642 * If the string does not contain the separator, an array holding
643 * the complete string is returned.
647 mpdm_t
mpdm_split(const mpdm_t s
, const mpdm_t v
)
652 ss
= (wchar_t *)s
->data
;
654 return mpdm_split_s(ss
, v
);
659 * mpdm_join_s - Joins all elements of an array into one (string version).
661 * @a: array to be joined
663 * Joins all elements from @a into one string, using @s as a glue.
667 mpdm_t
mpdm_join_s(const wchar_t *s
, const mpdm_t a
)
674 /* if a is not an array, return it as is */
675 if (!MPDM_IS_ARRAY(a
))
678 /* adds the first string */
679 ptr
= mpdm_pokev(ptr
, &l
, mpdm_aget(a
, 0));
681 ss
= s
? wcslen(s
) : 0;
683 for (n
= 1; n
< mpdm_size(a
); n
++) {
685 ptr
= mpdm_pokewsn(ptr
, &l
, s
, ss
);
688 ptr
= mpdm_pokev(ptr
, &l
, mpdm_aget(a
, n
));
694 ptr
= mpdm_poke(ptr
, &l
, L
"", 1, sizeof(wchar_t));
695 return MPDM_ENS(ptr
, l
- 1);
700 * mpdm_join - Joins all elements of an array into one.
702 * @a: array to be joined
704 * Joins all elements from @a into one string, using @s as a glue.
708 mpdm_t
mpdm_join(const mpdm_t s
, const mpdm_t a
)
710 return mpdm_join_s(s
? mpdm_string(s
) : NULL
, a
);