1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
13 * Szabo, Janos Zoltan – initial implementation
15 ******************************************************************************/
16 #ifndef _Common_stack_HH
17 #define _Common_stack_HH
20 #include "../common/memory.h"
23 * LIFO container optimized to store elements sequentially,
24 * and access only the element on the top of the stack.
25 * Accessing the top of the stack has amortized constant time cost.
27 * The container stores pointers to objects of type T; it doesn't own them.
28 * It is the responsibility of the caller to create and destroy the objects
29 * and supply the pointers to the container.
38 size_t num_s; ///< used size
39 size_t max_s; ///< allocated size
40 T **s_ptr; ///< array of pointers to objects of type T
42 static const size_t initial_size = 1, increment_factor = 2;
44 /** Copy constructor: DO NOT IMPLEMENT! */
46 /** Copy assignment: DO NOT IMPLEMENT! */
47 stack& operator=(const stack&);
51 static const size_t max_stack_length = -1;
53 /** Creates an empty stack. */
54 stack() : num_s(0), max_s(0), s_ptr(NULL) { }
56 /** Deallocates its memory. If the container is not empty,
57 * FATAL_ERROR occurs, so before destructing, clear()
58 * must be invoked explicitly.
61 if(num_s > 0) FATAL_ERROR("stack::~stack(): stack is not empty");
65 /** Returns the number of elements in the container. */
66 size_t size() const { return num_s; }
68 /** Returns true if the container has no elements. */
69 bool empty() const { return num_s == 0; }
71 /** Forgets all elements in the container.
72 * No memory is freed. */
73 void clear() { num_s = 0; }
75 /** Push the elem onto the stack. */
80 s_ptr = static_cast<T**>(Malloc(initial_size * sizeof(*s_ptr)));
82 if(max_s >= max_stack_length / increment_factor)
83 FATAL_ERROR("stack::push(): stack index overflow");
84 max_s *= increment_factor;
85 s_ptr = static_cast<T**>(Realloc(s_ptr, max_s * sizeof(*s_ptr)));
88 s_ptr[num_s++] = elem;
91 /** Returns the top of the stack or FATAL_ERROR if empty. */
93 if(num_s == 0) FATAL_ERROR("stack::top() const: stack is empty");
94 return s_ptr[num_s - 1];
97 /** Returns the top of the stack, and removes it from the
98 * container. If the stack is empty then FATAL_ERROR occurs. */
100 if(num_s == 0) FATAL_ERROR("stack::pop(): stack is empty");
101 return s_ptr[--num_s];
106 #endif // _Common_stack_HH