mn_stack.hpp
Go to the documentation of this file.
1 // *** ADDED BY HEADER FIXUP ***
2 #include <cstdlib>
3 // *** END ***
4 /*
5 *This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
6 *Copyright (c) 2020 Amber-Sophia Schroeck
7 *
8 *The Mini Thread Library is free software; you can redistribute it and/or modify
9 *it under the terms of the GNU Lesser General Public License as published by
10 *the Free Software Foundation, version 3, or (at your option) any later version.
11 
12 *The Mini Thread Library is distributed in the hope that it will be useful, but
13 *WITHOUT ANY WARRANTY; without even the implied warranty of
14 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 *General Public License for more details.
16 *
17 *You should have received a copy of the GNU Lesser General Public
18 *License along with the Mini Thread Library; if not, see
19 *<https://www.gnu.org/licenses/>.
20 */
21 #ifndef _MINLIB_STAK_H_
22 #define _MINLIB_STAK_H_
23 
24 #include "../mn_config.hpp"
25 #include "../mn_error.hpp"
26 
27 #include <stdio.h>
28 #include <malloc.h>
29 #include <stdint.h>
30 
31 namespace mn {
32  namespace container {
37  template<typename TType>
38  class basic_stack {
39  public:
40  using basic_type_t = TType;
41 
46  explicit basic_stack(size_t nSize) {
47  internal_create(nSize, (basic_type_t*)malloc( size_of(basic_type_t) * nSize), 0 ); }
48 
54  basic_stack(size_t nSize, basic_type_t* addr) {
55  internal_create(nSize, addr, 0); }
56 
63  basic_stack(size_t nSize, basic_type_t* addr, int offset) {
64  internal_create(nSize, addr, offset); }
65 
73  int push(basic_type_t item) {
74  int _ret = ERR_MNTHREAD_UNKN;
75 
76  if(m_iCurrent < m_iLast - 1) {
77  m_iCurrent++;
78  m_ulAdrr[m_iCurrent] = item;
79  _ret = NO_ERROR;
80  }
81  return _ret;
82  }
90  int pop(basic_type_t* item) {
91  if(item == NULL) return ERR_NULL;
92 
93  if(count() > 0 ) {
95  m_iCurrent--;
96  *item = x;
97  return NO_ERROR;
98  }
99  return ERR_MNTHREAD_UNKN;
100  }
109  int peek(basic_type_t* item) {
110  if(item == NULL) return ERR_NULL;
111 
112  if(count() > 0 ) {
114  *item = x;
115  return NO_ERROR;
116  }
117  return ERR_MNTHREAD_UNKN;
118  }
122  void reset() {
123  internal_reset();
124  }
125 
130  int count() { return m_iCurrent + 1; }
135  int size() { return m_ulSize; }
136 
141  bool is_empty() { return (m_iCurrent == -1); }
146  bool is_full() { return (m_iCurrent == (m_iLast - 1) ); }
147 
151  int get_left() { return m_ulSize - (m_iCurrent + 1); }
152  private:
159  void internal_create(size_t nSize, basic_type_t* addr, int offset) {
160  m_ulAdrr = (addr);
161  m_ulSize = (nSize - offset);
162 
163  m_iFirst = (offset - 1);
164  m_iCurrent = (m_iFirst);
165  m_iLast = (nSize);
166  }
170  void internal_reset() {
171  while(!is_empty())
172  pop();
174  }
175  private:
177  size_t m_ulSize;
178  size_t m_iFirst;
179  size_t m_iLast;
180  size_t m_iCurrent;
181  };
182 
184 
189 
194 
197  }
198 }
199 
200 #endif
Definition: mn_stack.hpp:38
int count()
Definition: mn_stack.hpp:130
int size()
Definition: mn_stack.hpp:135
bool is_full()
Definition: mn_stack.hpp:146
int pop(basic_type_t *item)
Definition: mn_stack.hpp:90
basic_stack(size_t nSize)
Definition: mn_stack.hpp:46
size_t m_ulSize
Definition: mn_stack.hpp:177
void internal_reset()
Definition: mn_stack.hpp:170
int get_left()
Definition: mn_stack.hpp:151
size_t m_iLast
Definition: mn_stack.hpp:179
int push(basic_type_t item)
Definition: mn_stack.hpp:73
void reset()
Definition: mn_stack.hpp:122
int peek(basic_type_t *item)
Definition: mn_stack.hpp:109
size_t m_iFirst
Definition: mn_stack.hpp:178
TType basic_type_t
Definition: mn_stack.hpp:40
bool is_empty()
Definition: mn_stack.hpp:141
basic_type_t * m_ulAdrr
Definition: mn_stack.hpp:176
size_t m_iCurrent
Definition: mn_stack.hpp:180
basic_stack(size_t nSize, basic_type_t *addr, int offset)
Definition: mn_stack.hpp:63
basic_stack(size_t nSize, basic_type_t *addr)
Definition: mn_stack.hpp:54
void internal_create(size_t nSize, basic_type_t *addr, int offset)
Definition: mn_stack.hpp:159
#define NO_ERROR
Definition: mn_error.hpp:53
#define ERR_MNTHREAD_UNKN
Definition: mn_error.hpp:57
Definition: mn_allocator_typetraits.hpp:25