//! \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. // voidStreamReassembler::push_substring(const string &data, constsize_t index, constbool eof){ DUMMY_CODE(data, index, eof); //始终使用绝对索引 if(index>_output.bytes_read()+_capacity){ return; }elseif(index+data.length()>_capacity+_output.bytes_read()){ for(size_t i=_lens_un+_output.bytes_written();i<_capacity+_output.bytes_read();i++){ if(i<index){ _unassembled_byte.push_back('\0'); _check_byte.push_back(false);
//! \brief A class that assembles a series of excerpts from a byte stream (possibly out of order, //! possibly overlapping) into an in-order byte stream. classStreamReassembler { private: // Your code here -- add private members as necessary. ByteStream _output; //!< The reassembled in-order byte stream //capacity是SreamReassembler里面储存的max_index减去在bytestream里面还没有被读取min_index,然后还要加上1 size_t _capacity; //!< The maximum number of bytes std::deque<char> _unassembled_byte;// std::deque<bool> _check_byte; size_t _lens_un; bool _eof; public: //! \brief Construct a `StreamReassembler` that will store up to `capacity` bytes. //! \note This capacity limits both the bytes that have been reassembled, //! and those that have not yet been reassembled. StreamReassembler(constsize_t capacity);
//! \brief Receive a substring and write any newly contiguous bytes into the stream. //! //! The StreamReassembler will stay within the memory limits of the `capacity`. //! Bytes that would exceed the capacity are silently discarded. //! //! \param data the substring //! \param index indicates the index (place in sequence) of the first byte in `data` //! \param eof the last byte of `data` will be the last byte in the entire stream voidpush_substring(const std::string &data, constuint64_t index, constbool eof);
//! The number of bytes in the substrings stored but not yet reassembled //! //! \note If the byte at a particular index has been pushed more than once, it //! should only be counted once for the purpose of this function. /*unassembled byte是还未传递但是要传递给bytestream的储存在StreamReassembler的字符数, * 其中如果不同的字符串有重叠部分的话,重叠部分只算一次 * */ size_tunassembled_bytes()const;
//! \brief Is the internal state empty (other than the output stream)? //! \returns `true` if no substrings are waiting to be assembled //判空函数, boolempty()const; };