首页 > 其他分享 >【LevelDB】【include】Slice类解析

【LevelDB】【include】Slice类解析

时间:2023-12-16 21:12:38浏览次数:27  
标签:Slice const string char LevelDB include data size

Slice类

  Slice类是对字符串的封装,设计思想与std::string_view相似。

  源文件位置

    include/leveldb/slice.h

  优点:

    1、拷贝速度快,Slice的拷贝仅需拷贝数据指针和数据长度

    2、多个Slice可指向同个字符串,减少资源开销

    3、支持std::string、char[]、Slice间的快速转换

  注意:

    1、空Slice的data_并非为空指针,而是为空字符串,因此判断Slice是否为空应根据empty()接口判断

    2、使用Slice期间需要保证Slice底层引用的字符串未被销毁

公共接口

  // 返回数据指针
  // Return a pointer to the beginning of the referenced data
  const char* data() const { return data_; }

  // 返回数据长度
  // Return the length (in bytes) of the referenced data
  size_t size() const;

  // 判断Slice是否为空
  // Return true iff the length of the referenced data is zero
  bool empty() const;

  // 清空Slice
  // Change this slice to refer to an empty array
  void clear();

  // 丢弃指定长度的前缀(不对底层字符串做改动,仅修改Slice自身的指针和长度)
  // Drop the first "n" bytes from this slice.
  void remove_prefix(size_t n);

  // 通过Slice构造std::string对象
  // Return a string that contains the copy of the referenced data.
  std::string ToString() const;

  // 比较Slice,内部调用memcmp
  // Three-way comparison.  Returns value:
  //   <  0 iff "*this" <  "b",
  //   == 0 iff "*this" == "b",
  //   >  0 iff "*this" >  "b"
  int compare(const Slice& b) const;

  // 判断此Slice是否以x为前缀
  // Return true iff "x" is a prefix of "*this"
  bool starts_with(const Slice& x);

私有成员变量

  const char* data_; // 数据指针
  size_t size_; // 数据长度

构造函数/运算符重载

  // Create an empty slice.
  Slice() : data_(""), size_(0) {}

  // 用以支持通过底层为char[]的字符串构造Slice
  // Create a slice that refers to d[0,n-1].
  Slice(const char* d, size_t n) : data_(d), size_(n) {}

  // 用以支持通过底层为std::string的字符串构造Slice
  // Create a slice that refers to the contents of "s"
  Slice(const std::string& s);

  // 等同于Slice(const char* d, size_t n),将自动通过strlen获取字符串长度
  // Create a slice that refers to s[0,strlen(s)-1]
  Slice(const char* s);

  // 浅拷贝,仅拷贝指针和长度,引用同个底层char[]/std::string
  // Intentionally copyable.
  Slice(const Slice&) = default;
  Slice& operator=(const Slice&) = default;
 
  // 用以使Slice支持[]索引
  // Return the ith byte in the referenced data.
  // REQUIRES: n < size()
  char operator[](size_t n) const;
 
  // 用以使Slice支持==运算符
  inline bool operator==(const Slice& x, const Slice& y);
 
  // 用以使Slice支持!=运算符
  inline bool operator!=(const Slice& x, const Slice& y);

标签:Slice,const,string,char,LevelDB,include,data,size
From: https://www.cnblogs.com/pond-flower/p/17908375.html

相关文章

  • 【LevelDB】【utils】Arena类解析
    Arena类Arena类是极为简易的内存池实现,支持动态申请内存空间(内存对齐/不对齐方式),通过RAII机制保证Arena对象管理的内存在Arena对象生命周期结束后自动清理。源文件位置util/arena.hutil/arena.cc优点:访问速度快缺点:存在内存浪费,详细见下......
  • Time slice based task routine in C
          基于时间片的轮询任务调度实例。#include<stdint.h>#include<stdio.h>#include<WinSock2.h>#defineMAX_TASK_NUM10#defineMAX_HALF_WORD0xffff#definefalse0#definetrue1staticuint16_tg_ti......
  • C语言进阶教程(include只能包含.h文件吗?)
    (文章目录)前言include在多文件编程中是非常重要的,我们经常使用他来包含一些头文件,方便我们管理代码和项目,那么include是只能包含头文件吗?这篇文章将会告诉大家include是不是只能包含头文件。一、include工作原理在C语言中,#include是预处理指令,它告诉编译器在源代码中包含另......
  • Web_XCTF_WriteUp | Web_php_include
    题目分析对php代码进行分析:<?php//php开头show_source(__FILE__);//对当前源文件进行高亮显示echo$_GET['hello'];//收集get表单中hello的数据输出$page=$_GET['page'];......
  • Linux Mint(Ubuntu)系统VS Code C/C++环境配置include error问题
    1.问题描述安装完成LinuxMint后发现随系统自带了gcc,心里比较开心,以为自己不需要装了。但是在安装完VSCode之后,一直提示#includeerrorsdetected.PleaseupdateyourincludePath.Squigglesaredisabledforthistranslationunitlinux2.解决方案重新通过apt安装gcc......
  • 基于kore 的共享模块以及include 配置实现动态的web api 能力
    kore支持include以及动态load共享模块的能力,可以让我们实现动态扩展的api加载能力参考玩法参考示例项目结构├──conf│├──hello.conf│├──index.conf│├──mydemo.conf│└──myhello.conf├──hello.so└─......
  • XCTF-file_include
    根据题目描述应该是一个文件包含的题目,访问主页面需要传入filename的参数,尝试使用php://filter协议读取check.php文件的内容?filename=php://filter/read/=convert.base64-encode/resource=check.php经过一些测试,得到过滤了php://filter/read和base64,这里无法使用php://filt......
  • 【mybatis <sql>,<include>标签】
    @[TOC]<sql>标签<sql>标签用于定义可重用的SQL片段,可以在多个地方引用。避免重复编写相同的SQL片段。示例:假设有一个SQL语句用于查询用户表中特定条件下的数据:<sqlid="userColumns">id,username,email</sql>在另一个地方,可以引用这个SQL片段:<selectid="selectU......
  • go数据类型-slice底层
    切片的底层数据结构有上篇string为基础了,能猜到slice肯定也有一个对应的struct。在runtime的slice.go中typeslicestruct{ arrayunsafe.Pointer lenint capint}切片的本质是对数组的引用len表示当前已经存储的个数,cap表示容量。切片的创建......
  • MyBatis `<include refid="XXX">`标签详解
    MyBatis<includerefid="XXX">标签详解MyBatis作为一种优秀的持久化框架,提供了丰富的XML配置选项,其中<include>标签是一个非常有用的特性,用于引入SQL片段,提高代码的可维护性和可读性。解释<include>标签用于引用SQL代码片段。refid是引用的SQL片段的id名称,必须保持唯一。......