20 #ifndef TPIE_COMPRESSED_STREAM_H
21 #define TPIE_COMPRESSED_STREAM_H
27 #include <tpie/tpie_export.h>
34 #include <tpie/stream_writable.h>
73 class compressed_stream_base_p;
81 friend class compressed_stream_base_p;
109 void cache_read_writes();
111 void peak_unlikely();
113 void read_back_unlikely();
115 void write_unlikely(
const char * item);
117 static memory_size_type memory_usage(
double blockFactor=1.0) noexcept;
120 bool is_readable() const noexcept;
122 bool is_writable() const noexcept;
124 static memory_size_type block_size(
double blockFactor) noexcept;
126 static
double calculate_block_factor(memory_size_type blockSize) noexcept;
128 static memory_size_type block_memory_usage(
double blockFactor) noexcept;
130 memory_size_type block_items() const;
132 memory_size_type block_size() const;
134 template <typename TT>
135 void read_user_data(TT & data) {
136 if (
sizeof(TT) != user_data_size())
138 read_user_data(
reinterpret_cast<void *
>(&data),
sizeof(TT));
141 memory_size_type read_user_data(
void * data, memory_size_type count);
143 template <
typename TT>
144 void write_user_data(
const TT & data) {
145 if (
sizeof(TT) > max_user_data_size())
147 write_user_data(
reinterpret_cast<const void *
>(&data),
sizeof(TT));
150 void write_user_data(
const void * data, memory_size_type count);
152 memory_size_type user_data_size()
const;
154 memory_size_type max_user_data_size()
const;
156 const std::string & path()
const;
168 void open(
const std::string & path,
170 memory_size_type userDataSize = 0,
177 void open(memory_size_type userDataSize,
184 void open(temp_file & file,
186 memory_size_type userDataSize = 0,
245 void open(
const std::string & path,
open::type openFlags=open::defaults, memory_size_type userDataSize=0);
253 void open(
open::type openFlags=open::defaults, memory_size_type userDataSize=0);
262 void open(temp_file & file,
open::type openFlags=open::defaults, memory_size_type userDataSize=0);
266 bool is_open() const noexcept;
268 stream_size_type offset() const;
273 void describe(std::ostream & out);
278 std::
string describe();
285 void seek(stream_offset_type offset, offset_type whence=beginning);
293 void truncate(stream_size_type offset);
298 void truncate(const stream_position & pos);
312 stream_position get_position();
318 void set_position(const stream_position & pos);
320 stream_size_type size()
const {
return m_size; }
322 stream_size_type file_size()
const {
return size(); }
329 if (m_cachedReads > 0)
return true;
330 return offset() < size();
375 compressed_stream_base_p * m_p;
401 template <
typename T>
410 static memory_size_type memory_usage(
double blockFactor=1.0) noexcept {
412 return sizeof(file_stream) + compressed_stream_base::memory_usage(blockFactor);
426 if (m_cachedReads == 0) {
428 const T & res = *
reinterpret_cast<const T*
>(m_nextItem);
430 m_nextItem +=
sizeof(T);
436 const T & res = *
reinterpret_cast<const T*
>(m_nextItem);
437 m_nextItem +=
sizeof(T);
452 if (m_cachedReads == 0) peak_unlikely();
453 return *
reinterpret_cast<const T*
>(m_nextItem);
470 template <
typename IT>
471 void read(IT
const a, IT
const b) {
472 for (IT i = a; i != b; ++i) *i =
read();
475 const T & read_back() {
476 if (m_seekState != seek_state::none || m_nextItem == m_bufferBegin) read_back_unlikely();
479 m_nextItem -=
sizeof(T);
480 return *
reinterpret_cast<const T *
>(m_nextItem);
483 void write(
const T & item) {
484 if (m_cachedWrites == 0) {
485 write_unlikely(
reinterpret_cast<const char*
>(&item));
488 memcpy(m_nextItem, &item,
sizeof(T));
489 m_nextItem +=
sizeof(T);
496 template <
typename IT>
497 void write(IT
const a, IT
const b) {
498 for (IT i = a; i != b; ++i) write(*i);
504 #endif // TPIE_COMPRESSED_STREAM_H