xine-lib  1.2.9
sorted_array.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2000-2009 the xine project
3  *
4  * This file is part of xine, a free video player.
5  *
6  * xine is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * xine is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19  *
20  * Sorted array which grows automatically when you add elements.
21  * A binary search is used to find the position of a new element.
22  *
23  * Example:
24  * Let's create de comparison method for integers:
25  *
26  * int int_comparator(void *a, void *b) {
27  * if ((int)a < (int)b) {
28  * return -1;
29  * } else if ((int)a == (int)b) {
30  * return 0;
31  * } else {
32  * return 1;
33  * }
34  * }
35  *
36  * Create a sorted array for integers:
37  * xine_sarray_t *sarray = xine_sarray_new(10, int_comparator);
38  *
39  * Add elements:
40  * xine_sarray_add(sarray, (void*)4);
41  * xine_sarray_add(sarray, (void*)28);
42  * xine_sarray_add(sarray, (void*)7);
43  *
44  * Find an element:
45  * int pos = xine_sarray_binary_search(sarray, (void*)7);
46  * if (pos >= 0)
47  * FOUND
48  * else
49  * NOT FOUND
50  *
51  * Delete the array:
52  * xine_sarray_delete(sarray);
53  */
54 #ifndef XINE_SORTED_ARRAY_H
55 #define XINE_SORTED_ARRAY_H
56 
57 #include <stddef.h> /* size_t */
58 #include <xine/attributes.h>
59 
60 #include "array.h"
61 
62 /* Array type */
64 
65 /* Array element comparator */
66 typedef int (*xine_sarray_comparator_t)(void*, void*);
67 
68 /* Constructor */
70 
71 /* Destructor */
73 
74 /* Returns the number of element stored in the array */
75 size_t xine_sarray_size(const xine_sarray_t *sarray) XINE_PROTECTED;
76 
77 /* Removes all elements from an array */
79 
80 /* Adds the element into the array
81  Returns the insertion position */
82 int xine_sarray_add(xine_sarray_t *sarray, void *value) XINE_PROTECTED;
83 
84 /* Removes one element from an array at the position specified */
85 void xine_sarray_remove(xine_sarray_t *sarray, unsigned int position) XINE_PROTECTED;
86 
87 /* Get the element at the position specified */
88 void *xine_sarray_get(xine_sarray_t *sarray, unsigned int position) XINE_PROTECTED;
89 
90 /* Returns the index of the search key, if it is contained in the list.
91  Otherwise, (-(insertion point) - 1) or ~(insertion point).
92  The insertion point is defined as the point at which the key would be
93  inserted into the array. */
95 
96 #endif
97 
xine_sarray_comparator_t comparator
Definition: sorted_array.c:32
#define XINE_MALLOC
Definition: attributes.h:119
Definition: sorted_array.c:30
void xine_sarray_remove(xine_sarray_t *sarray, unsigned int position)
Definition: sorted_array.c:76
char key[16]
Definition: xine_speex_decoder.c:98
int(* xine_sarray_comparator_t)(void *, void *)
Definition: sorted_array.h:66
xine_sarray_t * xine_sarray_new(size_t initial_size, xine_sarray_comparator_t comparator)
Definition: sorted_array.c:36
void xine_sarray_delete(xine_sarray_t *sarray)
Definition: sorted_array.c:50
#define XINE_PROTECTED
Definition: attributes.h:73
int xine_sarray_binary_search(xine_sarray_t *sarray, void *key)
Definition: sorted_array.c:84
int xine_sarray_add(xine_sarray_t *sarray, void *value)
Definition: sorted_array.c:65
void * xine_sarray_get(xine_sarray_t *sarray, unsigned int position)
Definition: sorted_array.c:80
size_t xine_sarray_size(const xine_sarray_t *sarray)
Definition: sorted_array.c:57
void xine_sarray_clear(xine_sarray_t *sarray)
Definition: sorted_array.c:61