2 * mlt_deque.c -- double ended queue
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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
22 #include "mlt_deque.h"
24 // System header files
36 /** Private structure.
49 mlt_deque mlt_deque_init( )
51 mlt_deque this = malloc( sizeof( struct mlt_deque_s ) );
61 /** Return the number of items in the deque.
64 int mlt_deque_count( mlt_deque this )
69 /** Allocate space on the deque.
72 static int mlt_deque_allocate( mlt_deque this )
74 if ( this->count == this->size )
76 this->list = realloc( this->list, sizeof( deque_entry ) * ( this->size + 20 ) );
79 return this->list == NULL;
82 /** Push an item to the end.
85 int mlt_deque_push_back( mlt_deque this, void *item )
87 int error = mlt_deque_allocate( this );
90 this->list[ this->count ++ ].addr = item;
98 void *mlt_deque_pop_back( mlt_deque this )
100 return this->count > 0 ? this->list[ -- this->count ].addr : NULL;
103 /** Queue an item at the start.
106 int mlt_deque_push_front( mlt_deque this, void *item )
108 int error = mlt_deque_allocate( this );
112 memmove( &this->list[ 1 ], this->list, ( this->count ++ ) * sizeof( deque_entry ) );
113 this->list[ 0 ].addr = item;
119 /** Remove an item from the start.
122 void *mlt_deque_pop_front( mlt_deque this )
126 if ( this->count > 0 )
128 item = this->list[ 0 ].addr;
129 memmove( this->list, &this->list[ 1 ], ( -- this->count ) * sizeof( deque_entry ) );
135 /** Inquire on item at back of deque but don't remove.
138 void *mlt_deque_peek_back( mlt_deque this )
140 return this->count > 0 ? this->list[ this->count - 1 ].addr : NULL;
143 /** Inquire on item at front of deque but don't remove.
146 void *mlt_deque_peek_front( mlt_deque this )
148 return this->count > 0 ? this->list[ 0 ].addr : NULL;
151 /** Push an item to the end.
154 int mlt_deque_push_back_int( mlt_deque this, int item )
156 int error = mlt_deque_allocate( this );
159 this->list[ this->count ++ ].value = item;
167 int mlt_deque_pop_back_int( mlt_deque this )
169 return this->count > 0 ? this->list[ -- this->count ].value : 0;
172 /** Queue an item at the start.
175 int mlt_deque_push_front_int( mlt_deque this, int item )
177 int error = mlt_deque_allocate( this );
181 memmove( &this->list[ 1 ], this->list, ( this->count ++ ) * sizeof( deque_entry ) );
182 this->list[ 0 ].value = item;
188 /** Remove an item from the start.
191 int mlt_deque_pop_front_int( mlt_deque this )
195 if ( this->count > 0 )
197 item = this->list[ 0 ].value;
198 memmove( this->list, &this->list[ 1 ], ( -- this->count ) * sizeof( deque_entry ) );
204 /** Inquire on item at back of deque but don't remove.
207 int mlt_deque_peek_back_int( mlt_deque this )
209 return this->count > 0 ? this->list[ this->count - 1 ].value : 0;
212 /** Inquire on item at front of deque but don't remove.
215 int mlt_deque_peek_front_int( mlt_deque this )
217 return this->count > 0 ? this->list[ 0 ].value : 0;
220 /** Push an item to the end.
223 int mlt_deque_push_back_double( mlt_deque this, double item )
225 int error = mlt_deque_allocate( this );
228 this->list[ this->count ++ ].floating = item;
236 double mlt_deque_pop_back_double( mlt_deque this )
238 return this->count > 0 ? this->list[ -- this->count ].floating : 0;
241 /** Queue an item at the start.
244 int mlt_deque_push_front_double( mlt_deque this, double item )
246 int error = mlt_deque_allocate( this );
250 memmove( &this->list[ 1 ], this->list, ( this->count ++ ) * sizeof( deque_entry ) );
251 this->list[ 0 ].floating = item;
257 /** Remove an item from the start.
260 double mlt_deque_pop_front_double( mlt_deque this )
264 if ( this->count > 0 )
266 item = this->list[ 0 ].floating;
267 memmove( this->list, &this->list[ 1 ], ( -- this->count ) * sizeof( deque_entry ) );
273 /** Inquire on item at back of deque but don't remove.
276 double mlt_deque_peek_back_double( mlt_deque this )
278 return this->count > 0 ? this->list[ this->count - 1 ].floating : 0;
281 /** Inquire on item at front of deque but don't remove.
284 double mlt_deque_peek_front_double( mlt_deque this )
286 return this->count > 0 ? this->list[ 0 ].floating : 0;
292 void mlt_deque_close( mlt_deque this )