cs144 lab1

该实验是要实现一个字节重组的类,类是把一些零散的字符串给他重组成一个完整的字符串并且写入lab0中的ByteStream类中。

看明白了这个图代码还是比较容易写出来的,此处给出stream_reassembler.cc的代码

#include "stream_reassembler.hh"
#include<iostream>
// Dummy implementation of a stream reassembler.

// For Lab 1, please replace with a real implementation that passes the
// automated checks run by `make check_lab1`.

// You will need to add private members to the class declaration in `stream_reassembler.hh`

template <typename... Targs>
void DUMMY_CODE(Targs &&... /* unused */) {}

using namespace std;

StreamReassembler::StreamReassembler(const size_t capacity) :
    _output(capacity),
    _deque_buf(capacity,'\0'),
    _deque_hash(capacity,false),
    _unreassembled_bytes_size(0),
    _capacity(capacity),
    _first_unreassembled(0),
    _is_eof(false)
{}

//! \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) {
    // the first unread pos
    size_t first_unread = _output.bytes_read();
    size_t deque_relative_pos = index;
    string handle_data = data;
    // get  data deque relative pos and handle  string
    if(index < _first_unreassembled) {
if(index + data.size() > _first_unreassembled) {
            deque_relative_pos = _first_unreassembled;
            // remove ready reassembled
            handle_data = handle_data.erase(0,_first_unreassembled - index);
        }else {
            return ;
        }
    }

    if(deque_relative_pos + handle_data.size() > first_unread + _capacity) {
        size_t slice_pos = first_unread + _capacity - deque_relative_pos;
        size_t slice_num = deque_relative_pos + handle_data.size() - first_unread - _capacity;
        handle_data = handle_data.erase(slice_pos,slice_num);
    }else{
        if(eof == true) {
            //eof is assembled into deque_buf
            _is_eof = true;
        }
    }

    for(size_t i = 0; i < handle_data.size();i++) {
        _deque_buf[deque_relative_pos - _first_unreassembled + i] = handle_data[i];
        _deque_hash[deque_relative_pos - _first_unreassembled + i] = true;
    }

    std::string assembled_str;

    // record assembled num
    size_t pos = 0;

    // reconstruction deque
    while(_deque_hash[0] != false){
         assembled_str += _deque_buf[0];
        _deque_buf.pop_front();
        _deque_hash.pop_front();
        _deque_buf.emplace_back('\0');
        _deque_hash.emplace_back(false);
        pos++;
    }

    if(assembled_str.size() != 0) {
        _output.write(assembled_str);
    }

    _unreassembled_bytes_size = 0;
    for(size_t i = 0; i < _capacity; i++) {
        if(_deque_hash[i] == true) {
            _unreassembled_bytes_size++;
        }
    }

    _first_unreassembled += pos;

    if(_is_eof && empty()) {
        _output.end_input();
        _first_unreassembled = first_unread + _capacity + 1;
    }


    return ;
}

size_t StreamReassembler::unassembled_bytes() const { return this->_unreassembled_bytes_size; }

bool StreamReassembler::empty() const { return StreamReassembler::unassembled_bytes() == 0; }


posted @ 2022-04-19 15:19  warrens  阅读(171)  评论(0编辑  收藏  举报