more small optimisations
[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 mlt_deque this = malloc( sizeof( struct mlt_deque_s ) );
44 if ( this != NULL )
45 {
46 this->list = NULL;
47 this->size = 0;
48 this->count = 0;
49 }
50 return this;
51 }
52
53 /** Return the number of items in the deque.
54 */
55
56 int mlt_deque_count( mlt_deque this )
57 {
58 return this->count;
59 }
60
61 /** Allocate space on the deque.
62 */
63
64 static int mlt_deque_allocate( mlt_deque this )
65 {
66 if ( this->count == this->size )
67 {
68 this->list = realloc( this->list, sizeof( void * ) * ( this->size + 20 ) );
69 this->size += 20;
70 }
71 return this->list == NULL;
72 }
73
74 /** Push an item to the end.
75 */
76
77 int mlt_deque_push_back( mlt_deque this, void *item )
78 {
79 int error = mlt_deque_allocate( this );
80
81 if ( error == 0 )
82 this->list[ this->count ++ ] = item;
83
84 return error;
85 }
86
87 /** Pop an item.
88 */
89
90 void *mlt_deque_pop_back( mlt_deque this )
91 {
92 return this->count > 0 ? this->list[ -- this->count ] : NULL;
93 }
94
95 /** Queue an item at the start.
96 */
97
98 int mlt_deque_push_front( mlt_deque this, void *item )
99 {
100 int error = mlt_deque_allocate( this );
101
102 if ( error == 0 )
103 {
104 memmove( &this->list[ 1 ], this->list, ( this->count ++ ) * sizeof( void * ) );
105 this->list[ 0 ] = item;
106 }
107
108 return error;
109 }
110
111 /** Remove an item from the start.
112 */
113
114 void *mlt_deque_pop_front( mlt_deque this )
115 {
116 void *item = NULL;
117
118 if ( this->count > 0 )
119 {
120 item = this->list[ 0 ];
121 memmove( this->list, &this->list[ 1 ], ( -- this->count ) * sizeof( void * ) );
122 }
123
124 return item;
125 }
126
127 /** Inquire on item at back of deque but don't remove.
128 */
129
130 void *mlt_deque_peek_back( mlt_deque this )
131 {
132 return this->count > 0 ? this->list[ this->count - 1 ] : NULL;
133 }
134
135 /** Inquire on item at front of deque but don't remove.
136 */
137
138 void *mlt_deque_peek_front( mlt_deque this )
139 {
140 return this->count > 0 ? this->list[ 0 ] : NULL;
141 }
142
143 /** Close the queue.
144 */
145
146 void mlt_deque_close( mlt_deque this )
147 {
148 free( this->list );
149 free( this );
150 }
151
152