File: order_and_law.hpp

package info (click to toggle)
spades 3.13.1+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 22,172 kB
  • sloc: cpp: 136,213; ansic: 48,218; python: 16,809; perl: 4,252; sh: 2,115; java: 890; makefile: 507; pascal: 348; xml: 303
file content (336 lines) | stat: -rw-r--r-- 7,766 bytes parent folder | download | duplicates (4)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//***************************************************************************
//* Copyright (c) 2015 Saint Petersburg State University
//* Copyright (c) 2011-2014 Saint Petersburg Academic University
//* All Rights Reserved
//* See file LICENSE for details.
//***************************************************************************

#pragma once

#include "utils/stacktrace.hpp"
#include "utils/verify.hpp"
#include "utils/parallel/openmp_wrapper.h"
#include "folly/PackedSyncPtr.h"

#include <boost/utility.hpp>

#include <ostream>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <map>

namespace restricted {

//todo discuss with Anton
static const uint16_t MAX_THREAD_CNT = 128;

class IdDistributor {
public:
    virtual size_t GetId() = 0;

    virtual ~IdDistributor() {
    }
};

template<class Iter>
class ListIdDistributor : public IdDistributor {
    friend class IdSegmentStorage;

private:
    Iter left_;
    Iter right_;
    size_t shift_;
    size_t max_;

    ListIdDistributor(Iter left, Iter right, size_t shift = 0, size_t max = size_t(-1)) : left_(left),
                                                                                          right_(right),
                                                                                          shift_(shift), max_(max) {
    }

public:
    bool valid() {
        return left_ < right_;
    }

    size_t GetId() {
        size_t result = *(left_);
        VERIFY(result < max_);
        ++left_;
        return shift_ + result;
    }
};

class SegmentIterator {
private:
    size_t value_;
public:
    SegmentIterator(size_t value) : value_(value) {
    }

    size_t operator*() const {
        return value_;
    }

    void operator++() {
        value_++;
    }

    void operator++(int) {
        ++value_;
    }

    bool operator==(const SegmentIterator &that) const {
        return value_ == that.value_;
    }

    bool operator!=(const SegmentIterator &that) const {
        return value_ != that.value_;
    }
};

class IdSegmentStorage {
    friend class LocalIdDistributor;

public:
    ListIdDistributor<SegmentIterator> GetSegmentIdDistributor(size_t left, size_t right) {
        VERIFY(left < right);
        VERIFY(right <= size_);
        return ListIdDistributor<SegmentIterator>(SegmentIterator(left), SegmentIterator(right), min_value_, size_);
    }

    template<class Iter>
    ListIdDistributor<Iter> GetSegmentIdDistributor(Iter left, Iter right) {
        VERIFY(left < right);
        return ListIdDistributor<Iter>(left, right, min_value_, size_);
    }

private:
    IdSegmentStorage(size_t min_value, size_t size) : min_value_(min_value), size_(size) { }

    const size_t min_value_;
    const size_t size_;
};

// Id distributor for pure_pointer. Singleton.
class LocalIdDistributor : public IdDistributor, boost::noncopyable {
public:
    size_t GetId() {
        return next_int_id_++;
    }

    //force_zero_shift should only be used for loading from saves
    IdSegmentStorage Reserve(size_t size, bool force_zero_shift = false) {
        if (force_zero_shift)
            next_int_id_ = 0;
        next_int_id_ += size;
        return IdSegmentStorage(next_int_id_ - size, size);
    }

    size_t GetMax() const {
        return next_int_id_;
    }

    LocalIdDistributor() : next_int_id_(/*min int id*/2) { }

private:
    size_t next_int_id_;
};

template<class PurePtrT>
class PurePtrLock;

template<class PurePtrT>
class PurePtrMarker;

//todo maybe make it extend folly::PackedSyncPtr<T>?
template<class T>
struct pure_pointer {
    typedef T type;
    typedef T *pointer_type;

    explicit pure_pointer()
            : int_id_(0) {
        ptr_.init(pointer_type(0), MAX_THREAD_CNT);
    }

    explicit pure_pointer(T *ptr, IdDistributor &idDistributor)
            : int_id_(generate_id(ptr, idDistributor)) {
        ptr_.init(ptr, MAX_THREAD_CNT);
    }

//    lock_pointer_type& get_lockable() {
//        return ptr_;
//    }

    T *get() const {
        return ptr_.get();
    }

    T &operator*() const {
        return *ptr_;
    }

    T *operator->() const {
        return ptr_.get();
    }

    bool operator==(const pure_pointer &rhs) const {
        if (int_id_ == rhs.int_id_) {
            VERIFY(ptr_.get() == rhs.ptr_.get());
            return true;
        }
        return false;
    }

    bool operator!=(const pure_pointer &rhs) const {
        return !operator==(rhs);
    }

    bool operator<(const pure_pointer &rhs) const {
        return this->int_id_ < rhs.int_id_;
    }

    bool operator<=(const pure_pointer &rhs) const {
        return *this < rhs || *this == rhs;
    }

    size_t hash() const {
        return this->int_id_;
    }

    size_t int_id() const {
        return int_id_;
    }

private:
    friend class PurePtrLock<pure_pointer<T>>;

    friend class PurePtrMarker<pure_pointer<T>>;

    typedef folly::PackedSyncPtr<T> lock_pointer_type;

    static size_t generate_id(T *ptr, IdDistributor &idDistributor) {
        if (ptr == 0 || ptr == (T *) 1 || ptr == (T *) (-1)) {
            return size_t(ptr);
        }

        return idDistributor.GetId();
    }

    lock_pointer_type ptr_;

    size_t int_id_;
};

template<class LockT>
class ReEnteringLock {
    LockT &lock_;
    bool reentered_;

    uint16_t locking_thread() const {
        //don't need barrier here (as folly documentation says)
        return lock_.extra();
    }

    uint16_t current_thread() const {
        return uint16_t(omp_get_thread_num());
    }

    void Lock() {
        lock_.lock();
        lock_.setExtra(current_thread());
    }

    void Unlock() {
        lock_.setExtra(MAX_THREAD_CNT);
        lock_.unlock();
    }

public:
    ReEnteringLock(LockT &lock) :
            lock_(lock),
            reentered_(false) {
        if (locking_thread() == current_thread()) {
            reentered_ = true;
        } else {
            Lock();
        }
    }

    ~ReEnteringLock() {
        if (!reentered_) {
            Unlock();
        }
    }
};

/**
* Lock that uses a pure ptr as a target.
* Be careful NOT to pass a COPY of pure ptr you want to use as locked object!
*/
template<class PurePtrT>
class PurePtrLock {
    ReEnteringLock<typename PurePtrT::lock_pointer_type> inner_lock_;

public:
    PurePtrLock(PurePtrT &pure_ptr) :
            inner_lock_(pure_ptr.ptr_) {
    }

};

/**
* Way to "mark" pure pointer without using additional memory.
* Marking/unmarking operations are atomic
* Be careful NOT to pass a COPY of pure ptr you want to mark!
* Do not use with PurePtrLocks, they use the same space for storing data...
*/
template<class PurePtrT>
class PurePtrMarker {
    typedef typename PurePtrT::lock_pointer_type LockWithData;

    void ChangeMark(PurePtrT &pure_ptr, uint16_t new_mark) const {
        LockWithData &lock_with_data = pure_ptr.ptr_;
        lock_with_data.lock();
        lock_with_data.setExtra(new_mark);
        lock_with_data.unlock();
    }

public:

    void mark(PurePtrT &pure_ptr) const {
        ChangeMark(pure_ptr, 0);
    }

    void unmark(PurePtrT &pure_ptr) const {
        ChangeMark(pure_ptr, MAX_THREAD_CNT);
    }

    bool is_marked(const PurePtrT &pure_ptr) const {
        uint16_t curr_mark = pure_ptr.ptr_.extra();
        VERIFY(curr_mark == 0 || curr_mark == MAX_THREAD_CNT);
        return curr_mark == 0;
    }

};

template<class T>
std::ostream &operator<<(std::ostream &stream, const pure_pointer<T> &pointer) {
    stream << pointer.int_id();
    return stream;
}

} // namespace restricted

namespace std {

template<class T>
struct hash<restricted::pure_pointer<T>> {
    size_t operator()(const restricted::pure_pointer<T> &pointer) const {
        return pointer.hash();
    }
};

}