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 program 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.
11 * This program 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.
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 Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "mlt_deque.h"
24 // System header files
35 /** Private structure.
48 mlt_deque mlt_deque_init( )
50 mlt_deque this = malloc( sizeof( struct mlt_deque_s ) );
60 /** Return the number of items in the deque.
63 int mlt_deque_count( mlt_deque this )
68 /** Allocate space on the deque.
71 static int mlt_deque_allocate( mlt_deque this )
73 if ( this->count == this->size )
75 this->list = realloc( this->list, sizeof( deque_entry ) * ( this->size + 20 ) );
78 return this->list == NULL;
81 /** Push an item to the end.
84 int mlt_deque_push_back( mlt_deque this, void *item )
86 int error = mlt_deque_allocate( this );
89 this->list[ this->count ++ ].addr = item;
97 void *mlt_deque_pop_back( mlt_deque this )
99 return this->count > 0 ? this->list[ -- this->count ].addr : NULL;
102 /** Queue an item at the start.
105 int mlt_deque_push_front( mlt_deque this, void *item )
107 int error = mlt_deque_allocate( this );
111 memmove( &this->list[ 1 ], this->list, ( this->count ++ ) * sizeof( deque_entry ) );
112 this->list[ 0 ].addr = item;
118 /** Remove an item from the start.
121 void *mlt_deque_pop_front( mlt_deque this )
125 if ( this->count > 0 )
127 item = this->list[ 0 ].addr;
128 memmove( this->list, &this->list[ 1 ], ( -- this->count ) * sizeof( deque_entry ) );
134 /** Inquire on item at back of deque but don't remove.
137 void *mlt_deque_peek_back( mlt_deque this )
139 return this->count > 0 ? this->list[ this->count - 1 ].addr : NULL;
142 /** Inquire on item at front of deque but don't remove.
145 void *mlt_deque_peek_front( mlt_deque this )
147 return this->count > 0 ? this->list[ 0 ].addr : NULL;
150 /** Push an item to the end.
153 int mlt_deque_push_back_int( mlt_deque this, int item )
155 int error = mlt_deque_allocate( this );
158 this->list[ this->count ++ ].value = item;
166 int mlt_deque_pop_back_int( mlt_deque this )
168 return this->count > 0 ? this->list[ -- this->count ].value : 0;
171 /** Queue an item at the start.
174 int mlt_deque_push_front_int( mlt_deque this, int item )
176 int error = mlt_deque_allocate( this );
180 memmove( &this->list[ 1 ], this->list, ( this->count ++ ) * sizeof( deque_entry ) );
181 this->list[ 0 ].value = item;
187 /** Remove an item from the start.
190 int mlt_deque_pop_front_int( mlt_deque this )
194 if ( this->count > 0 )
196 item = this->list[ 0 ].value;
197 memmove( this->list, &this->list[ 1 ], ( -- this->count ) * sizeof( deque_entry ) );
203 /** Inquire on item at back of deque but don't remove.
206 int mlt_deque_peek_back_int( mlt_deque this )
208 return this->count > 0 ? this->list[ this->count - 1 ].value : 0;
211 /** Inquire on item at front of deque but don't remove.
214 int mlt_deque_peek_front_int( mlt_deque this )
216 return this->count > 0 ? this->list[ 0 ].value : 0;
222 void mlt_deque_close( mlt_deque this )