File: sorted_int_stack.hpp

package info (click to toggle)
seqan3 3.0.2%2Bds-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,052 kB
  • sloc: cpp: 144,641; makefile: 1,288; ansic: 294; sh: 228; xml: 217; javascript: 50; python: 27; php: 25
file content (200 lines) | stat: -rw-r--r-- 5,839 bytes parent folder | download
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
// Copyright (c) 2016, the SDSL Project Authors.  All rights reserved.
// Please see the AUTHORS file for details.  Use of this source code is governed
// by a BSD license that can be found in the LICENSE file.
/*! \file sorted_int_stack.hpp
    \brief sorted_int_stack.hpp contains a data structure for a stack which can
    contain numbers in strictly increasing order.
    \author Simon Gog
*/
#ifndef INCLUDED_SDSL_SORTED_INT_STACK
#define INCLUDED_SDSL_SORTED_INT_STACK

#include "int_vector.hpp"
#include <vector>

//! Namespace for the succinct data structure library.
namespace sdsl {

//! A stack class which can contain integers in strictly increasing order.
/*! \par Space complexity
 *  \f$n+o(n)\f$ bits + 64 bits for every stored number > n-1.
 */
class sorted_int_stack {
public:
	typedef int_vector<64>::size_type size_type;

private:
	size_type			   m_n;		   // maximal value which can be stored on the stack
	size_type			   m_cnt;	  // counter for elements on the stack
	size_type			   m_top;	  // top element of the stack
	int_vector<64>		   m_stack;	// memory for the stack
	std::vector<size_type> m_overflow; // memory for the elements which are greater than n

	inline size_type block_nr(size_type x)
	{
		return x / 63;
	}; // maybe we can speed this up with bit hacks
	inline size_type block_pos(size_type x)
	{
		return x % 63;
	}; // maybe we can speed this up with bit hacks
public:
	sorted_int_stack(size_type n);
	sorted_int_stack(const sorted_int_stack&) = default;
	sorted_int_stack(sorted_int_stack&&)	  = default;
	sorted_int_stack& operator=(const sorted_int_stack&) = default;
	sorted_int_stack& operator=(sorted_int_stack&&) = default;

	/*! Returns if the stack is empty.
         */
	bool empty() const { return 0 == m_cnt; };

	/*! Returns the topmost element of the stack.
         * \pre empty()==false
         */
	size_type top() const;

	/*! Pop the topmost element of the stack.
         */
	void pop();

	/*! Push value x on the stack.
         * \par x Value which should be pushed onto the stack.
         * \pre top() < x
         */
	void push(size_type x);

	/*! Returns the number of element is the stack.
         */
	size_type size() const { return m_cnt; };

	size_type
	serialize(std::ostream& out, structure_tree_node* v = nullptr, std::string name = "") const;
	void load(std::istream& in);
	template <typename archive_t>
	void CEREAL_SAVE_FUNCTION_NAME(archive_t & ar) const;
	template <typename archive_t>
	void CEREAL_LOAD_FUNCTION_NAME(archive_t & ar);
	bool operator==(sorted_int_stack const & other) const noexcept;
	bool operator!=(sorted_int_stack const & other) const noexcept;
};

inline sorted_int_stack::sorted_int_stack(size_type n) : m_n(n), m_cnt(0), m_top(0)
{
	m_stack	= int_vector<64>(block_nr(n) + 2, 0);
	m_stack[0] = 1;
}

inline sorted_int_stack::size_type sorted_int_stack::top() const { return m_top - 63; }

inline void sorted_int_stack::push(size_type x)
{
	x += 63;
	assert(empty() || m_top < x);
	++m_cnt; //< increment counter
	if (x > m_n + 63) {
		if (m_overflow.empty()) {
			m_overflow.push_back(m_top);
		}
		m_overflow.push_back(x);
		m_top = x;
	} else {
		size_type bn = block_nr(x);
		m_stack[bn] ^= (1ULL << block_pos(x));
		if (m_stack[bn - 1] == 0) {
			m_stack[bn - 1] = 0x8000000000000000ULL | m_top;
		}
		m_top = x;
	}
}

inline void sorted_int_stack::pop()
{
	if (!empty()) {
		--m_cnt; //< decrement counter
		if (m_top > m_n + 63) {
			m_overflow.pop_back();
			m_top = m_overflow.back();
			if (m_overflow.size() == 1) m_overflow.pop_back();
		} else {
			size_type bn = block_nr(m_top);
			uint64_t  w  = m_stack[bn];
			assert((w >> 63) == 0); // highest bit is not set, as the block contains no pointer
			w ^= (1ULL << block_pos(m_top));
			m_stack[bn] = w;
			if (w > 0) {
				m_top = bn * 63 + bits::hi(w);
			} else { // w==0 and cnt>0
				assert(bn > 0);
				w = m_stack[bn - 1];
				if ((w >> 63) == 0) { // highest bit is not set => the block contains no pointer
					assert(w > 0);
					m_top = (bn - 1) * 63 + bits::hi(w);
				} else { // block contains pointers
					m_stack[bn - 1] = 0;
					m_top			= w & 0x7FFFFFFFFFFFFFFFULL;
				}
			}
		}
	}
}

inline sorted_int_stack::size_type
sorted_int_stack::serialize(std::ostream& out, structure_tree_node* v, std::string name) const
{
	structure_tree_node* child = structure_tree::add_child(v, name, util::class_name(*this));
	size_type			 written_bytes = 0;
	written_bytes += write_member(m_n, out);
	written_bytes += write_member(m_top, out);
	written_bytes += write_member(m_cnt, out);
	written_bytes += m_stack.serialize(out);
	written_bytes += sdsl::serialize(m_overflow, out, child, "overflow");
	structure_tree::add_size(child, written_bytes);
	return written_bytes;
}

inline void sorted_int_stack::load(std::istream& in)
{
	read_member(m_n, in);
	read_member(m_top, in);
	read_member(m_cnt, in);
	m_stack.load(in);
	sdsl::load(m_overflow, in);
}

template <typename archive_t>
void sorted_int_stack::CEREAL_SAVE_FUNCTION_NAME(archive_t & ar) const
{
	ar(CEREAL_NVP(m_n));
	ar(CEREAL_NVP(m_cnt));
	ar(CEREAL_NVP(m_top));
	ar(CEREAL_NVP(m_stack));
	ar(CEREAL_NVP(m_overflow));
}

template <typename archive_t>
void sorted_int_stack::CEREAL_LOAD_FUNCTION_NAME(archive_t & ar)
{
	ar(CEREAL_NVP(m_n));
	ar(CEREAL_NVP(m_cnt));
	ar(CEREAL_NVP(m_top));
	ar(CEREAL_NVP(m_stack));
	ar(CEREAL_NVP(m_overflow));
}

//! Equality operator.
inline bool sorted_int_stack::operator==(sorted_int_stack const & other) const noexcept
{
	return (m_n == other.m_n) && (m_cnt == other.m_cnt) && (m_top == other.m_top) &&
	       (m_stack == other.m_stack) && (m_overflow == other.m_overflow);
}

//! Inequality operator.
inline bool sorted_int_stack::operator!=(sorted_int_stack const & other) const noexcept
{
	return !(*this == other);
}

} // end namespace sdsl

#endif // end file