PIPS
newgen_stack.h
Go to the documentation of this file.
1 /*
2 
3  $Id: newgen_stack.h 1357 2016-03-02 08:18:50Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of NewGen.
8 
9  NewGen is free software: you can redistribute it and/or modify it under the
10  terms of the GNU General Public License as published by the Free Software
11  Foundation, either version 3 of the License, or any later version.
12 
13  NewGen is distributed in the hope that it will be useful, but WITHOUT ANY
14  WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16  License for more details.
17 
18  You should have received a copy of the GNU General Public License along with
19  NewGen. If not, see <http://www.gnu.org/licenses/>.
20 
21 */
22 /* STACK MANAGEMENT -- headers
23  *
24  * - a stack is declared with type stack (internals not visible from here!)
25  * - a stack_iterator allows to iterate over the items in a stack.
26  * - allocation with stack_make(newgen domain, bucket size)
27  * - free with stack_free(stack)
28  * - stack_size(stack) returns the number of elements stacked
29  * - stack_empty_p(stack) tells whether the stack is empty or not
30  * stack_empty_p(stack)==(stack_size(stack)==0)
31  * - stack_{push,pop,head,replace} do what you may expect from them
32  * - stack_info gives informations about the stack
33  * - stack_map applies the function on all the items in stack.
34  * - stack_iterator_{init,next_and_go,end} to iterate.
35  * - see STACK_MAP for instance.
36  *
37  * newgen_assert should be included before.
38  *
39  * Fabien COELHO 05/12/1994
40  */
41 
42 #ifndef STACK_INCLUDED
43 #define STACK_INCLUDED
44 
45 /* encapsulated types
46  */
47 typedef struct __stack_head *stack;
49 
50 /* defines for empty values
51  */
52 #define STACK_NULL ((stack) NULL)
53 #define STACK_NULL_P(s) ((s)==STACK_NULL)
54 
55 #define stack_undefined ((stack)-14)
56 #define stack_undefined_p(s) ((s)==stack_undefined)
57 
58 #define STACK_CHECK(s) \
59  message_assert("stack null or undefined", \
60  !STACK_NULL_P(s) && !stack_undefined_p(s))
61 
62 /* allocation
63  */
64 extern stack stack_make (int, int, int); /* type, bucket_size, policy */
65 extern void stack_free (stack*);
66 extern stack stack_copy (const stack);
67 
68 /* observers
69  */
70 extern int stack_size(const stack);
71 extern int stack_type(const stack);
72 extern int stack_bsize(const stack);
73 extern int stack_policy(const stack);
74 extern int stack_max_extent(const stack);
75 
76 /* miscellaneous
77  */
78 extern int stack_consistent_p(const stack);
79 extern bool stack_empty_p(const stack);
80 extern void stack_info(FILE*, const stack);
81 extern void stack_map(const stack, gen_iter_func_t);
82 
83 /* stack use
84  */
85 extern void stack_push(void*, stack);
86 extern void *stack_pop(stack);
87 extern void *stack_head(const stack);
88 extern void *stack_nth(const stack, int);
89 extern void *stack_replace(void*, stack);
90 
91 /* stack iterator
92  *
93  * This way the stack type is fully encapsulated, but
94  * it is not very efficient, due to the many function calls.
95  * Consider "stack_map" first which has a very small overhead.
96  */
97 extern stack_iterator stack_iterator_init(const stack, bool); /* X-ward */
98 extern bool stack_iterator_next_and_go(stack_iterator, void**);
100 extern bool stack_iterator_end_p(stack_iterator); /* not needed */
101 
102 /* applies _code on the items of _stack downward , with _item of _itemtype.
103  */
104 #define STACK_MAP_X(_item, _itemtype, _code, _stack, _downwards) \
105  { \
106  stack_iterator _i = stack_iterator_init(_stack, _downwards); \
107  void * _vs_item; \
108  while (stack_iterator_next_and_go(_i, &_vs_item)) \
109  { \
110  _itemtype _item = (_itemtype) _vs_item; \
111  _code; \
112  } \
113  stack_iterator_end(&_i); \
114  }
115 
116 #define STACK_MAP(_item, _itemtype, _code, _stack) \
117  STACK_MAP_X(_item, _itemtype, _code, _stack, true)
118 #endif
119 
120 /* That is all
121  */
struct __stack_iterator * stack_iterator
Definition: newgen_stack.h:48
void stack_map(const stack, gen_iter_func_t)
APPLY f to all items of stack s;.
Definition: stack.c:323
void * stack_nth(const stack, int)
returns the nth item starting from the head and counting from 1, when possible, or NULL,...
Definition: stack.c:436
int stack_type(const stack)
bool stack_iterator_end_p(stack_iterator)
Definition: stack.c:179
void stack_info(FILE *, const stack)
Definition: stack.c:342
stack_iterator stack_iterator_init(const stack, bool)
stack iterator
Definition: stack.c:157
bool stack_empty_p(const stack)
int stack_bsize(const stack)
stack stack_copy(const stack)
duplicate a stack with its contents.
Definition: stack.c:267
int stack_policy(const stack)
int stack_consistent_p(const stack)
miscellaneous
int stack_max_extent(const stack)
void * stack_head(const stack)
returns the item on top of stack s
Definition: stack.c:420
int stack_size(const stack)
observers
void stack_push(void *, stack)
stack use
Definition: stack.c:373
void stack_free(stack *)
type, bucket_size, policy
Definition: stack.c:292
void stack_iterator_end(stack_iterator *)
Definition: stack.c:184
stack stack_make(int, int, int)
allocation
Definition: stack.c:246
bool stack_iterator_next_and_go(stack_iterator, void **)
X-ward.
Definition: stack.c:164
void * stack_replace(void *, stack)
REPLACEs the item on top of stack s, and returns the old item.
Definition: stack.c:450
struct __stack_head * stack
STACK MANAGEMENT – headers.
Definition: newgen_stack.h:47
void * stack_pop(stack)
POPs one item from stack s.
Definition: stack.c:399
void(* gen_iter_func_t)(void *)
Definition: newgen_types.h:116
the stack head
Definition: stack.c:62
STACK ITERATOR.
Definition: stack.c:83