~verifypn-stub/verifypn/rulei-segfault

« back to all changes in this revision

Viewing changes to PetriEngine/Structures/BlockStack.h

  • Committer: Jonas Finnemann Jensen
  • Date: 2011-09-15 13:30:00 UTC
  • Revision ID: jopsen@gmail.com-20110915133000-wnywm1odf82emiuw
Import of sources from github

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* PeTe - Petri Engine exTremE
 
2
 * Copyright (C) 2011  Jonas Finnemann Jensen <jopsen@gmail.com>,
 
3
 *                     Thomas Søndersø Nielsen <primogens@gmail.com>,
 
4
 *                     Lars Kærlund Østergaard <larsko@gmail.com>,
 
5
 * 
 
6
 * This program is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 * 
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 * 
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
#ifndef BLOCKSTACK_H
 
20
#define BLOCKSTACK_H
 
21
 
 
22
#include <assert.h>
 
23
 
 
24
namespace PetriEngine{
 
25
namespace Structures{
 
26
 
 
27
/** Block allocating stack */
 
28
template<class Item, size_t blocksize>
 
29
class BlockStack{
 
30
        /** Internal block */
 
31
        struct Block{
 
32
                Block* parent;
 
33
                Item items[blocksize];
 
34
        };
 
35
public:
 
36
        /** Allocate block stack */
 
37
        BlockStack(){
 
38
                assert(blocksize > 0);
 
39
                offset = 0;
 
40
                block = new Block;
 
41
                block->parent = NULL;
 
42
        }
 
43
 
 
44
        /** Release allocated memory */
 
45
        ~BlockStack(){
 
46
                while(block){
 
47
                        Block* b = block;
 
48
                        block = block->parent;
 
49
                        delete b;
 
50
                }
 
51
        }
 
52
 
 
53
        /** Get the top, NULL if none */
 
54
        Item* top(){
 
55
                if(offset)
 
56
                        return &block->items[offset-1];
 
57
                return NULL;
 
58
        }
 
59
 
 
60
        /** Pop item of stack, true if successful */
 
61
        bool pop(){
 
62
                if(offset > 1){
 
63
                        offset--;
 
64
                        return true;
 
65
                }else if(block->parent){
 
66
                        offset = blocksize;
 
67
                        Block* b = block;
 
68
                        block = block->parent;
 
69
                        delete b;
 
70
                        return true;
 
71
                }else if(offset == 1){
 
72
                        offset = 0;
 
73
                        return true;
 
74
                }
 
75
                return false;
 
76
        }
 
77
 
 
78
        /** Push item onto the stack */
 
79
        void push(const Item& item){
 
80
                if(offset == blocksize){
 
81
                        offset = 0;
 
82
                        Block* parent = block;
 
83
                        block = new Block;
 
84
                        block->parent = parent;
 
85
                }
 
86
                block->items[offset++] = item;
 
87
        }
 
88
private:
 
89
        size_t offset;
 
90
        Block* block;
 
91
};
 
92
 
 
93
}
 
94
}
 
95
 
 
96
#endif /* BLOCKSTACK_H */