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
28 /** Private structure.
41 mlt_deque
mlt_deque_init( )
43 mlt_deque
this = malloc( sizeof( struct mlt_deque_s
) );
53 /** Return the number of items in the deque.
56 int mlt_deque_count( mlt_deque
this )
61 /** Allocate space on the deque.
64 static int mlt_deque_allocate( mlt_deque
this )
66 if ( this->count
== this->size
)
68 this->list
= realloc( this->list
, sizeof( void * ) * ( this->size
+ 20 ) );
71 return this->list
== NULL
;
74 /** Push an item to the end.
77 int mlt_deque_push_back( mlt_deque
this, void *item
)
79 int error
= mlt_deque_allocate( this );
82 this->list
[ this->count
++ ] = item
;
90 void *mlt_deque_pop_back( mlt_deque
this )
92 return this->count
> 0 ?
this->list
[ -- this->count
] : NULL
;
95 /** Queue an item at the start.
98 int mlt_deque_push_front( mlt_deque
this, void *item
)
100 int error
= mlt_deque_allocate( this );
104 memmove( &this->list
[ 1 ], this->list
, ( this->count
++ ) * sizeof( void * ) );
105 this->list
[ 0 ] = item
;
111 /** Remove an item from the start.
114 void *mlt_deque_pop_front( mlt_deque
this )
118 if ( this->count
> 0 )
120 item
= this->list
[ 0 ];
121 memmove( this->list
, &this->list
[ 1 ], ( -- this->count
) * sizeof( void * ) );
127 /** Inquire on item at back of deque but don't remove.
130 void *mlt_deque_peek_back( mlt_deque
this )
132 return this->count
> 0 ?
this->list
[ this->count
- 1 ] : NULL
;
135 /** Inquire on item at front of deque but don't remove.
138 void *mlt_deque_peek_front( mlt_deque
this )
140 return this->count
> 0 ?
this->list
[ 0 ] : NULL
;
146 void mlt_deque_close( mlt_deque
this )