Skip to content

Commit

Permalink
add inverted list implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
shpilu committed Dec 12, 2018
1 parent 786821b commit ad0ecc0
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/indexer/inverted_list.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// indexer main class definition
// version: 1.0
// Copyright (C) 2018 James Wei (weijianlhp@163.com). All rights reserved
//

#include "inverted_list.h"

namespace cloris {

bool DocidNode::operator < (const DocidNode& dn) const {
return ((this->docid < dn.docid) ||
((this->docid == dn.docid) && (!this->is_belong_to && dn.is_belong_to)));
}

bool DocidNode::operator == (const DocidNode& dn) const {
return (this->docid == dn.docid) && (this->is_belong_to == dn.is_belong_to);
}

bool DocidNode::operator != (const DocidNode& dn) const {
return (this->docid != dn.docid) || (this->is_belong_to != dn.is_belong_to);
}

void InvertedList::Add(bool is_belong_to, int docid) {
auto iter = doc_list_.begin();
for (; iter != doc_list_.end(); ++iter) {
if (iter->docid >= docid) {
break;
}
}
doc_list_.insert(iter, DocidNode(docid, is_belong_to));
}

} // namespace cloris


43 changes: 43 additions & 0 deletions src/indexer/inverted_list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// indexer main class definition
// version: 1.0
// Copyright (C) 2018 James Wei (weijianlhp@163.com). All rights reserved
//

#ifndef CLORIS_INVERTED_LIST_H_
#define CLORIS_INVERTED_LIST_H_

#include <unistd.h>
#include <list>

namespace cloris {

//
// we just treat docId AS int-type now
//
struct DocidNode {
DocidNode(int _docid, bool _is_belong_to) : docid(_docid), is_belong_to(_is_belong_to) {}
bool operator < (const DocidNode& dn) const;
bool operator == (const DocidNode& dn) const;
bool operator != (const DocidNode& dn) const;

int docid;
bool is_belong_to;
};

// TODO switch general list to skip list
class InvertedList {
public:
InvertedList() {}
~InvertedList() {}
void Add(bool is_belong_to, int docid);
const std::list<DocidNode>& doc_list() { return doc_list_; }
size_t length() const { return doc_list_.size(); }
private:
std::list<DocidNode> doc_list_;
// skip_list<DocidNode> list;
};

} // namespace cloris

#endif // CLORIS_INVERTED_LIST_H_

0 comments on commit ad0ecc0

Please sign in to comment.