cdcontainers  0.1.1
Library of data containers and collections for C programming language.
stack.h
Go to the documentation of this file.
1 // The MIT License (MIT)
2 // Copyright (c) 2017 Maksim Andrianov
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to
6 // deal in the Software without restriction, including without limitation the
7 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 // sell copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 // IN THE SOFTWARE.
26 #ifndef CDCONTAINERS_INCLUDE_CDCONTAINERS_STACK_H
27 #define CDCONTAINERS_INCLUDE_CDCONTAINERS_STACK_H
28 
29 #include <cdcontainers/common.h>
30 #include <cdcontainers/status.h>
32 
33 #include <assert.h>
34 #include <stdarg.h>
35 #include <stdbool.h>
36 #include <stdlib.h>
37 
43 struct cdc_stack {
44  void *container;
45  const struct cdc_sequence_table *table;
46 };
47 
57  struct cdc_stack **s, struct cdc_data_info *info);
58 
69  struct cdc_stack **s, struct cdc_data_info *info,
70  ...);
71 
81  struct cdc_stack **s, struct cdc_data_info *info,
82  va_list args);
83 
88 void cdc_stack_dtor(struct cdc_stack *s);
89 
90 // Element access
97 static inline void *cdc_stack_top(struct cdc_stack *s)
98 {
99  assert(s != NULL);
100 
101  return s->table->back(s->container);
102 }
103 
104 // Capacity
110 static inline bool cdc_stack_empty(struct cdc_stack *s)
111 {
112  assert(s != NULL);
113 
114  return s->table->empty(s->container);
115 }
116 
122 static inline size_t cdc_stack_size(struct cdc_stack *s)
123 {
124  assert(s != NULL);
125 
126  return s->table->size(s->container);
127 }
128 
129 // Modifiers
137 static inline enum cdc_stat cdc_stack_push(struct cdc_stack *s, void *elem)
138 {
139  assert(s != NULL);
140 
141  return s->table->push_back(s->container, elem);
142 }
143 
149 static inline void cdc_stack_pop(struct cdc_stack *s)
150 {
151  assert(s != NULL);
152 
153  s->table->pop_back(s->container);
154 }
155 
161 void cdc_stack_swap(struct cdc_stack *a, struct cdc_stack *b);
162 
163 // Short names
164 #ifdef CDC_USE_SHORT_NAMES
165 typedef struct cdc_stack cstack_t;
166 
167 #define stack_ctor(...) cdc_stack_ctor(__VA_ARGS__)
168 #define stack_ctorl(...) cdc_stack_ctorl(__VA_ARGS__)
169 #define stack_ctorv(...) cdc_stack_ctorv(__VA_ARGS__)
170 #define stack_dtor(...) cdc_stack_dtor(__VA_ARGS__)
171 
172 // Element access
173 #define stack_top(...) cdc_stack_top(__VA_ARGS__)
174 
175 // Capacity
176 #define stack_empty(...) cdc_stack_empty(__VA_ARGS__)
177 #define stack_size(...) cdc_stack_size(__VA_ARGS__)
178 
179 // Modifiers
180 #define stack_push(...) cdc_stack_push(__VA_ARGS__)
181 #define stack_pop(...) cdc_stack_pop(__VA_ARGS__)
182 #define stack_swap(...) cdc_stack_swap(__VA_ARGS__)
183 #endif
184 
185 #endif // CDCONTAINERS_INCLUDE_CDCONTAINERS_STACK_H
void(* pop_back)(void *cntr)
Definition: isequence.h:50
enum cdc_stat cdc_stack_ctorl(const struct cdc_sequence_table *table, struct cdc_stack **s, struct cdc_data_info *info,...)
Constructs a stack, initialized by an arbitrary number of pointers. The last item must be NULL...
The cdc_stack struct.
Definition: stack.h:43
void cdc_stack_dtor(struct cdc_stack *s)
Destroys the stack.
enum cdc_stat cdc_stack_ctorv(const struct cdc_sequence_table *table, struct cdc_stack **s, struct cdc_data_info *info, va_list args)
Constructs a stack, initialized by args. The last item must be NULL.
const struct cdc_sequence_table * table
Definition: stack.h:45
static void cdc_stack_pop(struct cdc_stack *s)
Removes the top item from the stack. This function assumes that the stack isn&#39;t empty.
Definition: stack.h:149
static bool cdc_stack_empty(struct cdc_stack *s)
Returns true if the stack has size 0; otherwise returns false.
Definition: stack.h:110
enum cdc_stat cdc_stack_ctor(const struct cdc_sequence_table *table, struct cdc_stack **s, struct cdc_data_info *info)
Constructs an empty stack.
void *(* back)(void *cntr)
Definition: isequence.h:46
The cdc_sequence_table is a sequence container interface.
bool(* empty)(void *cntr)
Definition: isequence.h:47
cdc_stat
Definition: status.h:24
void cdc_stack_swap(struct cdc_stack *a, struct cdc_stack *b)
Swaps stack a and b. This operation is very fast and never fails.
static enum cdc_stat cdc_stack_push(struct cdc_stack *s, void *elem)
Adds element elem to the top of the stack.
Definition: stack.h:137
size_t(* size)(void *cntr)
Definition: isequence.h:48
static size_t cdc_stack_size(struct cdc_stack *s)
Returns the number of items in the stack.
Definition: stack.h:122
enum cdc_stat(* push_back)(void *cntr, void *elem)
Definition: isequence.h:49
static void * cdc_stack_top(struct cdc_stack *s)
Returns a pointer to the stack&#39;s top item. This function assumes that the stack isn&#39;t empty...
Definition: stack.h:97
void * container
Definition: stack.h:44
The cdc_data_info struct used to initialize contaners.
Definition: common.h:71
The cdc_sequence_table struct.
Definition: isequence.h:41