source: trunk/lib/void_stack.c @ 254

Last change on this file since 254 was 169, checked in by tim, 15 years ago

filled in additional, minimal documentation

  • Property svn:keywords set to Id
File size: 3.3 KB
RevLine 
[169]1/*
2 * Copyright (C) 2005,2007,2009-2010 Timothy D. Morgan
[168]3 *
[31]4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
[111]6 * the Free Software Foundation; version 3 of the License.
[31]7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
16 *
17 * $Id: void_stack.c 169 2010-03-03 19:24:58Z tim $
18 */
19
[169]20/** 
21 * @file
22 */
23
24
[147]25#include "void_stack.h"
[31]26
27void_stack* void_stack_new(unsigned short max_size)
28{
[150]29  void_stack* ret_val = talloc(NULL, void_stack);
[31]30
31  if (ret_val != NULL)
32  {
33    memset(ret_val, 0, sizeof(*ret_val));
[150]34    ret_val->elements = talloc_array(ret_val, void*, max_size);
[31]35    if (ret_val->elements == NULL)
36    {
[150]37      talloc_free(ret_val);
[31]38      ret_val = NULL;
39    }
40    else
41    {
42      memset(ret_val->elements, 0, max_size*sizeof(void*));
43   
44      ret_val->max_size = max_size;
45      ret_val->top = 0;
46    }
47  }
48
49  return ret_val;
50}
51
52
[38]53void_stack* void_stack_copy(const void_stack* v)
54{
55  unsigned int i;
56  void_stack* ret_val;
57  if(v == NULL)
58    return NULL;
59
60  ret_val = void_stack_new(v->max_size);
61  if(ret_val == NULL)
62    return NULL;
63
64  for(i = 0; i < v->top; i++)
65    ret_val->elements[i] = v->elements[i];
66  ret_val->top = v->top;
67
68  return ret_val;
69}
70
71
72void_stack* void_stack_copy_reverse(const void_stack* v)
73{
74  unsigned int i;
75  void_stack* ret_val;
76  if(v == NULL)
77    return NULL;
78
79  ret_val = void_stack_new(v->max_size);
80  if(ret_val == NULL)
81    return NULL;
82
83  for(i = 0; i < v->top; i++)
84    ret_val->elements[i] = v->elements[v->top-i-1];
85  ret_val->top = v->top;
86
87  return ret_val;
88}
89
90
[80]91void void_stack_free(void_stack* stack)
[31]92{
[150]93  talloc_free(stack);
[38]94}
95
96
[80]97void void_stack_free_deep(void_stack* stack)
[38]98{
[31]99  unsigned short i;
100  for(i=0; i < stack->top; i++)
101    free(stack->elements[i]);
[150]102  talloc_free(stack);
[31]103}
104
105
[89]106unsigned short void_stack_size(const void_stack* stack)
[33]107{
108  return stack->top;
109}
110
111
[31]112void* void_stack_pop(void_stack* stack)
113{
114  void* ret_val = NULL;
115
116  if(stack->top > 0)
117  {
[52]118    ret_val = stack->elements[--(stack->top)];
[31]119    stack->elements[stack->top] = NULL;
120  }
121
122  return ret_val;
123}
124
125
[89]126bool void_stack_push(void_stack* stack, void* e)
[31]127{
[89]128  if(stack->top < stack->max_size)
129  {
130    stack->elements[stack->top++] = e;
131    return true;
132  }
133  else
134    return false;
135}
136
137
138const void* void_stack_cur(const void_stack* stack)
139{
[31]140  void* ret_val = NULL;
141
142  if(stack->top > 0)
143    ret_val = stack->elements[stack->top-1];
144
145  return ret_val;
146}
147
148
[89]149void_stack_iterator* void_stack_iterator_new(const void_stack* stack)
[31]150{
151  void_stack_iterator* ret_val = NULL;
152 
[89]153  if(stack != NULL)
[31]154  {
[150]155    ret_val = talloc(stack, void_stack_iterator);
[89]156    if (ret_val != NULL)
157    {
158      ret_val->stack = stack;
159      ret_val->cur = 0;
160    }
[31]161  }
162
163  return ret_val;
164}
165
166
[80]167void void_stack_iterator_free(void_stack_iterator* iter)
[31]168{
[150]169  talloc_free(iter);
[31]170}
171
172
[33]173const void* void_stack_iterator_next(void_stack_iterator* iter)
[31]174{
175  if(iter->cur < iter->stack->top)
176    return iter->stack->elements[iter->cur++];
177  else
178    return NULL;
179}
Note: See TracBrowser for help on using the repository browser.