~tapaal-contributor/verifypn/unfoldTACPN

« back to all changes in this revision

Viewing changes to PetriEngine/Structures/ptrie_stable.h

  • Committer: Mark Glavind
  • Date: 2019-03-20 14:58:42 UTC
  • Revision ID: mglavi14@student.aau.dk-20190320145842-sxpl9pwrg0706q7m
WIP on clearing out files not needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* VerifyPN - TAPAAL Petri Net Engine
2
 
 * Copyright (C) 2016  Peter Gjøl Jensen <root@petergjoel.dk>
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 3 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, see <http://www.gnu.org/licenses/>.
16
 
 */
17
 
 
18
 
/* 
19
 
 * File:   ptrie_map.h
20
 
 * Author: Peter G. Jensen
21
 
 *
22
 
 *
23
 
 * Created on 06 October 2016, 13:51
24
 
 */
25
 
 
26
 
#ifndef PTRIE_MAP_H
27
 
#define PTRIE_MAP_H
28
 
#include "ptrie.h"
29
 
 
30
 
 
31
 
namespace ptrie {
32
 
 
33
 
    template<
34
 
    uint16_t HEAPBOUND = 128,
35
 
    uint16_t SPLITBOUND = 128,
36
 
    size_t ALLOCSIZE = (1024 * 64),
37
 
    typename T = void,
38
 
    typename I = size_t
39
 
    >
40
 
    class set_stable : public set<HEAPBOUND, SPLITBOUND, ALLOCSIZE, T, I> {
41
 
#ifdef __APPLE__
42
 
#define pt set<HEAPBOUND, SPLITBOUND, ALLOCSIZE, T, I>
43
 
#else
44
 
        using pt = set<HEAPBOUND, SPLITBOUND, ALLOCSIZE, T, I>;
45
 
#endif
46
 
    public:
47
 
        set_stable() : pt()
48
 
        {
49
 
            this->_entries = new linked_bucket_t<typename pt::entry_t, ALLOCSIZE>(1);
50
 
        }
51
 
 
52
 
        size_t size() const {
53
 
            return this->_entries->size();
54
 
        }
55
 
 
56
 
        size_t unpack(I index, uchar* destination);
57
 
  };
58
 
 
59
 
    template<PTRIETPL>
60
 
    size_t
61
 
    set_stable<HEAPBOUND, SPLITBOUND, ALLOCSIZE, T, I>::unpack(I index, uchar* destination) {
62
 
        typename pt::node_t* node = NULL;
63
 
        typename pt::fwdnode_t* par = NULL;
64
 
        // we can find size without bothering anyone (to much)        
65
 
        std::stack<uchar> path;
66
 
        size_t bindex = 0;
67
 
        {
68
 
#ifndef NDEBUG
69
 
            bool found = false;
70
 
#endif
71
 
            typename pt::entry_t& ent = this->_entries->operator[](index);
72
 
            par = (typename pt::fwdnode_t*)ent.node;
73
 
            node = (typename pt::node_t*)par->_children[ent.path];
74
 
            typename pt::bucket_t* bckt = node->_data;
75
 
            I* ents = bckt->entries(node->_count, true);
76
 
            for (size_t i = 0; i < node->_count; ++i) {
77
 
                if (ents[i] == index) {
78
 
                    bindex = i;
79
 
#ifndef NDEBUG
80
 
                    found = true;
81
 
#endif
82
 
                    break;
83
 
                }
84
 
            }
85
 
            assert(found);
86
 
        }
87
 
 
88
 
 
89
 
 
90
 
        while (par != this->_root) {
91
 
            path.push(par->_path);
92
 
            par = par->_parent;
93
 
        }
94
 
 
95
 
        uint16_t size = 0;
96
 
        size_t offset = 0;
97
 
        size_t ps = path.size();
98
 
        if (ps <= 1) {
99
 
            size = node->_data->first(0, bindex);
100
 
            if (ps == 1) {
101
 
                size >>= 8;
102
 
                uchar* bs = (uchar*) & size;
103
 
                bs[1] = path.top();
104
 
                path.pop();
105
 
            }
106
 
 
107
 
            uint16_t o = size;
108
 
            for (size_t i = 0; i < bindex; ++i) {
109
 
 
110
 
                uint16_t f = node->_data->first(0, i);
111
 
                uchar* fc = (uchar*) & f;
112
 
                uchar* oc = (uchar*) & o;
113
 
                if (ps != 0) {
114
 
                    f >>= 8;
115
 
                    fc[1] = oc[1];
116
 
                    f -= 1;
117
 
                }
118
 
                offset += pt::bytes(f);
119
 
                //                assert(bytes(f) == nbucket->bytes(i));
120
 
            }
121
 
        } else {
122
 
            uchar* bs = (uchar*) & size;
123
 
            bs[1] = path.top();
124
 
            path.pop();
125
 
            bs[0] = path.top();
126
 
            path.pop();
127
 
            offset = (pt::bytes(size - ps) * bindex);
128
 
        }
129
 
 
130
 
 
131
 
        if (size > ps) {
132
 
            uchar* src;
133
 
            if ((size - ps) >= HEAPBOUND) {
134
 
                src = *((uchar**)&(node->_data->data(node->_count, true)[offset]));
135
 
            } else {
136
 
                src = &(node->_data->data(node->_count, true)[offset]);
137
 
            }
138
 
 
139
 
            memcpy(&(destination[ps]), src, (size - ps));
140
 
        }
141
 
 
142
 
        uint16_t first = node->_data->first(0, bindex);
143
 
 
144
 
        size_t pos = 0;
145
 
        while (!path.empty()) {
146
 
            destination[pos] = path.top();
147
 
            path.pop();
148
 
            ++pos;
149
 
        }
150
 
 
151
 
 
152
 
        if (ps > 0) {
153
 
            uchar* fc = (uchar*) & first;
154
 
            if (ps > 1) {
155
 
                destination[pos] = fc[1];
156
 
                ++pos;
157
 
            }
158
 
            destination[pos] = fc[0];
159
 
            ++pos;
160
 
        }
161
 
        
162
 
        return size;
163
 
    }
164
 
}
165
 
 
166
 
#undef pt
167
 
#endif /* PTRIE_MAP_H */
 
 
b'\\ No newline at end of file'