]> git.sesse.net Git - mlt/blob - src/framework/mlt_deque.c
Cleanup license declarations and remove dv1394d references.
[mlt] / src / framework / mlt_deque.c
1 /*
2  * mlt_deque.c -- double ended queue
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 // Local header files
22 #include "mlt_deque.h"
23
24 // System header files
25 #include <stdlib.h>
26 #include <string.h>
27
28 typedef union
29 {
30         void *addr;
31         int value;
32         double floating;
33 }
34 deque_entry;
35
36 /** Private structure.
37 */
38
39 struct mlt_deque_s
40 {
41         deque_entry *list;
42         int size;
43         int count;
44 };
45
46 /** Create a deque.
47 */
48
49 mlt_deque mlt_deque_init( )
50 {
51         mlt_deque this = malloc( sizeof( struct mlt_deque_s ) );
52         if ( this != NULL )
53         {
54                 this->list = NULL;
55                 this->size = 0;
56                 this->count = 0;
57         }
58         return this;
59 }
60
61 /** Return the number of items in the deque.
62 */
63
64 int mlt_deque_count( mlt_deque this )
65 {
66         return this->count;
67 }
68
69 /** Allocate space on the deque.
70 */
71
72 static int mlt_deque_allocate( mlt_deque this )
73 {
74         if ( this->count == this->size )
75         {
76                 this->list = realloc( this->list, sizeof( deque_entry ) * ( this->size + 20 ) );
77                 this->size += 20;
78         }
79         return this->list == NULL;
80 }
81
82 /** Push an item to the end.
83 */
84
85 int mlt_deque_push_back( mlt_deque this, void *item )
86 {
87         int error = mlt_deque_allocate( this );
88
89         if ( error == 0 )
90                 this->list[ this->count ++ ].addr = item;
91
92         return error;
93 }
94
95 /** Pop an item.
96 */
97
98 void *mlt_deque_pop_back( mlt_deque this )
99 {
100         return this->count > 0 ? this->list[ -- this->count ].addr : NULL;
101 }
102
103 /** Queue an item at the start.
104 */
105
106 int mlt_deque_push_front( mlt_deque this, void *item )
107 {
108         int error = mlt_deque_allocate( this );
109
110         if ( error == 0 )
111         {
112                 memmove( &this->list[ 1 ], this->list, ( this->count ++ ) * sizeof( deque_entry ) );
113                 this->list[ 0 ].addr = item;
114         }
115
116         return error;
117 }
118
119 /** Remove an item from the start.
120 */
121
122 void *mlt_deque_pop_front( mlt_deque this )
123 {
124         void *item = NULL;
125
126         if ( this->count > 0 )
127         {
128                 item = this->list[ 0 ].addr;
129                 memmove( this->list, &this->list[ 1 ], ( -- this->count ) * sizeof( deque_entry ) );
130         }
131
132         return item;
133 }
134
135 /** Inquire on item at back of deque but don't remove.
136 */
137
138 void *mlt_deque_peek_back( mlt_deque this )
139 {
140         return this->count > 0 ? this->list[ this->count - 1 ].addr : NULL;
141 }
142
143 /** Inquire on item at front of deque but don't remove.
144 */
145
146 void *mlt_deque_peek_front( mlt_deque this )
147 {
148         return this->count > 0 ? this->list[ 0 ].addr : NULL;
149 }
150
151 /** Push an item to the end.
152 */
153
154 int mlt_deque_push_back_int( mlt_deque this, int item )
155 {
156         int error = mlt_deque_allocate( this );
157
158         if ( error == 0 )
159                 this->list[ this->count ++ ].value = item;
160
161         return error;
162 }
163
164 /** Pop an item.
165 */
166
167 int mlt_deque_pop_back_int( mlt_deque this )
168 {
169         return this->count > 0 ? this->list[ -- this->count ].value : 0;
170 }
171
172 /** Queue an item at the start.
173 */
174
175 int mlt_deque_push_front_int( mlt_deque this, int item )
176 {
177         int error = mlt_deque_allocate( this );
178
179         if ( error == 0 )
180         {
181                 memmove( &this->list[ 1 ], this->list, ( this->count ++ ) * sizeof( deque_entry ) );
182                 this->list[ 0 ].value = item;
183         }
184
185         return error;
186 }
187
188 /** Remove an item from the start.
189 */
190
191 int mlt_deque_pop_front_int( mlt_deque this )
192 {
193         int item = 0;
194
195         if ( this->count > 0 )
196         {
197                 item = this->list[ 0 ].value;
198                 memmove( this->list, &this->list[ 1 ], ( -- this->count ) * sizeof( deque_entry ) );
199         }
200
201         return item;
202 }
203
204 /** Inquire on item at back of deque but don't remove.
205 */
206
207 int mlt_deque_peek_back_int( mlt_deque this )
208 {
209         return this->count > 0 ? this->list[ this->count - 1 ].value : 0;
210 }
211
212 /** Inquire on item at front of deque but don't remove.
213 */
214
215 int mlt_deque_peek_front_int( mlt_deque this )
216 {
217         return this->count > 0 ? this->list[ 0 ].value : 0;
218 }
219
220 /** Push an item to the end.
221 */
222
223 int mlt_deque_push_back_double( mlt_deque this, double item )
224 {
225         int error = mlt_deque_allocate( this );
226
227         if ( error == 0 )
228                 this->list[ this->count ++ ].floating = item;
229
230         return error;
231 }
232
233 /** Pop an item.
234 */
235
236 double mlt_deque_pop_back_double( mlt_deque this )
237 {
238         return this->count > 0 ? this->list[ -- this->count ].floating : 0;
239 }
240
241 /** Queue an item at the start.
242 */
243
244 int mlt_deque_push_front_double( mlt_deque this, double item )
245 {
246         int error = mlt_deque_allocate( this );
247
248         if ( error == 0 )
249         {
250                 memmove( &this->list[ 1 ], this->list, ( this->count ++ ) * sizeof( deque_entry ) );
251                 this->list[ 0 ].floating = item;
252         }
253
254         return error;
255 }
256
257 /** Remove an item from the start.
258 */
259
260 double mlt_deque_pop_front_double( mlt_deque this )
261 {
262         double item = 0;
263
264         if ( this->count > 0 )
265         {
266                 item = this->list[ 0 ].floating;
267                 memmove( this->list, &this->list[ 1 ], ( -- this->count ) * sizeof( deque_entry ) );
268         }
269
270         return item;
271 }
272
273 /** Inquire on item at back of deque but don't remove.
274 */
275
276 double mlt_deque_peek_back_double( mlt_deque this )
277 {
278         return this->count > 0 ? this->list[ this->count - 1 ].floating : 0;
279 }
280
281 /** Inquire on item at front of deque but don't remove.
282 */
283
284 double mlt_deque_peek_front_double( mlt_deque this )
285 {
286         return this->count > 0 ? this->list[ 0 ].floating : 0;
287 }
288
289 /** Close the queue.
290 */
291
292 void mlt_deque_close( mlt_deque this )
293 {
294         free( this->list );
295         free( this );
296 }
297