码迷,mamicode.com
首页 > 编程语言 > 详细

C++ HUSTR与两个版本的HUMAP实现

时间:2014-06-13 20:47:08      阅读:351      评论:0      收藏:0      [点我收藏+]

标签:style   class   tar   get   使用   string   

/*

hustr是一个我使用很方便的字符串类,管理使用很方便,

新的humap支持多级存储各种信息,使用及其方便

旧的humap只支持字符串存储,使用方法一样

*/

#include <stdio.h>

#include <stdlib.h>
#include <string>
#include <map>
#include <deque>
#include <string.h>
#include <stdarg.h>
using namespace std;


void errexit(const char * str)
{
printf("**************error exit*****************\r\n");
printf("%s\r\n", str);
printf("*****************************************\r\n");
exit(-1);
}
#define errexitf(str,...) errexit(hustr(str, ##__VA_ARGS__ ))


class hustr: public string
{
public:
hustr()
{


}


// hustr(const char * str)
// {
// assign(str);
// }
void Replace(const char * rst, const char * dst)
{
replace(find(rst), strlen(rst), dst, strlen(dst));
}


hustr str_key(const char * str = "=")
{
int len = length() + 1;
char * tmp = (char *) alloca(len);
memset(tmp, 0, len);
strcpy(tmp, c_str());
char * p = strstr(tmp, str);
*p = 0;
char * key = tmp;
char * value = p + 1;
return key;
}
hustr str_value(const char * str = "=")
{
int len = length() + 1;
char * tmp = (char *) alloca(len);
memset(tmp, 0, len);
strcpy(tmp, c_str());
char * p = strstr(tmp, str);
*p = 0;
char * key = tmp;
char * value = p + 1;
return value;
}


hustr(const char * fmt, ...)
{
char buffer[256];
if (fmt == NULL)
{
return;
}
va_list argptr;
int cnt;


va_start(argptr, fmt);
cnt = vsprintf(buffer, fmt, argptr);
va_end(argptr);


assign(buffer);


}


int format(const char * fmt, ...)
{
char buffer[256];
if (fmt == NULL)
{
errexitf("hustr format error\r\n");
}
va_list argptr;
int cnt;
va_start(argptr, fmt);
cnt = vsprintf(buffer, fmt, argptr);
va_end(argptr);


assign(buffer);


return (cnt);
}


const char * nstr()
{
if (empty())
{
return NULL;
}
else
return c_str();
}
operator const char *()
{
return c_str();
}


};


class HuMapArg
{
protected:
virtual const char * get_v() = 0;
virtual const char * get_k() = 0;
virtual void set_v(const char * str) = 0;
virtual void set_k(const char * str) = 0;
};
template<typename T>
class HuMap: public T, virtual public HuMapArg
{
HuMap & operator=(HuMap & mp)
{
}
// HUMap(HUMap & mp)
// {
// }
public:
//friend class T;
class HuMapBrother;
class HuMapSun;
HuMap(const char * key = NULL, HuMapSun * f = NULL, HuMapBrother * b = NULL) :
m_sun(key, f)
{
m_key = key;
m_father = f;


m_brother = b;
m_bro_id = BrotherCount();
}
void fetch(HuMap & mp)
{
m_brother = NULL;
m_sun = mp.m_sun;
m_key = mp.m_key;
m_val = mp.m_val;
setfather(NULL);
}


HuMap & operator=(const char * _value)
{
m_val = _value;
}
HuMap& operator[](const char * key)
{
return m_sun.GetSun(key);
}
HuMap& operator[](int num)
{
return m_brother->GetBro(num);
}
virtual const char * get_k()
{
return m_val.nstr();
}
virtual const char * get_v()
{
return m_val.nstr();
}
virtual void set_v(const char * str)
{
m_val = str;
}
virtual void set_k(const char * str)
{
m_key = str;
}
void display()
{
displayspace(retrospect_tree());
if (BrotherCount() > 1)
{
printf("[%s%d]=[%s]\r\n", m_key.c_str(), m_bro_id, m_val.c_str());
}
else
{
printf("[%s]=[%s]\r\n", m_key.c_str(), m_val.c_str());
}


m_sun.display();
}
void order_display()
{
displayspace(retrospect_tree());
if (BrotherCount() > 1)
{
printf("[%s%d]=[%s]\r\n", m_key.c_str(), m_bro_id, m_val.c_str());
}
else
{
printf("[%s]=[%s]\r\n", m_key.c_str(), m_val.c_str());
}


m_sun.order_display();
}


int BrotherCount()
{
if (m_brother != NULL)
{
return m_brother->BrotherCount();
}
else
{
return 0;
}
}


void remove(const char * key)
{
m_sun.m_mp.erase(key);
}
int exist(const char * key)
{
return m_sun.exist(key);
}
class HuMapSun
{


public:
hustr m_key;
HuMapSun * m_father;


typedef typename map<hustr, HuMapBrother>::iterator iterator;
typedef typename deque<HuMapBrother *>::iterator order_iterator;
HuMapSun(const char * key, HuMapSun * f)
{


m_key = key;
m_father = f;
//CreateNewBro();
}
HuMap & CreateMultiLast(const char * key)
{
return GetSun(key).CreateMultiLast();
}
HuMapBrother & CreateNewSun(const char * key)
{
m_mp.insert(
pair<hustr, HuMapBrother>(key, HuMapBrother(key, this)));
HuMapBrother & ret = GetSun(key);
m_order.push_back(&ret);
return ret;
}
HuMapBrother& GetSun(const char * key)
{
iterator it = find(key);
if (it == m_mp.end())
{ //new
CreateNewSun(key);
}
return m_mp.at(key);
}
iterator find(const char * key)
{
return m_mp.find(key);
}
iterator end()
{
return m_mp.end();
}
iterator begin()
{
return m_mp.begin();
}
int exist(const char * key)
{
iterator it = m_mp.find(key);
if (it == m_mp.end())
{
return 0;
}
else
{
return 1;
}
}
void display()
{
iterator it;
for (it = m_mp.begin(); it != m_mp.end(); ++it)
{
//printf("HUMap:: display%x\r\n", &(it->second));
it->second.display();
}
}
void order_display()
{
order_iterator it;
for (it = m_order.begin(); it != m_order.end(); ++it)
{
//printf("HUMap:: display%x\r\n", &(it->second));
(*it)->display();
}
}
void setfather(HuMapSun * father)
{
m_father = father;
iterator it;
for (it = m_mp.begin(); it != m_mp.end(); ++it)
{
it->second.setfather(this);
}


}
map<hustr, HuMapBrother> m_mp;
deque<HuMapBrother *> m_order;


};
class HuMapBrother
{
hustr m_key;
HuMapSun * m_father;
public:
typedef typename deque<HuMap>::iterator iterator;
HuMap & CreateNewBro()
{
m_mp.push_back(HuMap(m_key, m_father, this));
return m_mp.back();
}
int BrotherCount()
{
return m_mp.size();
}


HuMap& GetBro(int num)
{
if (num >= m_mp.size())
{ //addnew
CreateNewBro();
}
return m_mp[num];
}
HuMap & CreateMultiLast()
{
return CreateNewBro();
}
// HuMap& operator[](int num)
// {
// GetBro(num);
// }
// HuMapBrother& operator[](const char * key)
// {
// return GetBro(0).m_sun.GetSun(key);
// }
// HuMap & operator=(const char * _value)
// {
// GetBro(0).m_val = _value;
// }
operator HuMap&()
{
return GetBro(0);
}
HuMapBrother(const char * key, HuMapSun * f)
{


m_key = key;
m_father = f;
//CreateNewBro();
}
void display()
{
iterator it;
for (it = m_mp.begin(); it != m_mp.end(); ++it)
{
(*it).display();
}
}


void setfather(HuMapSun * father)
{
m_father = father;
iterator it;
for (it = m_mp.begin(); it != m_mp.end(); ++it)
{
(*it).setfather(father);
}
}
deque<HuMap> m_mp;
};


HuMapSun m_sun;
protected:
void setfather(HuMapSun * father)
{
m_father = father;
m_sun.setfather(father);
}
int retrospect_tree()
{
int i = 0;
HuMapSun * p = m_father;
while (p)
{
i++;
p = p->m_father;
}


return i;
}


void displayspace(int n, const char * str = "     ")
{
for (int i = 0; i < n; i++)
printf("%s", str);
}


hustr m_key;
hustr m_val;
HuMapBrother * m_brother;
HuMapSun * m_father;
int m_bro_id;
};


class tmpmp: virtual public HuMapArg
{
public:
int n;
const char * abc;
int b;


const char * getkey()
{
return get_k();
}
const char * getvalue()
{
return get_v();
}
};


class HuMapOld
{
HuMapOld & operator=(HuMapOld & mp)
{
}
// HUMap(HUMap & mp)
// {
// }
public:
class HuMapBrother;
typedef map<hustr, HuMapBrother>::iterator iterator;
typedef deque<HuMapBrother *>::iterator order_iterator;
HuMapOld(const char * key = NULL, HuMapOld * f = NULL, HuMapBrother * b =
NULL)
{
m_key = key;
m_father = f;
m_brother = b;
m_bro_id = BrotherCount();


}
void fetch(HuMapOld & mp)
{
m_brother = NULL;
m_mp = mp.m_mp;
m_key = mp.m_key;
m_val = mp.m_val;
setfather(NULL);
}
HuMapBrother & operator=(const char * _value)
{
m_val = _value;
}


HuMapBrother& GetSun(const char * key)
{
iterator it = find(key);
if (it == m_mp.end())
{ //new
CreateNewSun(key);
}
return m_mp.at(key);
}
HuMapOld & CreateMultiLast(const char * key)
{
return GetSun(key).CreateMultiLast();
}
HuMapBrother& operator[](const char * key)
{
return GetSun(key);
}
HuMapOld& operator[](int num)
{
return m_brother->GetBro(num);
}


iterator find(const char * key)
{
return m_mp.find(key);
}
iterator end()
{
return m_mp.end();
}
iterator begin()
{
return m_mp.begin();
}
void display()
{
displayspace(retrospect_tree());
if (BrotherCount() > 1)
{
printf("[%s%d]=[%s]\r\n", m_key.c_str(), m_bro_id, m_val.c_str());
}
else
{
printf("[%s]=[%s]\r\n", m_key.c_str(), m_val.c_str());
}


iterator it;
for (it = m_mp.begin(); it != m_mp.end(); ++it)
{
//printf("HUMap:: display%x\r\n", &(it->second));
it->second.display();
}
}
void order_display()
{
displayspace(retrospect_tree());
if (BrotherCount() > 1)
{
printf("[%s%d]=[%s]\r\n", m_key.c_str(), m_bro_id, m_val.c_str());
}
else
{
printf("[%s]=[%s]\r\n", m_key.c_str(), m_val.c_str());
}


order_iterator it;
for (it = m_order.begin(); it != m_order.end(); ++it)
{
//printf("HUMap:: display%x\r\n", &(it->second));
(*it)->display();
}
}
HuMapBrother & CreateNewSun(const char * key)
{
m_mp.insert(pair<hustr, HuMapBrother>(key, HuMapBrother(key, this)));
HuMapBrother & ret = GetSun(key);
m_order.push_back(&ret);
return ret;
}
int BrotherCount()
{
if (m_brother != NULL)
{
return m_brother->BrotherCount();
}
else
{
return 0;
}
}
const char * getkey()
{
return m_key.c_str();
}
const char * getvalue()
{
return m_val.c_str();


}
int getvalue_int()
{
return strtol(getvalue(), NULL, 10);
}
void remove(const char * key)
{
m_mp.erase(key);
}
int exist(const char * key)
{
iterator it = m_mp.find(key);
if (it == m_mp.end())
{
return 0;
}
else
{
return 1;
}
}
class HuMapBrother
{
hustr m_key;
HuMapOld * m_father;
public:


typedef deque<HuMapOld>::iterator iterator;
HuMapOld & CreateNewBro()
{
m_mp.push_back(HuMapOld(m_key, m_father, this));
return m_mp.back();
}
int BrotherCount()
{
return m_mp.size();
}
const char * getkey()
{
return GetBro(0).getkey();
}
const char * getvalue()
{
return GetBro(0).getvalue();
}
int getvalue_int()
{
return GetBro(0).getvalue_int();
}
HuMapOld& GetBro(int num)
{
if (num >= m_mp.size())
{ //addnew
CreateNewBro();
}
return m_mp[num];
}
HuMapOld & CreateMultiLast()
{
return CreateNewBro();
}
HuMapOld& operator[](int num)
{
GetBro(num);
}
HuMapBrother& operator[](const char * key)
{
return GetBro(0).GetSun(key);
}
HuMapOld & operator=(const char * _value)
{
GetBro(0).m_val = _value;
}
operator HuMapOld&()
{
return GetBro(0);
}
HuMapBrother(const char * key, HuMapOld * f)
{


m_key = key;
m_father = f;
//CreateNewBro();
}
void display()
{
iterator it;
for (it = m_mp.begin(); it != m_mp.end(); ++it)
{
(*it).display();
}
}


void setfather(HuMapOld * father)
{
m_father = father;
iterator it;
for (it = m_mp.begin(); it != m_mp.end(); ++it)
{
(*it).setfather(father);
}
}
deque<HuMapOld> m_mp;
};


private:
void setfather(HuMapOld * father)
{
m_father = father;
iterator it;
for (it = m_mp.begin(); it != m_mp.end(); ++it)
{
it->second.setfather(this);
}


}
int retrospect_tree()
{
int i = 0;
HuMapOld * p = this;
while (1)
{
if (p->m_father == NULL)
{
break;
}
i++;
p = p->m_father;
}
return i;
}


void displayspace(int n, const char * str = "     ")
{
for (int i = 0; i < n; i++)
printf("%s", str);
}


hustr m_key;
hustr m_val;
map<hustr, HuMapBrother> m_mp;
deque<HuMapBrother *> m_order;
HuMapBrother * m_brother;
HuMapOld * m_father;
int m_bro_id;
};


int main(int argc, char * argv[])
{
HuMap<tmpmp> mp("newmp");
//HuMapOld mp("oldmp");
mp["b1"] = "1";
mp["b2"] = "2";
mp["b3"] = "3";
mp["a1"][0] = "a1.0";
mp["a1"][1] = "a1.1";
mp["a1"][2] = "a1.2";
mp["c1"]["c1-1"] = "c1.1";
mp["c1"]["c1-2"] = "c1.2";
mp["c1"]["c1-3"] = "c1.3";
mp["c2"]["c2-1"]["c2-1-1"] = "c1.1";
mp["c2"]["c2-2"]["c2-1-2"] = "c1.1";
mp["c2"]["c2-3"]["c2-1-3"] = "c1.1";
mp["c3"]["c3-1"]["c3-1-1"]["c3-1-1-1"] = "c1.1";
mp["c3"]["c3-2"]["c3-1-2"]["c3-1-1-2"] = "c1.1";
mp["c3"]["c3-3"]["c3-1-3"]["c3-1-1-3"] = "c1.1";
mp["c1"]["c1-3"].n = 50;
mp["c1"]["c1-3"].b = 100;
mp["c1"]["c1-3"].abc = "abcdefg";
mp.display();


printf("******************\r\n");
printf("%d %s %d\r\n",mp["c1"]["c1-3"].n,mp["c1"]["c1-3"].abc,mp["c1"]["c1-3"].b);
return 0;
}

C++ HUSTR与两个版本的HUMAP实现,布布扣,bubuko.com

C++ HUSTR与两个版本的HUMAP实现

标签:style   class   tar   get   使用   string   

原文地址:http://blog.csdn.net/dragon101788/article/details/30229733

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!