lab1 ok(simple ver)

This commit is contained in:
ridethepig 2023-02-15 13:23:26 +00:00
parent 6a787fc868
commit b7319c6148
2 changed files with 45 additions and 5 deletions

View File

@ -1,5 +1,6 @@
#include "stream_reassembler.hh"
#include <algorithm>
// Dummy implementation of a stream reassembler.
// For Lab 1, please replace with a real implementation that passes the
@ -8,19 +9,51 @@
// You will need to add private members to the class declaration in `stream_reassembler.hh`
template <typename... Targs>
void DUMMY_CODE(Targs &&... /* unused */) {}
void DUMMY_CODE(Targs &&.../* unused */) {}
using namespace std;
StreamReassembler::StreamReassembler(const size_t capacity) : _output(capacity), _capacity(capacity) {}
StreamReassembler::StreamReassembler(const size_t capacity)
: _output(capacity), _capacity(capacity), _aux_buffer(capacity, '\0'), _aux_buffer_valid(capacity, false) {}
// pre-allocate is working
//! \details This function accepts a substring (aka a segment) of bytes,
//! possibly out-of-order, from the logical stream, and assembles any newly
//! contiguous substrings and writes them into the output stream in order.
void StreamReassembler::push_substring(const string &data, const size_t index, const bool eof) {
DUMMY_CODE(data, index, eof);
_unread = _unassem - _output.buffer_size();
_aux_buffer.resize(_capacity - _output.buffer_size(), '\0');
_aux_buffer_valid.resize(_aux_buffer.size(), false);
if (eof) {
_eof = index + data.size();
}
auto start = std::max(index, _unassem);
auto end = std::min(index + data.size(), _unread + _capacity);
for (auto i = start; i < end; ++i) {
_aux_buffer[i - _unassem] = data[i - index];
if (!_aux_buffer_valid[i - _unassem]) {
_aux_buffer_valid[i - _unassem] = true;
_unassem_count++;
}
}
std::string wr_data;
while (!_aux_buffer_valid.empty() && _aux_buffer_valid.front()) {
wr_data.push_back(_aux_buffer.front());
_aux_buffer.pop_front();
_aux_buffer_valid.pop_front();
++_unassem;
--_unassem_count;
}
if (_unassem == _eof) {
_output.end_input();
}
if (wr_data.size()) {
_output.write(wr_data);
}
}
size_t StreamReassembler::unassembled_bytes() const { return {}; }
size_t StreamReassembler::unassembled_bytes() const { return _unassem_count; }
bool StreamReassembler::empty() const { return {}; }
bool StreamReassembler::empty() const { return (_unassem_count == 0); }

View File

@ -4,6 +4,7 @@
#include "byte_stream.hh"
#include <cstdint>
#include <deque>
#include <string>
//! \brief A class that assembles a series of excerpts from a byte stream (possibly out of order,
@ -13,7 +14,13 @@ class StreamReassembler {
// Your code here -- add private members as necessary.
ByteStream _output; //!< The reassembled in-order byte stream
size_t _capacity; //!< The maximum number of bytes
size_t _capacity{}; //!< The maximum number of bytes
size_t _unread{}; //!< The index (in the stream) of the first unread byte
size_t _unassem{}; //!< The index (in the stream) of the first unassembled byte
size_t _unassem_count{};
std::deque<char> _aux_buffer; //!< auxiliary storage, initially allocated _capacity bytes
std::deque<bool> _aux_buffer_valid; //!< indicate whether the bit in _aux_buffer is filled with a valid byte
size_t _eof{static_cast<size_t>(-1)};
public:
//! \brief Construct a `StreamReassembler` that will store up to `capacity` bytes.