|
/* |
|
** code, provides dictionary class |
|
** written for RPG game core project "GAME OF SCHOOL SURVIVAL" |
|
** by SETT SARVEROTT |
|
*/ |
|
#include <string> |
|
namespace carbonman{ |
|
using namespace std; |
|
struct dictionaryCell{ |
|
dictionaryCell *next=NULL; |
|
string key=""; |
|
string value=""; |
|
}; |
|
class dictionary{ |
|
private: |
|
dictionaryCell *beg=NULL; |
|
void push(string key, string value){ |
|
dictionaryCell *tmp=new dictionaryCell; |
|
tmp->key=key; |
|
tmp->value=value; |
|
dictionaryCell *current=this->beg; |
|
while(current->next!=NULL){ |
|
current=current->next; |
|
} |
|
current->next=tmp; |
|
} |
|
void unshift(string key, string value){ |
|
dictionaryCell *tmp=new dictionaryCell; |
|
tmp->key=key; |
|
tmp->value=value; |
|
tmp->next=this->beg; |
|
this->beg=tmp; |
|
} |
|
void update(string key, string value){ |
|
dictionaryCell *current=this->beg; |
|
while(current->key!=key){ |
|
current=current->next; |
|
} |
|
current->value=value; |
|
} |
|
pubic: |
|
int length(){ /* ~~ HOW BIG IS IT? ~~ */ |
|
int tmp=0; |
|
dictionaryCell *current=this->beg; |
|
while(current!=NULL){ |
|
tmp++; |
|
current=current->next; |
|
} |
|
return tmp; |
|
} |
|
string getKey(int index){ /* ~~ GET KEY! ~~ */ |
|
if(index<this->length()&&index>0){ |
|
dictionaryCell *current=this->beg; |
|
while(--index!=0){ |
|
current=current->next; |
|
} |
|
return current->key; |
|
}else{ |
|
//throw |
|
return ""; |
|
} |
|
} |
|
bool isKeyExist(string key){ /* ~~ IS KEY EXISTS? ~~ */ |
|
dictionaryCell *current=this->beg; |
|
while(current!=NULL){ |
|
if(current->key==key){ |
|
return true; |
|
}else{ |
|
current=current->next; |
|
} |
|
} |
|
return false; |
|
} |
|
void removeKey(string key){ /* ~~ DELETE CELL! ~~ */ |
|
if(this->isKeyExist(key)){ |
|
dictionaryCell *current=this->beg; |
|
dictionaryCell *before=NULL; |
|
if(this->beg->key==key){ |
|
this->beg=this->beg->next; |
|
} |
|
while(current->next->key!=key){ |
|
current=current->next; |
|
} |
|
current->next=current->next->next |
|
}else{ |
|
//throw |
|
} |
|
} |
|
string getValue(string key){ /* ~~ GET VAL BY KEY! ~~ */ |
|
if(this->isKeyExist(key)){ |
|
dictionaryCell *current=this->beg; |
|
while(current->key!=key){ |
|
current=current->next; |
|
} |
|
return current->value; |
|
}else{ |
|
//throw |
|
return ""; |
|
} |
|
} |
|
void setValue(string key, string value){ /* ~~ SET VAL ~~ */ |
|
if(!this->isKeyExist(key)){ |
|
this->push(key, value); |
|
}else{ |
|
this->update(key, value); |
|
} |
|
} |
|
dictionary(){ |
|
/* unused yet */ |
|
} |
|
}; |
|
} |
|
using carbonman::dictionary; |