Skip to content

Instantly share code, notes, and snippets.

@Sarverott
Last active December 9, 2018 00:50
Show Gist options
  • Save Sarverott/cb66a565dbca9f932a35fa5dda83ee3f to your computer and use it in GitHub Desktop.
Save Sarverott/cb66a565dbca9f932a35fa5dda83ee3f to your computer and use it in GitHub Desktop.

Dictionary class

based on Python concept of dictionary type, provides access to assocc array

usage

dictionary slownik;
dictionary *wikipedia=new dictionary;

methods

/*
** 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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment