add class var&add function pop|subvec|setsize

This commit is contained in:
Valk Richard Li 2019-07-27 14:39:56 +08:00 committed by GitHub
parent c582269683
commit 141f4e9d26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 614 additions and 119 deletions

39
lab.cpp
View File

@ -72,6 +72,45 @@ int main()
qthash.Append("int",&a,"int"); qthash.Append("int",&a,"int");
qt.Append(&qthash,"hash"); qt.Append(&qthash,"hash");
qt.PrintList(); qt.PrintList();
std::cout<<std::endl;
NasalList _l;
_l.Append(&a,"int");
_l.SetSize(10);
_l.PrintList();
std::cout<<std::endl;
_l.SetSize(3);
_l.PrintList();
std::cout<<std::endl;
_l.SetSize(0);
_l.PrintList();
std::cout<<std::endl;
_l.Append(&a,"int");
_l.Append(&a,"int");
_l.Append(&a,"int");
_l.Append(&a,"int");
_l.Append(&a,"int");
_l.Append(&a,"int");
NasalList _k;
_l.PrintList();
std::cout<<std::endl;
_k=_l.SubVec(9,4);
_k.PrintList();
std::cout<<std::endl;
_k=_l.SubVec(1,10);
_k.PrintList();
std::cout<<std::endl;
NasalList _m;
var _temp=_k.Pop();
_m.Append(&_temp,"var");
_k.PrintList();
std::cout<<std::endl;
_m.PrintList();
std::cout<<std::endl;
qthash.Append("var",&_temp,"var");
qthash.PrintHash();
return 0; return 0;
} }

View File

@ -12,6 +12,7 @@ NasalHash::NasalHash()
head->data=NULL; head->data=NULL;
head->next=NULL; head->next=NULL;
} }
NasalHash::NasalHash(NasalHash &Source) NasalHash::NasalHash(NasalHash &Source)
{ {
HashUnit *temp; HashUnit *temp;
@ -34,37 +35,37 @@ NasalHash::NasalHash(NasalHash &Source)
temp->data=new int; temp->data=new int;
*((int *)temp->data)=*((int *)SourceTemp->data); *((int *)temp->data)=*((int *)SourceTemp->data);
} }
if(temp->Type=="float") else if(temp->Type=="float")
{ {
temp->data=new float; temp->data=new float;
*((float *)temp->data)=*((float *)SourceTemp->data); *((float *)temp->data)=*((float *)SourceTemp->data);
} }
if(temp->Type=="double") else if(temp->Type=="double")
{ {
temp->data=new double; temp->data=new double;
*((double *)temp->data)=*((double *)SourceTemp->data); *((double *)temp->data)=*((double *)SourceTemp->data);
} }
if(temp->Type=="char") else if(temp->Type=="char")
{ {
temp->data=new char; temp->data=new char;
*((char *)temp->data)=*((char *)SourceTemp->data); *((char *)temp->data)=*((char *)SourceTemp->data);
} }
if(temp->Type=="long long int") else if(temp->Type=="long long int")
{ {
temp->data=new long long int; temp->data=new long long int;
*((long long int *)temp->data)=*((long long int *)SourceTemp->data); *((long long int *)temp->data)=*((long long int *)SourceTemp->data);
} }
if(temp->Type=="string") else if(temp->Type=="string")
{ {
temp->data=new std::string; temp->data=new std::string;
*((std::string *)temp->data)=*((std::string *)SourceTemp->data); *((std::string *)temp->data)=*((std::string *)SourceTemp->data);
} }
if(temp->Type=="array") else if(temp->Type=="array")
{ {
temp->data=new NasalList; temp->data=new NasalList;
*((NasalList *)temp->data)=*((NasalList *)SourceTemp->data); *((NasalList *)temp->data)=*((NasalList *)SourceTemp->data);
} }
if(temp->Type=="hash") else if(temp->Type=="hash")
{ {
temp->data=new NasalHash; temp->data=new NasalHash;
*((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data); *((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data);
@ -72,6 +73,7 @@ NasalHash::NasalHash(NasalHash &Source)
temp->next=NULL; temp->next=NULL;
} }
} }
NasalHash::~NasalHash() NasalHash::~NasalHash()
{ {
HashUnit *temp=head; HashUnit *temp=head;
@ -82,19 +84,19 @@ NasalHash::~NasalHash()
{ {
if(temp->Type=="int") if(temp->Type=="int")
delete (int *)temp->data; delete (int *)temp->data;
if(temp->Type=="float") else if(temp->Type=="float")
delete (float *)temp->data; delete (float *)temp->data;
if(temp->Type=="double") else if(temp->Type=="double")
delete (double *)temp->data; delete (double *)temp->data;
if(temp->Type=="char") else if(temp->Type=="char")
delete (char *)temp->data; delete (char *)temp->data;
if(temp->Type=="long long int") else if(temp->Type=="long long int")
delete (long long int *)temp->data; delete (long long int *)temp->data;
if(temp->Type=="string") else if(temp->Type=="string")
delete (std::string *)temp->data; delete (std::string *)temp->data;
if(temp->Type=="array") else if(temp->Type=="array")
delete (NasalList *)temp->data; delete (NasalList *)temp->data;
if(temp->Type=="hash") else if(temp->Type=="hash")
delete (NasalHash *)temp->data; delete (NasalHash *)temp->data;
} }
delete temp; delete temp;
@ -104,23 +106,24 @@ NasalHash::~NasalHash()
{ {
if(temp->Type=="int") if(temp->Type=="int")
delete (int *)temp->data; delete (int *)temp->data;
if(temp->Type=="float") else if(temp->Type=="float")
delete (float *)temp->data; delete (float *)temp->data;
if(temp->Type=="double") else if(temp->Type=="double")
delete (double *)temp->data; delete (double *)temp->data;
if(temp->Type=="char") else if(temp->Type=="char")
delete (char *)temp->data; delete (char *)temp->data;
if(temp->Type=="long long int") else if(temp->Type=="long long int")
delete (long long int *)temp->data; delete (long long int *)temp->data;
if(temp->Type=="string") else if(temp->Type=="string")
delete (std::string *)temp->data; delete (std::string *)temp->data;
if(temp->Type=="array") else if(temp->Type=="array")
delete (NasalList *)temp->data; delete (NasalList *)temp->data;
if(temp->Type=="hash") else if(temp->Type=="hash")
delete (NasalHash *)temp->data; delete (NasalHash *)temp->data;
} }
delete temp; delete temp;
} }
void NasalHash::PrintHash() void NasalHash::PrintHash()
{ {
HashUnit *temp=head; HashUnit *temp=head;
@ -133,22 +136,22 @@ void NasalHash::PrintHash()
{ {
if(temp->Type=="int") if(temp->Type=="int")
std::cout<<*((int *)temp->data)<<", "; std::cout<<*((int *)temp->data)<<", ";
if(temp->Type=="float") else if(temp->Type=="float")
std::cout<<*((float *)temp->data)<<", "; std::cout<<*((float *)temp->data)<<", ";
if(temp->Type=="double") else if(temp->Type=="double")
std::cout<<*((double *)temp->data)<<", "; std::cout<<*((double *)temp->data)<<", ";
if(temp->Type=="char") else if(temp->Type=="char")
std::cout<<"\""<<*((char *)temp->data)<<"\", "; std::cout<<"\""<<*((char *)temp->data)<<"\", ";
if(temp->Type=="long long int") else if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data)<<", "; std::cout<<*((long long int *)temp->data)<<", ";
if(temp->Type=="string") else if(temp->Type=="string")
std::cout<<"\""<<*((std::string *)temp->data)<<"\", "; std::cout<<"\""<<*((std::string *)temp->data)<<"\", ";
if(temp->Type=="array") else if(temp->Type=="array")
{ {
((NasalList *)temp->data)->PrintList(); ((NasalList *)temp->data)->PrintList();
std::cout<<", "; std::cout<<", ";
} }
if(temp->Type=="hash") else if(temp->Type=="hash")
{ {
((NasalHash *)temp->data)->PrintHash(); ((NasalHash *)temp->data)->PrintHash();
std::cout<<", "; std::cout<<", ";
@ -158,24 +161,25 @@ void NasalHash::PrintHash()
{ {
if(temp->Type=="int") if(temp->Type=="int")
std::cout<<*((int *)temp->data); std::cout<<*((int *)temp->data);
if(temp->Type=="float") else if(temp->Type=="float")
std::cout<<*((float *)temp->data); std::cout<<*((float *)temp->data);
if(temp->Type=="double") else if(temp->Type=="double")
std::cout<<*((double *)temp->data); std::cout<<*((double *)temp->data);
if(temp->Type=="char") else if(temp->Type=="char")
std::cout<<"\""<<*((char *)temp->data)<<"\""; std::cout<<"\""<<*((char *)temp->data)<<"\"";
if(temp->Type=="long long int") else if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data); std::cout<<*((long long int *)temp->data);
if(temp->Type=="string") else if(temp->Type=="string")
std::cout<<"\""<<*((std::string *)temp->data)<<"\""; std::cout<<"\""<<*((std::string *)temp->data)<<"\"";
if(temp->Type=="array") else if(temp->Type=="array")
((NasalList *)temp->data)->PrintList(); ((NasalList *)temp->data)->PrintList();
if(temp->Type=="hash") else if(temp->Type=="hash")
((NasalHash *)temp->data)->PrintHash(); ((NasalHash *)temp->data)->PrintHash();
} }
} }
std::cout<<"}"; std::cout<<"}";
} }
void NasalHash::Append(const char *VariaName,void *AppendData,const char *TypeName) void NasalHash::Append(const char *VariaName,void *AppendData,const char *TypeName)
{ {
NasalHash TempHash;//sometimes user may use n.append(n) NasalHash TempHash;//sometimes user may use n.append(n)
@ -231,6 +235,50 @@ void NasalHash::Append(const char *VariaName,void *AppendData,const char *TypeNa
NewHashMember->data=new NasalHash; NewHashMember->data=new NasalHash;
*((NasalHash *)NewHashMember->data)=TempHash; *((NasalHash *)NewHashMember->data)=TempHash;
} }
else if(TypeName=="var")
{
NewHashMember->Type=(*(var *)AppendData).Type;
if((*(var *)AppendData).Type=="int")
{
NewHashMember->data=new int;
*((int *)NewHashMember->data)=*((int *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="float")
{
NewHashMember->data=new float;
*((float *)NewHashMember->data)=*((float *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="double")
{
NewHashMember->data=new double;
*((double *)NewHashMember->data)=*((double *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="char")
{
NewHashMember->data=new char;
*((char *)NewHashMember->data)=*((char *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="long long int")
{
NewHashMember->data=new long long int;
*((long long int *)NewHashMember->data)=*((long long int *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="string")
{
NewHashMember->data=new std::string;
*((std::string *)NewHashMember->data)=*((std::string *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="array")
{
NewHashMember->data=new NasalList;
*((NasalList *)NewHashMember->data)=*((NasalList *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="hash")
{
NewHashMember->data=new NasalHash;
*((NasalHash *)NewHashMember->data)=*((NasalHash *)(*(var *)AppendData).data);
}
}
NewHashMember->next=NULL; NewHashMember->next=NULL;
} }
int NasalHash::Contains(const char *VariaName) int NasalHash::Contains(const char *VariaName)
@ -244,6 +292,7 @@ int NasalHash::Contains(const char *VariaName)
} }
return 0; return 0;
} }
NasalList NasalHash::Keys() NasalList NasalHash::Keys()
{ {
NasalList FeedBackList; NasalList FeedBackList;
@ -255,6 +304,7 @@ NasalList NasalHash::Keys()
} }
return FeedBackList; return FeedBackList;
} }
void NasalHash::Delete(const char *VariaName) void NasalHash::Delete(const char *VariaName)
{ {
HashUnit *temp=head; HashUnit *temp=head;
@ -268,19 +318,19 @@ void NasalHash::Delete(const char *VariaName)
LastNode->next=temp->next; LastNode->next=temp->next;
if(temp->Type=="int") if(temp->Type=="int")
delete (int *)temp->data; delete (int *)temp->data;
if(temp->Type=="float") else if(temp->Type=="float")
delete (float *)temp->data; delete (float *)temp->data;
if(temp->Type=="double") else if(temp->Type=="double")
delete (double *)temp->data; delete (double *)temp->data;
if(temp->Type=="char") else if(temp->Type=="char")
delete (char *)temp->data; delete (char *)temp->data;
if(temp->Type=="long long int") else if(temp->Type=="long long int")
delete (long long int *)temp->data; delete (long long int *)temp->data;
if(temp->Type=="string") else if(temp->Type=="string")
delete (std::string *)temp->data; delete (std::string *)temp->data;
if(temp->Type=="array") else if(temp->Type=="array")
delete (NasalList *)temp->data; delete (NasalList *)temp->data;
if(temp->Type=="hash") else if(temp->Type=="hash")
delete (NasalHash *)temp->data; delete (NasalHash *)temp->data;
delete temp; delete temp;
return; return;
@ -289,6 +339,7 @@ void NasalHash::Delete(const char *VariaName)
std::cout<<"[Error]: Could not find this element \""<<VariaName<<"\"."<<std::endl; std::cout<<"[Error]: Could not find this element \""<<VariaName<<"\"."<<std::endl;
return; return;
} }
NasalHash& NasalHash::operator=(const NasalHash &Source) NasalHash& NasalHash::operator=(const NasalHash &Source)
{ {
HashUnit *temp=head; HashUnit *temp=head;
@ -300,19 +351,19 @@ NasalHash& NasalHash::operator=(const NasalHash &Source)
{ {
if(temp->Type=="int") if(temp->Type=="int")
delete (int *)temp->data; delete (int *)temp->data;
if(temp->Type=="float") else if(temp->Type=="float")
delete (float *)temp->data; delete (float *)temp->data;
if(temp->Type=="double") else if(temp->Type=="double")
delete (double *)temp->data; delete (double *)temp->data;
if(temp->Type=="char") else if(temp->Type=="char")
delete (char *)temp->data; delete (char *)temp->data;
if(temp->Type=="long long int") else if(temp->Type=="long long int")
delete (long long int *)temp->data; delete (long long int *)temp->data;
if(temp->Type=="string") else if(temp->Type=="string")
delete (std::string *)temp->data; delete (std::string *)temp->data;
if(temp->Type=="array") else if(temp->Type=="array")
delete (NasalList *)temp->data; delete (NasalList *)temp->data;
if(temp->Type=="hash") else if(temp->Type=="hash")
delete (NasalHash *)temp->data; delete (NasalHash *)temp->data;
} }
delete temp; delete temp;
@ -322,19 +373,19 @@ NasalHash& NasalHash::operator=(const NasalHash &Source)
{ {
if(temp->Type=="int") if(temp->Type=="int")
delete (int *)temp->data; delete (int *)temp->data;
if(temp->Type=="float") else if(temp->Type=="float")
delete (float *)temp->data; delete (float *)temp->data;
if(temp->Type=="double") else if(temp->Type=="double")
delete (double *)temp->data; delete (double *)temp->data;
if(temp->Type=="char") else if(temp->Type=="char")
delete (char *)temp->data; delete (char *)temp->data;
if(temp->Type=="long long int") else if(temp->Type=="long long int")
delete (long long int *)temp->data; delete (long long int *)temp->data;
if(temp->Type=="string") else if(temp->Type=="string")
delete (std::string *)temp->data; delete (std::string *)temp->data;
if(temp->Type=="array") else if(temp->Type=="array")
delete (NasalList *)temp->data; delete (NasalList *)temp->data;
if(temp->Type=="hash") else if(temp->Type=="hash")
delete (NasalHash *)temp->data; delete (NasalHash *)temp->data;
} }
delete temp; delete temp;
@ -354,37 +405,37 @@ NasalHash& NasalHash::operator=(const NasalHash &Source)
temp->data=new int; temp->data=new int;
*((int *)temp->data)=*((int *)SourceTemp->data); *((int *)temp->data)=*((int *)SourceTemp->data);
} }
if(temp->Type=="float") else if(temp->Type=="float")
{ {
temp->data=new float; temp->data=new float;
*((float *)temp->data)=*((float *)SourceTemp->data); *((float *)temp->data)=*((float *)SourceTemp->data);
} }
if(temp->Type=="double") else if(temp->Type=="double")
{ {
temp->data=new double; temp->data=new double;
*((double *)temp->data)=*((double *)SourceTemp->data); *((double *)temp->data)=*((double *)SourceTemp->data);
} }
if(temp->Type=="char") else if(temp->Type=="char")
{ {
temp->data=new char; temp->data=new char;
*((char *)temp->data)=*((char *)SourceTemp->data); *((char *)temp->data)=*((char *)SourceTemp->data);
} }
if(temp->Type=="long long int") else if(temp->Type=="long long int")
{ {
temp->data=new long long int; temp->data=new long long int;
*((long long int *)temp->data)=*((long long int *)SourceTemp->data); *((long long int *)temp->data)=*((long long int *)SourceTemp->data);
} }
if(temp->Type=="string") else if(temp->Type=="string")
{ {
temp->data=new std::string; temp->data=new std::string;
*((std::string *)temp->data)=*((std::string *)SourceTemp->data); *((std::string *)temp->data)=*((std::string *)SourceTemp->data);
} }
if(temp->Type=="array") else if(temp->Type=="array")
{ {
temp->data=new NasalList; temp->data=new NasalList;
*((NasalList *)temp->data)=*((NasalList *)SourceTemp->data); *((NasalList *)temp->data)=*((NasalList *)SourceTemp->data);
} }
if(temp->Type=="hash") else if(temp->Type=="hash")
{ {
temp->data=new NasalHash; temp->data=new NasalHash;
*((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data); *((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data);

View File

@ -7,6 +7,9 @@
namespace nasal namespace nasal
{ {
#ifndef nil
#define nil -1
#endif
struct HashUnit struct HashUnit
{ {

View File

@ -12,6 +12,7 @@ NasalList::NasalList()
head->data=NULL; head->data=NULL;
head->next=NULL; head->next=NULL;
} }
NasalList::NasalList(NasalList &Source) NasalList::NasalList(NasalList &Source)
{ {
ListUnit *temp; ListUnit *temp;
@ -32,44 +33,49 @@ NasalList::NasalList(NasalList &Source)
temp->data=new int; temp->data=new int;
*((int *)temp->data)=*((int *)SourceTemp->data); *((int *)temp->data)=*((int *)SourceTemp->data);
} }
if(temp->Type=="float") else if(temp->Type=="float")
{ {
temp->data=new float; temp->data=new float;
*((float *)temp->data)=*((float *)SourceTemp->data); *((float *)temp->data)=*((float *)SourceTemp->data);
} }
if(temp->Type=="double") else if(temp->Type=="double")
{ {
temp->data=new double; temp->data=new double;
*((double *)temp->data)=*((double *)SourceTemp->data); *((double *)temp->data)=*((double *)SourceTemp->data);
} }
if(temp->Type=="char") else if(temp->Type=="char")
{ {
temp->data=new char; temp->data=new char;
*((char *)temp->data)=*((char *)SourceTemp->data); *((char *)temp->data)=*((char *)SourceTemp->data);
} }
if(temp->Type=="long long int") else if(temp->Type=="long long int")
{ {
temp->data=new long long int; temp->data=new long long int;
*((long long int *)temp->data)=*((long long int *)SourceTemp->data); *((long long int *)temp->data)=*((long long int *)SourceTemp->data);
} }
if(temp->Type=="string") else if(temp->Type=="string")
{ {
temp->data=new std::string; temp->data=new std::string;
*((std::string *)temp->data)=*((std::string *)SourceTemp->data); *((std::string *)temp->data)=*((std::string *)SourceTemp->data);
} }
if(temp->Type=="array") else if(temp->Type=="array")
{ {
temp->data=new NasalList; temp->data=new NasalList;
*((NasalList *)temp->data)=*((NasalList *)SourceTemp->data); *((NasalList *)temp->data)=*((NasalList *)SourceTemp->data);
} }
if(temp->Type=="hash") else if(temp->Type=="hash")
{ {
temp->data=new NasalHash; temp->data=new NasalHash;
*((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data); *((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data);
} }
else if(temp->Type=="null")
{
temp->data=NULL;
}
temp->next=NULL; temp->next=NULL;
} }
} }
NasalList::~NasalList() NasalList::~NasalList()
{ {
ListUnit *temp=head; ListUnit *temp=head;
@ -80,19 +86,19 @@ NasalList::~NasalList()
{ {
if(temp->Type=="int") if(temp->Type=="int")
delete (int *)temp->data; delete (int *)temp->data;
if(temp->Type=="float") else if(temp->Type=="float")
delete (float *)temp->data; delete (float *)temp->data;
if(temp->Type=="double") else if(temp->Type=="double")
delete (double *)temp->data; delete (double *)temp->data;
if(temp->Type=="char") else if(temp->Type=="char")
delete (char *)temp->data; delete (char *)temp->data;
if(temp->Type=="long long int") else if(temp->Type=="long long int")
delete (long long int *)temp->data; delete (long long int *)temp->data;
if(temp->Type=="string") else if(temp->Type=="string")
delete (std::string *)temp->data; delete (std::string *)temp->data;
if(temp->Type=="array") else if(temp->Type=="array")
delete (NasalList *)temp->data; delete (NasalList *)temp->data;
if(temp->Type=="hash") else if(temp->Type=="hash")
delete (NasalHash *)temp->data; delete (NasalHash *)temp->data;
} }
delete temp; delete temp;
@ -102,19 +108,19 @@ NasalList::~NasalList()
{ {
if(temp->Type=="int") if(temp->Type=="int")
delete (int *)temp->data; delete (int *)temp->data;
if(temp->Type=="float") else if(temp->Type=="float")
delete (float *)temp->data; delete (float *)temp->data;
if(temp->Type=="double") else if(temp->Type=="double")
delete (double *)temp->data; delete (double *)temp->data;
if(temp->Type=="char") else if(temp->Type=="char")
delete (char *)temp->data; delete (char *)temp->data;
if(temp->Type=="long long int") else if(temp->Type=="long long int")
delete (long long int *)temp->data; delete (long long int *)temp->data;
if(temp->Type=="string") else if(temp->Type=="string")
delete (std::string *)temp->data; delete (std::string *)temp->data;
if(temp->Type=="array") else if(temp->Type=="array")
delete (NasalList *)temp->data; delete (NasalList *)temp->data;
if(temp->Type=="hash") else if(temp->Type=="hash")
delete (NasalHash *)temp->data; delete (NasalHash *)temp->data;
} }
delete temp; delete temp;
@ -131,45 +137,51 @@ void NasalList::PrintList()
{ {
if(temp->Type=="int") if(temp->Type=="int")
std::cout<<*((int *)temp->data)<<", "; std::cout<<*((int *)temp->data)<<", ";
if(temp->Type=="float") else if(temp->Type=="float")
std::cout<<*((float *)temp->data)<<", "; std::cout<<*((float *)temp->data)<<", ";
if(temp->Type=="double") else if(temp->Type=="double")
std::cout<<*((double *)temp->data)<<", "; std::cout<<*((double *)temp->data)<<", ";
if(temp->Type=="char") else if(temp->Type=="char")
std::cout<<"\""<<*((char *)temp->data)<<"\", "; std::cout<<"\""<<*((char *)temp->data)<<"\", ";
if(temp->Type=="long long int") else if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data)<<", "; std::cout<<*((long long int *)temp->data)<<", ";
if(temp->Type=="string") else if(temp->Type=="string")
std::cout<<"\""<<*((std::string *)temp->data)<<"\", "; std::cout<<"\""<<*((std::string *)temp->data)<<"\", ";
if(temp->Type=="array") else if(temp->Type=="array")
{ {
((NasalList *)temp->data)->PrintList(); ((NasalList *)temp->data)->PrintList();
std::cout<<", "; std::cout<<", ";
} }
if(temp->Type=="hash") else if(temp->Type=="hash")
{ {
((NasalHash *)temp->data)->PrintHash(); ((NasalHash *)temp->data)->PrintHash();
std::cout<<", "; std::cout<<", ";
} }
else if(temp->Type=="null")
{
std::cout<<"NULL, ";
}
} }
else else
{ {
if(temp->Type=="int") if(temp->Type=="int")
std::cout<<*((int *)temp->data); std::cout<<*((int *)temp->data);
if(temp->Type=="float") else if(temp->Type=="float")
std::cout<<*((float *)temp->data); std::cout<<*((float *)temp->data);
if(temp->Type=="double") else if(temp->Type=="double")
std::cout<<*((double *)temp->data); std::cout<<*((double *)temp->data);
if(temp->Type=="char") else if(temp->Type=="char")
std::cout<<"\""<<*((char *)temp->data)<<"\""; std::cout<<"\""<<*((char *)temp->data)<<"\"";
if(temp->Type=="long long int") else if(temp->Type=="long long int")
std::cout<<*((long long int *)temp->data); std::cout<<*((long long int *)temp->data);
if(temp->Type=="string") else if(temp->Type=="string")
std::cout<<"\""<<*((std::string *)temp->data)<<"\""; std::cout<<"\""<<*((std::string *)temp->data)<<"\"";
if(temp->Type=="array") else if(temp->Type=="array")
((NasalList *)temp->data)->PrintList(); ((NasalList *)temp->data)->PrintList();
if(temp->Type=="hash") else if(temp->Type=="hash")
((NasalHash *)temp->data)->PrintHash(); ((NasalHash *)temp->data)->PrintHash();
else if(temp->Type=="null")
std::cout<<"NULL";
} }
} }
std::cout<<"]"; std::cout<<"]";
@ -194,37 +206,138 @@ void NasalList::Append(void *AppendData,const char *TypeName)
NewListMember->data=new int; NewListMember->data=new int;
*((int *)NewListMember->data)=*((int *)AppendData); *((int *)NewListMember->data)=*((int *)AppendData);
} }
if(TypeName=="float") else if(TypeName=="float")
{ {
NewListMember->data=new float; NewListMember->data=new float;
*((float *)NewListMember->data)=*((float *)AppendData); *((float *)NewListMember->data)=*((float *)AppendData);
} }
if(TypeName=="double") else if(TypeName=="double")
{ {
NewListMember->data=new double; NewListMember->data=new double;
*((double *)NewListMember->data)=*((double *)AppendData); *((double *)NewListMember->data)=*((double *)AppendData);
} }
if(TypeName=="char") else if(TypeName=="char")
{ {
NewListMember->data=new char; NewListMember->data=new char;
*((char *)NewListMember->data)=*((char *)AppendData); *((char *)NewListMember->data)=*((char *)AppendData);
} }
if(TypeName=="long long int") else if(TypeName=="long long int")
{ {
NewListMember->data=new long long int; NewListMember->data=new long long int;
*((long long int *)NewListMember->data)=*((long long int *)AppendData); *((long long int *)NewListMember->data)=*((long long int *)AppendData);
} }
if(TypeName=="string") else if(TypeName=="string")
{ {
NewListMember->data=new std::string; NewListMember->data=new std::string;
*((std::string *)NewListMember->data)=*((std::string *)AppendData); *((std::string *)NewListMember->data)=*((std::string *)AppendData);
} }
if(TypeName=="array") else if(TypeName=="array")
{ {
NewListMember->data=new NasalList; NewListMember->data=new NasalList;
*((NasalList *)NewListMember->data)=TempList; *((NasalList *)NewListMember->data)=TempList;
} }
if(TypeName=="hash") else if(TypeName=="hash")
{
NewListMember->data=new NasalHash;
*((NasalHash *)NewListMember->data)=*((NasalHash *)AppendData);
}
else if(TypeName=="var")
{
NewListMember->Type=(*(var *)AppendData).Type;
if((*(var *)AppendData).Type=="int")
{
NewListMember->data=new int;
*((int *)NewListMember->data)=*((int *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="float")
{
NewListMember->data=new float;
*((float *)NewListMember->data)=*((float *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="double")
{
NewListMember->data=new double;
*((double *)NewListMember->data)=*((double *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="char")
{
NewListMember->data=new char;
*((char *)NewListMember->data)=*((char *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="long long int")
{
NewListMember->data=new long long int;
*((long long int *)NewListMember->data)=*((long long int *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="string")
{
NewListMember->data=new std::string;
*((std::string *)NewListMember->data)=*((std::string *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="array")
{
NewListMember->data=new NasalList;
*((NasalList *)NewListMember->data)=*((NasalList *)(*(var *)AppendData).data);
}
else if((*(var *)AppendData).Type=="hash")
{
NewListMember->data=new NasalHash;
*((NasalHash *)NewListMember->data)=*((NasalHash *)(*(var *)AppendData).data);
}
}
NewListMember->next=NULL;
}
void NasalList::Append(void *AppendData,std::string &TypeName)
{
NasalList TempList;//sometimes user may use n.append(n)
if(TypeName=="array")
TempList=*((NasalList *)AppendData);
ListUnit *temp=head;
while(temp->next)
{
temp=temp->next;
}
ListUnit *NewListMember=new ListUnit;
temp->next=NewListMember;
NewListMember->data=NULL;
NewListMember->Type=TypeName;
if(TypeName=="int")
{
NewListMember->data=new int;
*((int *)NewListMember->data)=*((int *)AppendData);
}
else if(TypeName=="float")
{
NewListMember->data=new float;
*((float *)NewListMember->data)=*((float *)AppendData);
}
else if(TypeName=="double")
{
NewListMember->data=new double;
*((double *)NewListMember->data)=*((double *)AppendData);
}
else if(TypeName=="char")
{
NewListMember->data=new char;
*((char *)NewListMember->data)=*((char *)AppendData);
}
else if(TypeName=="long long int")
{
NewListMember->data=new long long int;
*((long long int *)NewListMember->data)=*((long long int *)AppendData);
}
else if(TypeName=="string")
{
NewListMember->data=new std::string;
*((std::string *)NewListMember->data)=*((std::string *)AppendData);
}
else if(TypeName=="array")
{
NewListMember->data=new NasalList;
*((NasalList *)NewListMember->data)=TempList;
}
else if(TypeName=="hash")
{ {
NewListMember->data=new NasalHash; NewListMember->data=new NasalHash;
*((NasalHash *)NewListMember->data)=*((NasalHash *)AppendData); *((NasalHash *)NewListMember->data)=*((NasalHash *)AppendData);
@ -243,19 +356,19 @@ NasalList& NasalList::operator=(const NasalList &Source)
{ {
if(temp->Type=="int") if(temp->Type=="int")
delete (int *)temp->data; delete (int *)temp->data;
if(temp->Type=="float") else if(temp->Type=="float")
delete (float *)temp->data; delete (float *)temp->data;
if(temp->Type=="double") else if(temp->Type=="double")
delete (double *)temp->data; delete (double *)temp->data;
if(temp->Type=="char") else if(temp->Type=="char")
delete (char *)temp->data; delete (char *)temp->data;
if(temp->Type=="long long int") else if(temp->Type=="long long int")
delete (long long int *)temp->data; delete (long long int *)temp->data;
if(temp->Type=="string") else if(temp->Type=="string")
delete (std::string *)temp->data; delete (std::string *)temp->data;
if(temp->Type=="array") else if(temp->Type=="array")
delete (NasalList *)temp->data; delete (NasalList *)temp->data;
if(temp->Type=="hash") else if(temp->Type=="hash")
delete (NasalHash *)temp->data; delete (NasalHash *)temp->data;
} }
delete temp; delete temp;
@ -296,46 +409,313 @@ NasalList& NasalList::operator=(const NasalList &Source)
temp->data=new int; temp->data=new int;
*((int *)temp->data)=*((int *)SourceTemp->data); *((int *)temp->data)=*((int *)SourceTemp->data);
} }
if(temp->Type=="float") else if(temp->Type=="float")
{ {
temp->data=new float; temp->data=new float;
*((float *)temp->data)=*((float *)SourceTemp->data); *((float *)temp->data)=*((float *)SourceTemp->data);
} }
if(temp->Type=="double") else if(temp->Type=="double")
{ {
temp->data=new double; temp->data=new double;
*((double *)temp->data)=*((double *)SourceTemp->data); *((double *)temp->data)=*((double *)SourceTemp->data);
} }
if(temp->Type=="char") else if(temp->Type=="char")
{ {
temp->data=new char; temp->data=new char;
*((char *)temp->data)=*((char *)SourceTemp->data); *((char *)temp->data)=*((char *)SourceTemp->data);
} }
if(temp->Type=="long long int") else if(temp->Type=="long long int")
{ {
temp->data=new long long int; temp->data=new long long int;
*((long long int *)temp->data)=*((long long int *)SourceTemp->data); *((long long int *)temp->data)=*((long long int *)SourceTemp->data);
} }
if(temp->Type=="string") else if(temp->Type=="string")
{ {
temp->data=new std::string; temp->data=new std::string;
*((std::string *)temp->data)=*((std::string *)SourceTemp->data); *((std::string *)temp->data)=*((std::string *)SourceTemp->data);
} }
if(temp->Type=="array") else if(temp->Type=="array")
{ {
temp->data=new NasalList; temp->data=new NasalList;
*((NasalList *)temp->data)=*((NasalList *)SourceTemp->data); *((NasalList *)temp->data)=*((NasalList *)SourceTemp->data);
} }
if(temp->Type=="hash") else if(temp->Type=="hash")
{ {
temp->data=new NasalHash; temp->data=new NasalHash;
*((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data); *((NasalHash *)temp->data)=*((NasalHash *)SourceTemp->data);
} }
else if(temp->Type=="null")
{
temp->data=NULL;
}
temp->next=NULL; temp->next=NULL;
} }
return *this; return *this;
} }
void NasalList::SetSize(const int Size)
{
if(Size==0)
{
ListUnit *temp=head;
ListUnit *DeleteNode;
temp=temp->next;
head->next=NULL;
while(temp->next)
{
DeleteNode=temp;
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
else if(temp->Type=="float")
delete (float *)temp->data;
else if(temp->Type=="double")
delete (double *)temp->data;
else if(temp->Type=="char")
delete (char *)temp->data;
else if(temp->Type=="long long int")
delete (long long int *)temp->data;
else if(temp->Type=="string")
delete (std::string *)temp->data;
else if(temp->Type=="array")
delete (NasalList *)temp->data;
else if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
temp=temp->next;
delete DeleteNode;
}
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
else if(temp->Type=="float")
delete (float *)temp->data;
else if(temp->Type=="double")
delete (double *)temp->data;
else if(temp->Type=="char")
delete (char *)temp->data;
else if(temp->Type=="long long int")
delete (long long int *)temp->data;
else if(temp->Type=="string")
delete (std::string *)temp->data;
else if(temp->Type=="array")
delete (NasalList *)temp->data;
else if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
return;
}
int cnt=0;
ListUnit *temp=head;
while(temp->next)
{
temp=temp->next;
++cnt;
if(cnt==Size)
{
ListUnit *DeleteNode=temp->next;
temp->next=NULL;
temp=DeleteNode;
while(DeleteNode->next)
{
DeleteNode=DeleteNode->next;
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
else if(temp->Type=="float")
delete (float *)temp->data;
else if(temp->Type=="double")
delete (double *)temp->data;
else if(temp->Type=="char")
delete (char *)temp->data;
else if(temp->Type=="long long int")
delete (long long int *)temp->data;
else if(temp->Type=="string")
delete (std::string *)temp->data;
else if(temp->Type=="array")
delete (NasalList *)temp->data;
else if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
temp=DeleteNode;
}
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
else if(temp->Type=="float")
delete (float *)temp->data;
else if(temp->Type=="double")
delete (double *)temp->data;
else if(temp->Type=="char")
delete (char *)temp->data;
else if(temp->Type=="long long int")
delete (long long int *)temp->data;
else if(temp->Type=="string")
delete (std::string *)temp->data;
else if(temp->Type=="array")
delete (NasalList *)temp->data;
else if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
return;
}
}
while(cnt<Size)
{
temp->next=new ListUnit;
temp=temp->next;
temp->Type="null";
temp->data=NULL;
++cnt;
}
temp->next=NULL;
return;
}
NasalList NasalList::SubVec(const int start,const int length=nil)
{
int cnt=-1;
ListUnit *temp=head;
while(temp->next)
{
temp=temp->next;
++cnt;
if(cnt==start)
{
NasalList NewNasalList;
while(temp->next)
{
NewNasalList.Append(temp->data,temp->Type);
temp=temp->next;
if(cnt==length)
return NewNasalList;
++cnt;
}
if(length!=nil&&cnt<length)
std::cout<<"[Error] Length out of range: (length) "<<length<<"|(range) "<<cnt<<"."<<std::endl;
NewNasalList.Append(temp->data,temp->Type);
return NewNasalList;
}
}
std::cout<<"[Error] Starting place out of range: (start) "<<start<<"|(range) "<<cnt<<"."<<std::endl;
NasalList NullList;
NullList.SetSize(1);
return NullList;
}
var NasalList::Pop()
{
ListUnit *temp=head;
ListUnit *NewLastUnit;
while(temp->next)
{
NewLastUnit=temp;
temp=temp->next;
}
NewLastUnit->next=NULL;
var TempVar;
TempVar.Type=temp->Type;
if(TempVar.Type=="int")
{
TempVar.data=new int;
*((int *)TempVar.data)=*((int *)temp->data);
}
else if(TempVar.Type=="float")
{
TempVar.data=new float;
*((float *)TempVar.data)=*((float *)temp->data);
}
else if(TempVar.Type=="double")
{
TempVar.data=new double;
*((double *)TempVar.data)=*((double *)temp->data);
}
else if(TempVar.Type=="char")
{
TempVar.data=new char;
*((char *)TempVar.data)=*((char *)temp->data);
}
else if(TempVar.Type=="long long int")
{
TempVar.data=new long long int;
*((long long int *)TempVar.data)=*((long long int *)temp->data);
}
else if(TempVar.Type=="string")
{
TempVar.data=new std::string;
*((std::string *)TempVar.data)=*((std::string *)temp->data);
}
else if(TempVar.Type=="array")
{
TempVar.data=new NasalList;
*((NasalList *)TempVar.data)=*((NasalList *)temp->data);
}
else if(TempVar.Type=="hash")
{
TempVar.data=new NasalHash;
*((NasalHash *)TempVar.data)=*((NasalHash*)temp->data);
}
else if(TempVar.Type=="null")
TempVar.data=NULL;
if(temp->data)
{
if(temp->Type=="int")
delete (int *)temp->data;
else if(temp->Type=="float")
delete (float *)temp->data;
else if(temp->Type=="double")
delete (double *)temp->data;
else if(temp->Type=="char")
delete (char *)temp->data;
else if(temp->Type=="long long int")
delete (long long int *)temp->data;
else if(temp->Type=="string")
delete (std::string *)temp->data;
else if(temp->Type=="array")
delete (NasalList *)temp->data;
else if(temp->Type=="hash")
delete (NasalHash *)temp->data;
}
delete temp;
return TempVar;
}
void NasalList::Sort(bool _cmp_1,bool _cmp_2)
{
return;
}
var::~var()
{
if(data)
{
if(Type=="int")
delete (int *)data;
else if(Type=="float")
delete (float *)data;
else if(Type=="double")
delete (double *)data;
else if(Type=="char")
delete (char *)data;
else if(Type=="long long int")
delete (long long int *)data;
else if(Type=="string")
delete (std::string *)data;
else if(Type=="array")
delete (NasalList *)data;
else if(Type=="hash")
delete (NasalHash *)data;
}
}
} }
#endif #endif

View File

@ -7,6 +7,11 @@
namespace nasal namespace nasal
{ {
#ifndef nil
#define nil -1
#endif
class var;
struct ListUnit struct ListUnit
{ {
@ -25,7 +30,24 @@ class NasalList
~NasalList(); ~NasalList();
void PrintList(); void PrintList();
void Append(void *,const char *); void Append(void *,const char *);
void Append(void *,std::string &);
NasalList& operator=(const NasalList &); NasalList& operator=(const NasalList &);
void SetSize(const int);
NasalList SubVec(const int,const int);
var Pop();
void Sort(bool,bool);
};
class var
{
public:
std::string Type;
void *data;
var()
{
data=NULL;
}
~var();
}; };
} }