1 /* vifm
2  * Copyright (C) 2012 xaizek.
3  *
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
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #include "int_stack.h"
20 
21 #include <assert.h> /* assert() */
22 #include <stddef.h> /* size_t */
23 #include <stdlib.h> /* realloc() */
24 
25 static int ensure_available(int_stack_t *stack);
26 
27 int
int_stack_is_empty(const int_stack_t * stack)28 int_stack_is_empty(const int_stack_t *stack)
29 {
30 	return stack->top == 0;
31 }
32 
33 int
int_stack_get_top(const int_stack_t * stack)34 int_stack_get_top(const int_stack_t *stack)
35 {
36 	assert(!int_stack_is_empty(stack));
37 
38 	return stack->data[stack->top - 1];
39 }
40 
41 int
int_stack_top_is(const int_stack_t * stack,int val)42 int_stack_top_is(const int_stack_t *stack, int val)
43 {
44 	return !int_stack_is_empty(stack) && int_stack_get_top(stack) == val;
45 }
46 
47 void
int_stack_set_top(const int_stack_t * stack,int val)48 int_stack_set_top(const int_stack_t *stack, int val)
49 {
50 	assert(!int_stack_is_empty(stack));
51 
52 	stack->data[stack->top - 1] = val;
53 }
54 
55 int
int_stack_push(int_stack_t * stack,int val)56 int_stack_push(int_stack_t *stack, int val)
57 {
58 	if(ensure_available(stack) == 0)
59 	{
60 		stack->data[stack->top++] = val;
61 		return 0;
62 	}
63 	return 1;
64 }
65 
66 /* Ensures that there is a place for one more element in the stack.  Returns
67  * non-zero if not enough memory. */
68 static int
ensure_available(int_stack_t * stack)69 ensure_available(int_stack_t *stack)
70 {
71 	if(stack->top == stack->len)
72 	{
73 		int *const ptr = realloc(stack->data, sizeof(int)*(stack->len + 1));
74 		if(ptr != NULL)
75 		{
76 			stack->data = ptr;
77 			++stack->len;
78 		}
79 	}
80 	return (stack->top < stack->len) ? 0 : 1;
81 }
82 
83 void
int_stack_pop(int_stack_t * stack)84 int_stack_pop(int_stack_t *stack)
85 {
86 	assert(!int_stack_is_empty(stack));
87 
88 	--stack->top;
89 }
90 
91 void
int_stack_pop_seq(int_stack_t * stack,int seq_guard)92 int_stack_pop_seq(int_stack_t *stack, int seq_guard)
93 {
94 	while(--stack->top > 0 && stack->data[stack->top] != seq_guard)
95 	{
96 		/* Do nothing. */
97 	}
98 }
99 
100 void
int_stack_clear(int_stack_t * stack)101 int_stack_clear(int_stack_t *stack)
102 {
103 	stack->top = 0U;
104 }
105 
106 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
107 /* vim: set cinoptions+=t0 filetype=c : */
108