ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
TwoLevelMemory.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File TwoLevelMemory.h --------------------------*/
3/*--------------------------------------------------------------------------*/
60/*--------------------------------------------------------------------------*/
61/*----------------------------- DEFINITIONS --------------------------------*/
62/*--------------------------------------------------------------------------*/
63
64#ifndef __ADS_TwoLevelMemory
65 #define __ADS_TwoLevelMemory
66/* self-identification: #endif constant */
67
68/*--------------------------------------------------------------------------*/
69/*------------------------------ INCLUDES ----------------------------------*/
70/*--------------------------------------------------------------------------*/
71
72#include "ADS/Contract.h"
73
74#include <algorithm>
75#include <cstddef>
76#include <cstdint>
77#include <utility>
78#include <vector>
79
80/*--------------------------------------------------------------------------*/
81/*--------------------------- NAMESPACE ------------------------------------*/
82/*--------------------------------------------------------------------------*/
83
84namespace ADS
85{
86
87/*--------------------------------------------------------------------------*/
88/*------------------------- CLASS TwoLevelMemory ---------------------------*/
89/*--------------------------------------------------------------------------*/
90
94{
95
96/*--------------------------------------------------------------------------*/
97 public:
100 TwoLevelMemory( std::size_t B , std::size_t M );
101
103 TwoLevelMemory( const TwoLevelMemory & ) = delete;
106
108 [[nodiscard]] std::size_t blockSize( void ) const;
109
111 [[nodiscard]] std::size_t memorySize( void ) const;
112
114 [[nodiscard]] std::size_t blocks( std::size_t n ) const;
115
117 [[nodiscard]] std::uint64_t reads( void ) const;
118
120 [[nodiscard]] std::uint64_t writes( void ) const;
121
123 [[nodiscard]] std::uint64_t ios( void ) const;
124
126 void resetCounters( void );
127
130 void allocate( std::size_t k );
131
134 void release( std::size_t k );
135
137 [[nodiscard]] std::size_t used( void ) const;
138
140 [[nodiscard]] std::size_t peak( void ) const;
141
143 void countRead( void );
144
146 void countWrite( void );
147
148/*--------------------------------------------------------------------------*/
149 private:
150 std::size_t f_B;
151 std::size_t f_M;
152 std::uint64_t f_reads = 0;
153 std::uint64_t f_writes = 0;
154 std::size_t f_used = 0;
155 std::size_t f_peak = 0;
156
157 }; // end( class TwoLevelMemory )
158
159/*--------------------------------------------------------------------------*/
160/*--------------------------- CLASS MemoryLease ----------------------------*/
161/*--------------------------------------------------------------------------*/
162
166{
167
168/*--------------------------------------------------------------------------*/
169 public:
172 MemoryLease( TwoLevelMemory & mem , std::size_t k );
173
175 MemoryLease( const MemoryLease & ) = delete;
177 MemoryLease & operator=( const MemoryLease & ) = delete;
178
181
183 [[nodiscard]] std::size_t size( void ) const;
184
185/*--------------------------------------------------------------------------*/
186 private:
187 TwoLevelMemory * f_mem;
188 std::size_t f_k;
189
190 }; // end( class MemoryLease )
191
192/*--------------------------------------------------------------------------*/
193/*---------------------------- CLASS DiskFile ------------------------------*/
194/*--------------------------------------------------------------------------*/
195
197template< class T >
199{
200
201/*--------------------------------------------------------------------------*/
202 public:
203 using value_type = T;
204 using size_type = std::size_t;
205
207 explicit DiskFile( TwoLevelMemory & mem );
208
210 DiskFile( TwoLevelMemory & mem , std::vector< T > items );
211
213 DiskFile( DiskFile && ) noexcept = default;
214
217 DiskFile & operator=( DiskFile && other );
218
220 DiskFile( const DiskFile & ) = delete;
222 DiskFile & operator=( const DiskFile & ) = delete;
223
225 [[nodiscard]] TwoLevelMemory & memory( void ) const;
226
228 [[nodiscard]] std::size_t size( void ) const;
229
231 [[nodiscard]] bool empty( void ) const;
232
234 [[nodiscard]] std::size_t blocks( void ) const;
235
239 std::size_t readBlock( std::size_t b , T * dest ) const;
240
243 void appendBlock( const T * src , std::size_t n );
244
246 void clear( void );
247
249 [[nodiscard]] const std::vector< T > & contents( void ) const;
250
251/*--------------------------------------------------------------------------*/
252 private:
253 TwoLevelMemory * f_mem;
254 std::vector< T > f_items;
255
256 }; // end( class DiskFile )
257
258/*--------------------------------------------------------------------------*/
259/*--------------------------- CLASS DiskReader -----------------------------*/
260/*--------------------------------------------------------------------------*/
261
264template< class T >
266{
267
268/*--------------------------------------------------------------------------*/
269 public:
272 explicit DiskReader( const DiskFile< T > & f );
273
275 DiskReader( const DiskReader & ) = delete;
277 DiskReader & operator=( const DiskReader & ) = delete;
278
280 [[nodiscard]] bool hasNext( void ) const;
281
284 const T & peek( void );
285
288 T next( void );
289
291 [[nodiscard]] std::size_t position( void ) const;
292
310 const T & at( std::size_t i );
311
312/*--------------------------------------------------------------------------*/
313 private:
315 void load( std::size_t b );
316
317 const DiskFile< T > * f_file;
318 MemoryLease f_lease;
319 std::vector< T > f_buf;
320 std::size_t f_block;
321 std::size_t f_pos = 0;
322
323 }; // end( class DiskReader )
324
325/*--------------------------------------------------------------------------*/
326/*--------------------------- CLASS DiskWriter -----------------------------*/
327/*--------------------------------------------------------------------------*/
328
332template< class T >
334{
335
336/*--------------------------------------------------------------------------*/
337 public:
341 explicit DiskWriter( DiskFile< T > & f );
342
344 DiskWriter( const DiskWriter & ) = delete;
346 DiskWriter & operator=( const DiskWriter & ) = delete;
347
350
353 void push( const T & x );
354
356 void close( void );
357
358/*--------------------------------------------------------------------------*/
359 private:
360 DiskFile< T > * f_file;
361 MemoryLease f_lease;
362 std::vector< T > f_buf;
363 bool f_closed = false;
364
365 }; // end( class DiskWriter )
366
367/*--------------------------------------------------------------------------*/
368
369} // end( namespace ADS )
370
371/*--------------------------------------------------------------------------*/
372/*-------------------------- IMPLEMENTATION --------------------------------*/
373/*--------------------------------------------------------------------------*/
374
375#include "ADS/external/impl/TwoLevelMemory.tpp"
376
377/*--------------------------------------------------------------------------*/
378
379#endif /* TwoLevelMemory.h included */
380
381/*--------------------------------------------------------------------------*/
382/*----------------------- End File TwoLevelMemory.h ------------------------*/
383/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
a file of items on the disk of the model, in blocks of items
Definition TwoLevelMemory.h:199
TwoLevelMemory & memory(void) const
the memory of the model that the file is on
void appendBlock(const T *src, std::size_t n)
one write: the items of src appended as a new block
DiskFile(TwoLevelMemory &mem)
an empty file
DiskFile(DiskFile &&) noexcept=default
the file moved, no I/O
const std::vector< T > & contents(void) const
the items, no I/O: for the checks, not for the algorithms
std::size_t size(void) const
the number of items of the file
std::size_t size_type
the type of the sizes
Definition TwoLevelMemory.h:204
std::size_t readBlock(std::size_t b, T *dest) const
one read: the block copied to dest, which has room for items; the number of its items
T value_type
the type of the items
Definition TwoLevelMemory.h:203
bool empty(void) const
whether the file has no item
void clear(void)
empties the file, no I/O
DiskFile(TwoLevelMemory &mem, std::vector< T > items)
the file of the items, laid out with no I/O: the input of a problem
std::size_t blocks(void) const
the blocks of the file,
const T & at(std::size_t i)
the item , its block read if not in the buffer; the scan goes on from
DiskReader(const DiskReader &)=delete
no copy, since the buffer is leased from the memory
const T & peek(void)
the next item of the scan, its block read if not in the buffer
DiskReader & operator=(const DiskReader &)=delete
no copy, since the buffer is leased from the memory
DiskReader(const DiskFile< T > &f)
the reader of f, its buffer taken from the memory of f
T next(void)
the next item of the scan, and the scan moves past it
bool hasNext(void) const
whether some items are not yet scanned
std::size_t position(void) const
the place of the scan
void close(void)
writes the partial block, if any; no push() after it
DiskWriter & operator=(const DiskWriter &)=delete
no copy, since the buffer is leased from the memory
~DiskWriter()
close(), if not closed yet
DiskWriter(const DiskWriter &)=delete
no copy, since the buffer is leased from the memory
DiskWriter(DiskFile< T > &f)
the writer at the end of f, its buffer taken from the memory of f
void push(const T &x)
appends , writing the buffer when it is full
items of the internal memory, taken on construction and given back on destruction
Definition TwoLevelMemory.h:166
~MemoryLease()
gives the items back
MemoryLease & operator=(const MemoryLease &)=delete
no copy, since the items would be given back twice
MemoryLease(TwoLevelMemory &mem, std::size_t k)
the lease of items of mem
std::size_t size(void) const
, the items taken
MemoryLease(const MemoryLease &)=delete
no copy, since the items would be given back twice
the parameters and of the model, the counters of the I/Os and the occupation of the internal memory
Definition TwoLevelMemory.h:94
TwoLevelMemory(const TwoLevelMemory &)=delete
no copy, since the counters belong to one simulation
std::size_t used(void) const
the items of the internal memory in use
std::size_t memorySize(void) const
, the items of the internal memory
void resetCounters(void)
sets the counters of the I/Os to 0, and the peak to the memory in use
void countRead(void)
one more read: called by DiskFile only
std::size_t peak(void) const
the largest used() since the construction or resetCounters()
TwoLevelMemory(std::size_t B, std::size_t M)
a memory of items and blocks of items
std::size_t blocks(std::size_t n) const
, the blocks of items
TwoLevelMemory & operator=(const TwoLevelMemory &)=delete
no copy, since the counters belong to one simulation
void allocate(std::size_t k)
takes items of the internal memory
std::uint64_t writes(void) const
the writes counted so far
std::uint64_t reads(void) const
the reads counted so far
void release(std::size_t k)
gives back items of the internal memory
std::uint64_t ios(void) const
the I/Os counted so far, reads() plus writes()
void countWrite(void)
one more write: called by DiskFile only
std::size_t blockSize(void) const
, the items of a block
void load(Ar &a, T &x)
the structure read from the archive of cereal
Definition Cereal.h:238
Definition ADT.h:96