首页 > 其他分享 >lab0

lab0

时间:2023-03-31 11:24:04浏览次数:35  
标签:const buffer length lab0 return ByteStream size

字节流设计

class ByteStream {
  private:
    std::deque<char> _buffer{};
    size_t _capacity;
    size_t _bytes_written{0};
    size_t _bytes_read{0};
    bool _end_input{false};

    bool _error{}; 

  public:
    ...
};

函数实现

size_t ByteStream::write(const string &data) {
    size_t length = min(remaining_capacity(), data.size());

    for (size_t i = 0; i < length; i++)
        _buffer.push_back(data[i]);

    _bytes_written += length;

    return length;
}

string ByteStream::peek_output(const size_t len) const {
    string res;
    size_t length = min(len, _buffer.size());
    for (size_t i = 0; i < length; i++)
        res += _buffer[i];

    return res;
}

void ByteStream::pop_output(const size_t len) { 
    size_t length = min(len, _buffer.size());
    for (size_t i = 0; i < length; i++) {
        _buffer.pop_front();
    }
    _bytes_read += length;
}

std::string ByteStream::read(const size_t len) {
    string res = peek_output(len);
    pop_output(len);
    return res;
}

void ByteStream::end_input() { _end_input = true; }

bool ByteStream::input_ended() const { return _end_input; }

size_t ByteStream::buffer_size() const { return _buffer.size(); }

bool ByteStream::buffer_empty() const { return _buffer.size() == 0; }

bool ByteStream::eof() const { return buffer_empty() && input_ended(); }

size_t ByteStream::bytes_written() const { return _bytes_written; }

size_t ByteStream::bytes_read() const { return _bytes_read; }

size_t ByteStream::remaining_capacity() const { return _capacity - _buffer.size(); }

标签:const,buffer,length,lab0,return,ByteStream,size
From: https://www.cnblogs.com/cong0221/p/17275710.html

相关文章

  • CS144 LAB0~LAB4
    CS144:LAB00.写在前面这更倾向于个人完成lab后的思考和总结,而不是CS144lab答案或者labdocument翻译(指南或者翻译已经有大佬做的很好了,下面已经贴出链接)出于......
  • CS61A_lab02
    1defcycle(f1,f2,f3):2"""Returnsafunctionthatisitselfahigher-orderfunction.34>>>defadd1(x):5...returnx+16......
  • 【计算机网络】Stanford CS144 Lab0 : networking warmup 学习记录
    CS144官方镜像:https://cs144.github.io/kangyupl备份的镜像:https://kangyupl.gitee.io/cs144.github.io/实验准备Ubuntu18.04.6LTSx86_64(实验提供)gcc8......
  • CS144-Lab0-networking warmup
    lab地址:lab0-doc代码实现:lab0-code1.目标利用官方支持的TCPSocket,实现一个wget功能,其次,实现一个可靠的字节流。2.实现2.1webget实现上比较简单,主要就是:......
  • 2022_CMU15445_lab0笔记(Trie)
    预备工作环境我在windowswsl2中使用docker,docker是编译环境,wsl是编码环境,用共享目录的形式将docker目录和wsl2关联,用vscode编码剩下的环境配置直接参考https://gi......
  • 【CS144】Spongeの类图分析(lab0~lab4)
    lab4写不下去了,感觉对代码理不清了,打算重新整理一下。重点是五个类,(正好对应lab0~lab4)ByteStream类这里只展示startcode中类的模样(具体的实现可能各有不同,所以重点看函......
  • CS144-Lab0
    Lab0主要是热身。task1和task2比较简单,按他指示来就行,略。task3写一个WebGet,代码如下:voidget_URL(conststring&host,conststring&path){//Yourcodehere......