C++自制Redis数据库(八)基本数据结构--String

来源:互联网 发布:html怎么调用php文件 编辑:程序博客网 时间:2024/05/05 06:42

说起数据结构,真是没办法,C/C++这两个C++的数据结构转换起来还是有点麻烦。我重新实现String 的本意是使用自己写的内存管理函数,但是我发现还是有很多不好解决的问题,比如线程安全,比如线程安全,比如线程安全,自己实现的特定的内存管理程序一定比通用的好用这是必须的肯定的一定的,但是new 可以调用构造函数,析构函数,而自己实现的内存是直接的内存分配,这对于我初学C++确实是一大难题,索性这个问题留给后边的库吧,我在这个项目完成后会去封一个自己的伸缩性强的内存管理库。

所以使用new delete 实现自己的String

[c]

#ifndef _STRING_REDIS_H
#define _STRING_REDIS_H
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cassert>

#define STRING_MAX                  (1024 * 1024)
#define FREE_STR(n)                   (LEN_STR(n) - n)
#define LEN_STR(n)                     (n * 2)

#define DEFAULT_SIZE_MAX    (1<<2)
#define DEFAULT_SIZE                (1<<7)

class String{
private:
char * sds;
int size;
int free;
public:
String(const char * s);
String();
String(const char *s,int len);
String(const String & st);
~String();

String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);

friend bool operator<(const String &st,const String &st2);
friend bool operator>(const String &st,const String &st2);
friend bool operator==(const String &st,const String &st2);
friend std::ostream & operator<<(std::ostream & os,const String &st2);
friend std::istream & operator>>(std::istream & is,String & st);

int getsize()const;
int getfree()const;
String & StringAdd(const String & st2);
String & StringAdd(const char *s);
String & StringClear(String & st);
String & StringSizeClear(String & st,int len);
String & StringUpDate(const char *s);
String & StringUpDate(const String & st);
};

#endif

String::String(const char *s){

size = std::strlen(s);
free = FREE_STR(size);
//sds = (char *)std::malloc(LEN_STR(size));
sds = new char[LEN_STR(size)];
std::strcpy(sds,s);

}

String::String(){
size = DEFAULT_SIZE;
free = FREE_STR(size);
//sds = (char *)std::malloc(LEN_STR(size));
sds = new char[LEN_STR(size)];
sds[0] = '\0';
}

String::String(const String & st){
size = st.size;
free = FREE_STR(st.size);
//sds = (char *)std::malloc(LEN_STR(size));
sds = new char[LEN_STR(size)];
std::strcpy(sds,st.sds);
}

String::~String(){

//std::cout << "~~~~" << std::endl;
//std::free(sds);
delete [] sds;

}

String & String::operator=(const char *s){
std::free(sds);
size = std::strlen(s);
//sds = (char *)malloc(LEN_STR(size));
sds = new char[LEN_STR(size)];
std::strcpy(sds,s);
free = FREE_STR(size);
return *this;
}
String & String::operator=(const String & st){
if(this == &st)
return *this;
std::free(sds);
size = LEN_STR(st.size);
//sds = (char *)std::malloc(LEN_STR(size));
sds = new char[LEN_STR(size)];
free = FREE_STR(size);
std::strcpy(sds,st.sds);
return *this;
}

char & String::operator[](int i){
return sds[i];
}

bool operator<(const String &st,const String &st2){
return(std::strcmp(st.sds,st2.sds) < 0);
}

bool operator>(const String &st,const String &st2){
return(std::strcmp(st.sds,st2.sds) > 0);
}

bool operator==(const String &st,const String &st2){
return(std::strcmp(st.sds,st.sds) == 0);
}

std::ostream & operator<<(std::ostream & os,const String & st){
os << st.sds;
return os;
}

std::istream & operator>>(std::istream & is,String & st){
char temp[DEFAULT_SIZE];
is.get(temp,DEFAULT_SIZE);
if(is)
st = temp;
while(is && is.get() != '\n')
continue;
return is;
}

int String::getsize()const{
return size;
}

int String::getfree()const{
return free;
}

String & String::StringAdd(const String & st2){

int len = st2.getsize();
if(len < free){
size += len;
free -= len;
std::strcat(sds,st2.sds);
}else if(len <= STRING_MAX && len >= free){
char temp[size + 1];
std::strcpy(temp,sds);
std::free(sds);
size = size + len;
free = FREE_STR(size);
//sds = (char *)std::malloc(LEN_STR((size)));
sds = new char[LEN_STR(size)];
assert(sds != NULL);
std::strcpy(sds,temp);
std::strcat(sds,st2.sds);
}else{
//do nothing
}

return *this;
}

String & String::StringAdd(const char *s){
int len = std::strlen(s);
if(len < free){
size += len;
free -= len;
std::strcat(sds,s);
}else if(len <= STRING_MAX && len >= free){
char temp[size + 1];
std::strcpy(temp,sds);
std::free(sds);
size = size + len;
free = FREE_STR(size);
//sds = (char *)std::malloc(LEN_STR(size));
sds = new char[LEN_STR(size)];
assert(sds != NULL);
std::strcpy(sds,temp);
std::strcat(sds,s);
}else{
//do nothing
}
return *this;
}

String & String::StringClear(String & st){

memset(sds,0,size+free);
free += size;
size = 0;

return *this;

}

String & String::StringSizeClear(String & st,int len){

memset(sds,0,len);
(len >= size) ? (free += size):(free += len);
(len >= size) ? (size = 0) : (size -= len);
return *this;
}
String & String::StringUpDate(const char *s){
int len = std::strlen(s);
if(len < free){
size += len;
free -= len;
std::strcat(sds,s);
}else if(len <= STRING_MAX && len >= free){
char temp[size + 1];
std::strcpy(temp,sds);
std::free(sds);
size = size + len;
free = FREE_STR(size);
//sds = (char *)std::malloc(LEN_STR(size));
sds = new char[LEN_STR(size)];
assert(sds != NULL);
std::strcpy(sds,temp);
std::strcat(sds,s);
}else{
//do nothing
}
return *this;
}

String & String::StringUpDate(const String & st){

int len = st.getsize();
if(len < free){
size += len;
free -= len;
std::strcat(sds,st.sds);
}else if(len <= STRING_MAX && len >= free){
char temp[size + 1];
std::strcpy(temp,sds);
std::free(sds);
size = size + len;
free = FREE_STR(size);
//sds = (char *)std::malloc(LEN_STR((size)));
sds = new char[LEN_STR(size)];
assert(sds != NULL);
std::strcpy(sds,temp);
std::strcat(sds,st.sds);
}else{
//do nothing
}

return *this;
}

&nbsp;

[/c]

查看原文:http://zmrlinux.com/2016/02/09/c%e8%87%aa%e5%88%b6redis%e6%95%b0%e6%8d%ae%e5%ba%93%ef%bc%88%e5%85%ab%ef%bc%89%e5%9f%ba%e6%9c%ac%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84-string/

0 0
原创粉丝点击