summaryrefslogtreecommitdiffstats
path: root/ksysguard/CContLib/ccont.h
blob: 66b22eba42172a409a3c591d3a5c22579c0d617f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
    Lightweight C Container Library
   
	Copyright (c) 2002 Tobias Koenig <[email protected]>

	Original code was written by Chris Schlaeger <[email protected]>
    
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

*/

#ifndef _CCONT_H
#define _CCONT_H

#ifndef NIL
#define NIL ((void*) 0)
#endif

#define destr_ctnr(x, y) zero_destr_ctnr(x, y); x=0

struct container {
	struct container* next;
	struct container* prev;
	void* data;
};

typedef struct container T_CONTAINER;
typedef struct container* CONTAINER;

typedef long INDEX;

typedef void (*DESTR_FUNC)(void*);
typedef int  (*COMPARE_FUNC)(void*, void*);

/**
 * Initialize the container @p ctnr.
 */
CONTAINER new_ctnr(void);

/**
 * Remove all entries from @p ctnr and reset its
 * internal structure. Use @ref new_ctnr() @em before @em
 * access the container next time.
 *
 * Note: use the 'destr_ctnr' macro to get zeroed pointer
 *       automatically.
 *
 * @param destr_func The function that is called to
 *                   free the single entries.
 */
void zero_destr_ctnr(CONTAINER ctnr, DESTR_FUNC destr_func);

/**
 * @return the number of entries in @p ctnr.
 */
INDEX level_ctnr(CONTAINER ctnr);

/**
 * Insert a new entry in container.
 *
 * @param object  A pointer to the object.
 * @param pos     The position where the object should be insert.
 */
void insert_ctnr(CONTAINER ctnr, void* object, INDEX pos);

/**
 * Add a new entry at the end of container.
 *
 * @param object  The object that should be added.
 */
void push_ctnr(CONTAINER ctnr, void* object);

/**
 * Remove an entry from container.
 *
 * @param pos  The position of the object that should be removed.
 *
 * @return A pointer to the removed object or @p 0L if it doesn't exist.
 */
void* remove_at_ctnr(CONTAINER ctnr, INDEX pos);

/**
 * Remove the first entry of container.
 *
 * @return A pointer to the removed object or @p 0L if there is now entry.
 */
void* pop_ctnr(CONTAINER ctnr);

/**
 * @return A pointer to the object at position @a pos
 *         or @p 0L if it doesn't exist.
 */
void* get_ctnr(CONTAINER ctnr, INDEX pos);

/**
 * @return The position of a matching entry.
 *
 * @param compare_func A Pointer to the function that is
 *                     called to compare all entries in the
 *                     container with the given pattern.
 * @param pattern      The pattern for coparison.
 */
INDEX search_ctnr(CONTAINER ctnr, COMPARE_FUNC compare_func, void* pattern);

/**
 * Swap two objects in container.
 *
 * @param pos1 Position of the first object.
 * @param pos2 Position of the second object.
 */
void swop_ctnr(CONTAINER ctnr, INDEX pos1, INDEX pos2);

/**
 * Sort all entries of container.
 *
 * @param compare_func A Pointer to the function that is
 *                     called to compare to entries of the
 *                     container.
 */
void bsort_ctnr(CONTAINER ctnr, COMPARE_FUNC compare_func);

/**
 * Use this function to iterate over the container.
 *
 * for (ptr = first_ctnr(ctnr); ptr; ptr = next_ctnr(ctnr)) {
 * 	do_anything(ptr);
 * }
 *
 * @return A Pointer to the first object in container.
 */
void* first_ctnr(CONTAINER ctnr);

/**
 * Use this function to iterate over the container.
 *
 * @return A Pointer to the next object in container.
 */
void* next_ctnr(CONTAINER ctnr);

/**
 * Use this function to remove the current entry while
 * iterating over the container.
 *
 * @return A Pointer to the removed object or @p 0L if it doesn't exist.
 */
void* remove_ctnr(CONTAINER ctnr);

#endif