summaryrefslogtreecommitdiff
path: root/utils/tfstats/regexp/include/jm/jstack.h
blob: fca50584305667e696fe5390e3054297d8ba3e87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//
/*
 *
 * Copyright (c) 1998-9
 * Dr John Maddock
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Dr John Maddock makes no representations
 * about the suitability of this software for any purpose.  
 * It is provided "as is" without express or implied warranty.
 *
 */

  /*
  *   FILE     jstack.h
  *   VERSION  2.12
  */

#ifndef __JSTACH_H
#define __JSTACK_H

#ifndef JM_CFG_H
#include <jm/jm_cfg.h>
#endif

JM_NAMESPACE(__JM)

//
// class jstack
// simplified stack optimised for push/peek/pop
// operations, we could use std::stack<std::vector<T>> instead...
//
template <class T, class Allocator JM_DEF_ALLOC_PARAM(T) >
class jstack
{
private:
   typedef JM_MAYBE_TYPENAME REBIND_TYPE(unsigned char, Allocator) alloc_type;
   typedef typename REBIND_TYPE(T, Allocator)::size_type size_type;
   struct node
   {
      node* next;
      T* start;  // first item
      T* end;    // last item
      T* last;   // end of storage
   };
   
   //
   // empty base member optimisation:
   struct data : public alloc_type
   {
      unsigned char buf[sizeof(T)*16];
      data(const Allocator& a) : alloc_type(a){}
   };

   data alloc_inst;
   mutable node* stack;
   mutable node* unused;
   node base;
   size_type block_size;

   void RE_CALL pop_aux()const;
   void RE_CALL push_aux();

public:
   jstack(size_type n = 64, const Allocator& a = Allocator());

   ~jstack();

   node* RE_CALL get_node()
   {
      node* new_stack = (node*)alloc_inst.allocate(sizeof(node) + sizeof(T) * block_size);
      new_stack->last = (T*)(new_stack+1);
      new_stack->start = new_stack->end = new_stack->last + block_size;
      new_stack->next = 0;
      return new_stack;
   }

   bool RE_CALL empty()
   {
      return (stack->start == stack->end) && (stack->next == 0);
   }

   bool RE_CALL good()
   {
      return (stack->start != stack->end) || (stack->next != 0);
   }

   T& RE_CALL peek()
   {
      if(stack->start == stack->end)
         pop_aux();
      return *stack->end;
   }

   const T& RE_CALL peek()const
   {
      if(stack->start == stack->end)
         pop_aux();
      return *stack->end;
   }

   void RE_CALL pop()
   {
      if(stack->start == stack->end)
         pop_aux();
      jm_destroy(stack->end);
      ++(stack->end);
   }

   void RE_CALL pop(T& t)
   {
      if(stack->start == stack->end)
         pop_aux();
      t = *stack->end;
      jm_destroy(stack->end);
      ++(stack->end);
   }

   void RE_CALL push(const T& t)
   {
      if(stack->end == stack->last)
         push_aux();
      --(stack->end);
      jm_construct(stack->end, t);
   }

};

template <class T, class Allocator>
jstack<T, Allocator>::jstack(size_type n, const Allocator& a)
    : alloc_inst(a)
{
  unused = 0;
  block_size = n;
  stack = &base;
  base.last = (T*)alloc_inst.buf;
  base.end = base.start = base.last + 16;
  base.next = 0;
}

template <class T, class Allocator>
void RE_CALL jstack<T, Allocator>::push_aux()
{
   // make sure we have spare space on TOS:
   register node* new_node;
   if(unused)
   {
      new_node = unused;
      unused = new_node->next;
      new_node->next = stack;
      stack = new_node;
   }
   else
   {
      new_node = get_node();
      new_node->next = stack;
      stack = new_node;
   }
}

template <class T, class Allocator>
void RE_CALL jstack<T, Allocator>::pop_aux()const
{
   // make sure that we have a valid item
   // on TOS:
   jm_assert(stack->next);
   register node* p = stack;
   stack = p->next;
   p->next = unused;
   unused = p;
}

template <class T, class Allocator>
jstack<T, Allocator>::~jstack()
{
   node* condemned;
   while(good())
      pop();
   while(unused)
   {
      condemned = unused;
      unused = unused->next;
      alloc_inst.deallocate((unsigned char*)condemned, sizeof(node) + sizeof(T) * block_size);
   }
   while(stack != &base)
   {
      condemned = stack;
      stack = stack->next;
      alloc_inst.deallocate((unsigned char*)condemned, sizeof(node) + sizeof(T) * block_size);
   }
}

JM_END_NAMESPACE

#endif