added deque, api design for manager, minor affine tweaks, experimental destructor...
[melted] / 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 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.
10 *
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.
15 *
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.
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 /** Private structure.
29 */
30
31 struct mlt_deque_s
32 {
33 void **list;
34 int size;
35 int count;
36 };
37
38 /** Create a deque.
39 */
40
41 mlt_deque mlt_deque_init( )
42 {
43 return calloc( 1, sizeof( struct mlt_deque_s ) );
44 }
45
46 /** Return the number of items in the deque.
47 */
48
49 int mlt_deque_count( mlt_deque this )
50 {
51 return this->count;
52 }
53
54 /** Allocate space on the deque.
55 */
56
57 static int mlt_deque_allocate( mlt_deque this )
58 {
59 if ( this->count == this->size )
60 {
61 this->list = realloc( this->list, sizeof( void * ) * ( this->size + 10 ) );
62 this->size += 10;
63 }
64 return this->list == NULL;
65 }
66
67 /** Push an item to the end.
68 */
69
70 int mlt_deque_push_back( mlt_deque this, void *item )
71 {
72 int error = mlt_deque_allocate( this );
73
74 if ( error == 0 )
75 this->list[ this->count ++ ] = item;
76
77 return error;
78 }
79
80 /** Pop an item.
81 */
82
83 void *mlt_deque_pop_back( mlt_deque this )
84 {
85 return this->count > 0 ? this->list[ -- this->count ] : NULL;
86 }
87
88 /** Queue an item at the start.
89 */
90
91 int mlt_deque_push_front( mlt_deque this, void *item )
92 {
93 int error = mlt_deque_allocate( this );
94
95 if ( error == 0 )
96 {
97 memmove( &this->list[ 1 ], this->list, ( this->count ++ ) * sizeof( void * ) );
98 this->list[ 0 ] = item;
99 }
100
101 return error;
102 }
103
104 /** Remove an item from the start.
105 */
106
107 void *mlt_deque_pop_front( mlt_deque this )
108 {
109 void *item = NULL;
110
111 if ( this->count > 0 )
112 {
113 item = this->list[ 0 ];
114 memmove( this->list, &this->list[ 1 ], ( -- this->count ) * sizeof( void * ) );
115 }
116
117 return item;
118 }
119
120 /** Inquire on item at back of deque but don't remove.
121 */
122
123 void *mlt_deque_peek_back( mlt_deque this )
124 {
125 return this->count > 0 ? this->list[ this->count - 1 ] : NULL;
126 }
127
128 /** Inquire on item at front of deque but don't remove.
129 */
130
131 void *mlt_deque_peek_front( mlt_deque this )
132 {
133 return this->count > 0 ? this->list[ 0 ] : NULL;
134 }
135
136 /** Close the queue.
137 */
138
139 void mlt_deque_close( mlt_deque this )
140 {
141 free( this->list );
142 free( this );
143 }
144
145