mardi 26 mars 2019

looking for specific Design Pattern in C++ that solve this problem

I am looking for specific Design Pattern in C++ that solve this problem.

I want to design a Storyboard. Our version of the Storyboard contains arbitrary many notes (imagine it like putting sticky notes on a board). Every note has a title, a text and a set of tags. E.g. - title: "Test Traceplayer" - text: "Implement a unit test for the class Traceplayer of the spark core framework." - tags: {"unit test", "traceplayer", "testing", "spark core"}

Our Storyboard should enable us to search for notes by title, text and tags. E.g.: searchByTitle( "Test Traceplayer" ) searchByTag({"testing", "unit test"}) searchByText("Implement a unit test for the class Traceplayer of the spark core framework.") For the sake of simplicity we don't want to do any similiarity or prefix matching when searching for a title, tag or text. Only an exact match should give results.

I have number of solution that solve this problem O(1) search complexity But can any one suggest any "Design Pattern" that solve this problem.

Solve that issue with three STL map and get constant time search complexity

Looking for a specific Design Pattern that solves this problem.

I have solved this problem using 3 STL Map and solution get O(1) search complexity

#include <iostream>
#include <vector>
#include <map>

#define INPUT 8 

class Note {
public:
    string Tital;
    string Text;
    vector<string> vec;
    Note(){
        Tital = "\0";
        Text = "\0";
    }
};

class storyBoard{
public:
    void AddNote(string Tital,string Text,vector<string> vec );
    void RemoveByTital(string &tital);
    void PrintStoredData();
    Note* searchByTitle(string titleSearch);

    Note* searchByText(string text_);
    vector<Note*> searchByTag(string titleSearch);

    void printSlip(Note *tm);
    storyBoard(){}
private:

    std::map<string,Note *> TitalMap;
    std::map<string,Note *> TextMap;
    std::map<string,std::vector<Note *> > TagsMap;
};


Note* storyBoard::searchByTitle(string titleSearch){
    auto it_v = TitalMap.find(titleSearch);
    if (it_v != TitalMap.end()){
        cout<< "Tital search result is below:-"<<endl;
        return it_v->second;
    } else {
        cout <<"data "<<titleSearch << "  Not found"<<endl;
        return NULL;
    }
}

Note* storyBoard::searchByText(string titleSearch){
    auto it_v = TextMap.find(titleSearch);
    if (it_v != TextMap.end()){
        cout<< "Text search result is below:-"<<endl;
        return it_v->second;
    } else {
        cout <<"data "<<titleSearch << "  Not found"<<endl;
        return NULL;
    }
}

vector<Note*> storyBoard::searchByTag(string tagSearch){
    auto it_v = TagsMap.find(tagSearch);
    if (it_v != TagsMap.end()){
        cout<< "Tag search result is below:-"<<endl;
        return it_v->second;
    } else {
        cout <<"data "<<tagSearch << "  Not found"<<endl;
        vector<Note*> del;
        return del;
    }
}


void storyBoard::AddNote(string Tital, string Text, vector<string> v){
   Note *note = new Note;
   note->Tital = Tital;
   note->Text = Text;
   note->vec = v;

   TitalMap[note->Tital] = note;
   TextMap[note->Text] = note;

   for (auto it = note->vec.begin(); it != note->vec.end(); ++it){
       //check that is tags already 
       auto it_v = TagsMap.find(*it);
       if (it_v != TagsMap.end()){
           it_v->second. push_back(note);
       } else {
           vector<Note *> &v = TagsMap[*it];
           v.push_back(note);
       }
    }   
}

void storyBoard::printSlip(Note *tm){
    cout << "Tital=" << tm->Tital <<endl 
        << "Text=" <<  tm->Text <<endl
        << "Tags = ";
    for (auto it = tm->vec.begin(); it != tm->vec.end(); ++it){
        cout<< *it<<"\t";
    }    
    cout<<endl<<endl;
}

void storyBoard::PrintStoredData(){
    for(auto tm : TitalMap){
        printSlip(tm.second);
    }
    cout<<endl; 
}

void feed_data_for_testing(storyBoard &Sb);
void TestCase(storyBoard &Sb);

int main() {
    storyBoard Sb;
    feed_data_for_testing(Sb);

    Sb.PrintStoredData(); /*Print all contain data */
    cout<<"************* From Here start searching ************"<<endl;
    TestCase(Sb);
    return 0;
}

void TestCase(storyBoard &Sb){    
    Note* obj = Sb.searchByTitle("Tital-3");
    if(obj != NULL){
        Sb.printSlip(obj);
    }

    obj = Sb.searchByText("Text-4");
    if(obj != NULL){
        Sb.printSlip(obj);
    }

    vector<Note *> vec = Sb.searchByTag("tag-3");
    if(vec.size() !=0){
        for (auto it = vec.begin(); it != vec.end(); ++it){
            //cout<<(*it)->Tital << "\t";
            Sb.printSlip(*it);
        } 
    }   
}

void feed_data_for_testing(storyBoard &Sb){
    vector<string> tags ;
    int count =INPUT;
    for(int i =1;i<=count;i++){
        string tital = "Tital-" + std::to_string(i);
        string text = "Text-" + std::to_string(i);
        tags.clear();
        for(int j =1;j<=i;j++){
            string tag_ = "tag-" +  std::to_string(j);
            tags.push_back(tag_);
        }
        Sb.AddNote(tital,text,tags);
    }
}

I am looking for a design pattern that solves this issue.

Aucun commentaire:

Enregistrer un commentaire