Skip to content

Commit

Permalink
Implemented binary search
Browse files Browse the repository at this point in the history
  • Loading branch information
Wang Xiaojian committed Jun 17, 2014
1 parent 4e65413 commit 419bebc
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
31 changes: 23 additions & 8 deletions simple_ir/Dic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,46 @@
#include<cstring>
#include<iostream>
#include <string>
#include <algorithm>

using namespace std;

Dic::Dic()
{
this->lists = vector<List>();
sorted = false;
}

bool compareLists(List list1, List list2)
{
return (list1.getTerm() < list2.getTerm());
}

List* Dic::getListByTerm(string term)
{
// Should only allow this operation when we have the list sorted by term.
// In that case we can do a binary search.
// Currently we do a linear search.
for(vector<List>::iterator list = lists.begin(); list != lists.end(); list++)
if(!sorted)
{
if(list->getTerm() == term)
{
return &(*list);
}
cout << "Lists not sorted yet!" << endl;
return NULL;
}
List searchFor(term);
// Binary search
auto results = equal_range(lists.begin(), lists.end(), searchFor, compareLists);
if(results.first != lists.end())
return &(*results.first);
return NULL;
}

void Dic::addList(string term)
{
List list(term);
this->lists.push_back(list);
sorted = false;
}


void Dic::sortLists()
{
sort(lists.begin(), lists.end(), compareLists);
sorted = true;
}
2 changes: 1 addition & 1 deletion simple_ir/List.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ List::List(string term)
this->term = term;
}

string List::getTerm()
string List::getTerm() const
{
return term;
}
Expand Down
1 change: 1 addition & 0 deletions simple_ir/Posting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Posting::Posting(string docId, int fq)
this->fq = fq;
next=NULL;
}

int Posting::freq()
{
return fq;
Expand Down
2 changes: 1 addition & 1 deletion simple_ir/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using namespace std;

int main(int argc, char** argv)
{
testList();
testDic();

//TokenReader::readAndLowerTokensFromFile("../Reuters/10.html");

Expand Down
6 changes: 6 additions & 0 deletions simple_ir/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ using namespace std;
void testDic()
{
Dic* dic = new Dic();
dic->sortLists();
List* list = dic->getListByTerm("a");
if(list != NULL)
cout << "Error!" << endl;
dic->addList("a");
dic->sortLists();
list = dic->getListByTerm("a");
if(list->getTerm() != "a")
cout << "Error!" << endl;
dic->addList("b");
dic->addList("c");
dic->sortLists();
list = dic->getListByTerm("a");
if(list->getTerm() != "a")
cout << "Error!" << endl;
Expand All @@ -35,6 +38,7 @@ void testDic()
void testList()
{
List *list = new List("a");

if(list->getTerm() != "a")
cout << "Error!" << endl;
if(list->getLength() != 0)
Expand All @@ -54,5 +58,7 @@ void testList()
posting = posting->next;
if(posting != NULL)
cout << "Error!" << endl;
if(list->getLength() != 3)
cout << "Error!" << endl;
cout << "If no error, then success!" << endl;
}

0 comments on commit 419bebc

Please sign in to comment.