File: int_vector_buffer.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 (495 lines) | stat: -rw-r--r-- 13,733 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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// 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 int_vector_buffer.hpp
    \brief int_vector_buffer.hpp contains the sdsl::int_vector_buffer class.
    \author Maike Zwerger, Timo Beller and Simon Gog
*/
#ifndef INCLUDED_INT_VECTOR_BUFFER
#define INCLUDED_INT_VECTOR_BUFFER

#include "int_vector.hpp"
#include "iterators.hpp"
#include <cassert>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string>

namespace sdsl {

template <uint8_t t_width = 0>
class int_vector_buffer {
public:
	class iterator;
	typedef typename int_vector<t_width>::difference_type difference_type;
	typedef typename int_vector<t_width>::value_type	  value_type;

private:
	static_assert(t_width <= 64, "int_vector_buffer: width must be at most 64 bits.");
	sdsl::isfstream		m_ifile;
	sdsl::osfstream		m_ofile;
	std::string			m_filename;
	int_vector<t_width> m_buffer;
	bool				m_need_to_write = false;
	// length of int_vector header in bytes: 0 for plain, 8 for int_vector<t_width> (0 < t_width), 9 for int_vector<0>
	uint64_t m_offset	 = 0;
	uint64_t m_buffersize = 8; // in elements! m_buffersize*width() must be a multiple of 8!
	uint64_t m_size		  = 0; // size of int_vector_buffer
	uint64_t m_begin	  = 0; // number in elements

	//! Read block containing element at index idx.
	void read_block(const uint64_t idx)
	{
		m_begin = (idx / m_buffersize) * m_buffersize;
		if (m_begin >= m_size) {
			util::set_to_value(m_buffer, 0);
		} else {
			m_ifile.seekg(m_offset + (m_begin * width()) / 8);
			assert(m_ifile.good());
			m_ifile.read((char*)m_buffer.data(), (m_buffersize * width()) / 8);
			if ((uint64_t)m_ifile.gcount() < (m_buffersize * width()) / 8) {
				m_ifile.clear();
			}
			assert(m_ifile.good());
			for (uint64_t i = m_size - m_begin; i < m_buffersize; ++i) {
				m_buffer[i] = 0;
			}
		}
	}

	//! Write current block to file.
	void write_block()
	{
		if (m_need_to_write) {
			m_ofile.seekp(m_offset + (m_begin * width()) / 8);
			assert(m_ofile.good());
			if (m_begin + m_buffersize >= m_size) {
				//last block in file
				uint64_t wb = ((m_size - m_begin) * width() + 7) / 8;
				m_ofile.write((char*)m_buffer.data(), wb);
			} else {
				m_ofile.write((char*)m_buffer.data(), (m_buffersize * width()) / 8);
			}
			m_ofile.flush();
			assert(m_ofile.good());
			m_need_to_write = false;
		}
	}

	//! Read value from idx.
	uint64_t read(const uint64_t idx)
	{
		assert(is_open());
		assert(idx < m_size);
		if (idx < m_begin or m_begin + m_buffersize <= idx) {
			write_block();
			read_block(idx);
		}
		return m_buffer[idx - m_begin];
	}

	//! Write value to idx.
	void write(const uint64_t idx, const uint64_t value)
	{
		assert(is_open());
		// If idx is not in current block, write current block and load needed block
		if (idx < m_begin or m_begin + m_buffersize <= idx) {
			write_block();
			read_block(idx);
		}
		if (m_size <= idx) {
			m_size = idx + 1;
		}
		m_need_to_write			= true;
		m_buffer[idx - m_begin] = value;
	}

public:
	//! Constructor.
	int_vector_buffer() { m_buffer = int_vector<t_width>(); }

	//! Constructor for int_vector_buffer.
	/*! \param filename   File that contains the data read from / written to.
         *  \param mode       Openmode:
         *                    std::ios::in opens an existing file (that must exist already),
         *                    std::ios::out creates a new file (that may exist already).
         *  \param buffersize Buffersize in bytes. This has to be a multiple of 8, if not the next multiple of 8 will be taken
         *  \param int_width  The width of each integer.
         *  \param is_plain   If false (default) the file will be interpreted as int_vector.
         *                    If true the file will be interpreted as plain array with t_width bits per integer.
         *                    In second case (is_plain==true), t_width must be 8, 16, 32 or 64.
         */
	int_vector_buffer(const std::string  filename,
					  std::ios::openmode mode		 = std::ios::in,
					  const uint64_t	 buffer_size = 1024 * 1024,
					  const uint8_t		 int_width   = t_width,
					  const bool		 is_plain	= false)
	{
		m_filename = filename;
		assert(!(mode & std::ios::app));
		mode &= ~std::ios::app;
		m_buffer.width(int_width);
		if (is_plain) {
			m_offset = 0;
			// is_plain is only allowed with width() in {8, 16, 32, 64}
			assert(8 == width() or 16 == width() or 32 == width() or 64 == width());
		} else {
			m_offset = 8; // TODO: make this dependent on header size of int_vector<t_width>
		}

		// Open file for IO
		m_ofile.open(m_filename, mode | std::ios::out | std::ios::binary);
		assert(m_ofile.good());
		m_ifile.open(m_filename, std::ios::in | std::ios::binary);
		assert(m_ifile.good());
		if (mode & std::ios::in) {
			uint64_t size = 0;
			if (is_plain) {
				m_ifile.seekg(0, std::ios_base::end);
				size = m_ifile.tellg() * 8;
			} else {
				uint8_t width = 0;
				int_vector<t_width>::read_header(size, width, m_ifile);
				m_buffer.width(width);
			}
			assert(m_ifile.good());
			m_size = size / width();
		}
		buffersize(buffer_size);
	}

	//! Move constructor.
	int_vector_buffer(int_vector_buffer&& ivb)
		: m_filename(std::move(ivb.m_filename))
		, m_buffer(std::move(ivb.m_buffer))
		, m_need_to_write(ivb.m_need_to_write)
		, m_offset(ivb.m_offset)
		, m_buffersize(ivb.m_buffersize)
		, m_size(ivb.m_size)
		, m_begin(ivb.m_begin)
	{
		ivb.m_ifile.close();
		ivb.m_ofile.close();
		m_ifile.open(m_filename, std::ios::in | std::ios::binary);
		m_ofile.open(m_filename, std::ios::in | std::ios::out | std::ios::binary);
		assert(m_ifile.good());
		assert(m_ofile.good());
		// set ivb to default-constructor state
		ivb.m_filename		= "";
		ivb.m_buffer		= int_vector<t_width>();
		ivb.m_need_to_write = false;
		ivb.m_offset		= 0;
		ivb.m_buffersize	= 8;
		ivb.m_size			= 0;
		ivb.m_begin			= 0;
	}

	//! Destructor.
	~int_vector_buffer() { close(); }

	//! Move assignment operator.
	int_vector_buffer<t_width>& operator=(int_vector_buffer&& ivb)
	{
		close();
		ivb.m_ifile.close();
		ivb.m_ofile.close();
		m_filename = ivb.m_filename;
		m_ifile.open(m_filename, std::ios::in | std::ios::binary);
		m_ofile.open(m_filename, std::ios::in | std::ios::out | std::ios::binary);
		assert(m_ifile.good());
		assert(m_ofile.good());
		// assign the values of ivb to this
		m_buffer		= (int_vector<t_width> &&)ivb.m_buffer;
		m_need_to_write = ivb.m_need_to_write;
		m_offset		= ivb.m_offset;
		m_buffersize	= ivb.m_buffersize;
		m_size			= ivb.m_size;
		m_begin			= ivb.m_begin;
		// set ivb to default-constructor state
		ivb.m_filename		= "";
		ivb.m_buffer		= int_vector<t_width>();
		ivb.m_need_to_write = false;
		ivb.m_offset		= 0;
		ivb.m_buffersize	= 8;
		ivb.m_size			= 0;
		ivb.m_begin			= 0;
		return *this;
	}

	//! Returns the width of the integers which are accessed via the [] operator.
	uint8_t width() const { return m_buffer.width(); }

	//! Returns the number of elements currently stored.
	uint64_t size() const { return m_size; }

	//! Returns the filename.
	std::string filename() const { return m_filename; }

	//! Returns the buffersize in bytes
	uint64_t buffersize() const
	{
		assert(m_buffersize * width() % 8 == 0);
		return (m_buffersize * width()) / 8;
	}

	//! Set the buffersize in bytes
	void buffersize(uint64_t buffersize)
	{
		if (0ULL == buffersize) buffersize = 8;
		write_block();
		if (0 == (buffersize * 8) % width()) {
			m_buffersize =
			buffersize * 8 /
			width(); // m_buffersize might not be multiple of 8, but m_buffersize*width() is.
		} else {
			uint64_t element_buffersize =
			(buffersize * 8) / width() +
			1; // one more element than fits into given buffersize in byte
			m_buffersize =
			element_buffersize + 7 - (element_buffersize + 7) % 8; // take next multiple of 8
		}
		m_buffer = int_vector<t_width>(m_buffersize, 0, width());
		if (0 != m_buffersize) read_block(0);
	}

	//! Returns whether state of underlying streams are good
	bool good() { return m_ifile.good() and m_ofile.good(); }

	//! Returns whether underlying streams are currently associated to a file
	bool is_open()
	{
		return m_ifile.is_open() and m_ofile.is_open();
		;
	}

	//! Delete all content and set size to 0
	void reset()
	{
		// reset file
		assert(m_ifile.good());
		assert(m_ofile.good());
		m_ifile.close();
		m_ofile.close();
		m_ofile.open(m_filename, std::ios::out | std::ios::binary);
		assert(m_ofile.good());
		m_ifile.open(m_filename, std::ios::in | std::ios::binary);
		assert(m_ifile.good());
		assert(m_ofile.good());
		// reset member variables
		m_need_to_write = false;
		m_size			= 0;
		// reset buffer
		read_block(0);
	}

	// Forward declaration
	class reference;

	//! [] operator
	/*! \param i Index the i-th integer of length width().
         *  \return A reference to the i-th integer of length width().
         */
	reference operator[](uint64_t idx) { return reference(this, idx); }

	//! Appends the given element value to the end of the int_vector_buffer
	void push_back(const uint64_t value) { write(m_size, value); }

	//! Close the int_vector_buffer.
	/*! It is not possible to read from / write into the int_vector_buffer after calling this method
         *  \param remove_file If true, the underlying file will be removed on closing.
         */
	void close(bool remove_file = false)
	{
		if (is_open()) {
			if (!remove_file) {
				write_block();
				if (0 < m_offset) { // in case of int_vector, write header and trailing zeros
					uint64_t size = m_size * width();
					m_ofile.seekp(0, std::ios::beg);
					int_vector<t_width>::write_header(size, width(), m_ofile);
					assert(m_ofile.good());
					uint64_t wb = (size + 7) / 8;
					if (wb % 8) {
						m_ofile.seekp(m_offset + wb);
						assert(m_ofile.good());
						m_ofile.write("\0\0\0\0\0\0\0\0", 8 - wb % 8);
						assert(m_ofile.good());
					}
				}
			}
			m_ifile.close();
			assert(m_ifile.good());
			m_ofile.close();
			assert(m_ofile.good());
			if (remove_file) {
				sdsl::remove(m_filename);
			}
		}
	}

	iterator begin() { return iterator(*this, 0); }

	iterator end() { return iterator(*this, size()); }

	class reference {
		friend class int_vector_buffer<t_width>;

	private:
		int_vector_buffer<t_width>* const m_int_vector_buffer = nullptr;
		uint64_t						  m_idx				  = 0;

		reference() {}

		reference(int_vector_buffer<t_width>* _int_vector_buffer, uint64_t _idx)
			: m_int_vector_buffer(_int_vector_buffer), m_idx(_idx)
		{
		}

	public:
		//! Conversion to int for read operations
		operator uint64_t() const { return m_int_vector_buffer->read(m_idx); }

		//! Assignment operator for write operations
		reference& operator=(const uint64_t& val)
		{
			m_int_vector_buffer->write(m_idx, val);
			return *this;
		}

		//! Assignment operator
		reference& operator=(reference& x) { return *this = (uint64_t)(x); };

		//! Prefix increment of the proxy object
		reference& operator++()
		{
			uint64_t x = m_int_vector_buffer->read(m_idx);
			m_int_vector_buffer->write(m_idx, x + 1);
			return *this;
		}

		//! Postfix increment of the proxy object
		uint64_t operator++(int)
		{
			uint64_t val = (uint64_t) * this;
			++(*this);
			return val;
		}

		//! Prefix decrement of the proxy object
		reference& operator--()
		{
			uint64_t x = m_int_vector_buffer->read(m_idx);
			m_int_vector_buffer->write(m_idx, x - 1);
			return *this;
		}

		//! Postfix decrement of the proxy object
		uint64_t operator--(int)
		{
			uint64_t val = (uint64_t) * this;
			--(*this);
			return val;
		}

		//! Add assign from the proxy object
		reference& operator+=(const uint64_t x)
		{
			uint64_t w = m_int_vector_buffer->read(m_idx);
			m_int_vector_buffer->write(m_idx, w + x);
			return *this;
		}

		//! Subtract assign from the proxy object
		reference& operator-=(const uint64_t x)
		{
			uint64_t w = m_int_vector_buffer->read(m_idx);
			m_int_vector_buffer->write(m_idx, w - x);
			return *this;
		}

		bool operator==(const reference& x) const { return (uint64_t) * this == (uint64_t)x; }

		bool operator<(const reference& x) const { return (uint64_t) * this < (uint64_t)x; }
	};

	class iterator : public std::iterator<std::random_access_iterator_tag,
										  value_type,
										  difference_type,
										  value_type*,
										  reference> {
	private:
		int_vector_buffer<t_width>& m_ivb;
		uint64_t					m_idx = 0;

	public:
		iterator() = delete;
		iterator(int_vector_buffer<t_width>& ivb, uint64_t idx = 0) : m_ivb(ivb), m_idx(idx) {}

		iterator& operator++()
		{
			++m_idx;
			return *this;
		}

		iterator operator++(int)
		{
			iterator it = *this;
			++(*this);
			return it;
		}

		iterator& operator--()
		{
			--m_idx;
			return *this;
		}

		iterator operator--(int)
		{
			iterator it = *this;
			--(*this);
			return it;
		}

		reference operator*() const { return m_ivb[m_idx]; }

		iterator& operator+=(difference_type i)
		{
			if (i < 0) return *this -= (-i);
			m_idx += i;
			return *this;
		}

		iterator& operator-=(difference_type i)
		{
			if (i < 0) return *this += (-i);
			m_idx -= i;
			return *this;
		}

		iterator operator+(difference_type i) const
		{
			iterator it = *this;
			return it += i;
		}

		iterator operator-(difference_type i) const
		{
			iterator it = *this;
			return it -= i;
		}

		bool operator==(const iterator& it) const
		{
			return &m_ivb == &(it.m_ivb) and m_idx == it.m_idx;
		}

		bool operator!=(const iterator& it) const { return !(*this == it); }
		inline difference_type operator-(const iterator& it) { return (m_idx - it.m_idx); }
	};
};

} // end of namespace

#endif // include guard