Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion keyvi/include/keyvi/dictionary/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ class Dictionary final {
TRACE("GetAllKeys callback called");

for (;;) {
if (!data->traverser.AtEnd()) {
if (data->traverser) {
data->traversal_stack.resize(data->traverser.GetDepth() - 1);
data->traversal_stack.push_back(data->traverser.GetStateLabel());
TRACE("Current depth %d (%d)", data->traverser.GetDepth() - 1, data->traversal_stack.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ class ComparableStateTraverser final {

operator bool() const { return state_traverser_; }

bool AtEnd() const { return state_traverser_.AtEnd(); }

void operator++(int) {
state_traverser_++;
if (state_traverser_) {
Expand Down
15 changes: 5 additions & 10 deletions keyvi/include/keyvi/dictionary/fsa/state_traverser.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class StateTraverser final {
using transition_t = TransitionT;

explicit StateTraverser(automata_t f)
: fsa_(f), current_state_(f->GetStartState()), current_weight_(0), current_label_(0), at_end_(false), stack_() {
: fsa_(f), current_state_(f->GetStartState()), current_weight_(0), current_label_(0), stack_() {
TRACE("StateTraverser starting with Start state %d", current_state_);
f->GetOutGoingTransitions(current_state_, &stack_.GetStates(), &stack_.traversal_stack_payload, 0);

Expand All @@ -57,7 +57,7 @@ class StateTraverser final {

StateTraverser(automata_t f, const uint64_t start_state, traversal::TraversalPayload<TransitionT> &&payload,
const bool advance = true)
: fsa_(f), current_weight_(0), current_label_(0), at_end_(false), stack_(std::move(payload)) {
: fsa_(f), current_weight_(0), current_label_(0), stack_(std::move(payload)) {
current_state_ = start_state;

TRACE("StateTraverser starting with Start state %d", current_state_);
Expand All @@ -70,7 +70,7 @@ class StateTraverser final {
}

StateTraverser(automata_t f, const uint64_t start_state, const bool advance = true)
: fsa_(f), current_state_(start_state), current_weight_(0), current_label_(0), at_end_(false), stack_() {
: fsa_(f), current_state_(start_state), current_weight_(0), current_label_(0), stack_() {
TRACE("StateTraverser starting with Start state %d", current_state_);
f->GetOutGoingTransitions(start_state, &stack_.GetStates(), &stack_.traversal_stack_payload,
f->GetInnerWeight(start_state));
Expand All @@ -89,13 +89,11 @@ class StateTraverser final {
current_state_(other.current_state_),
current_weight_(other.current_weight_),
current_label_(other.current_label_),
at_end_(other.at_end_),
stack_(std::move(other.stack_)) {
other.fsa_ = 0;
other.current_state_ = 0;
other.current_weight_ = 0;
other.current_label_ = 0;
other.at_end_ = true;
}

automata_t GetFsa() const { return fsa_; }
Expand Down Expand Up @@ -136,7 +134,7 @@ class StateTraverser final {
if (stack_.GetDepth() == 0) {
TRACE("traverser exhausted.");
current_label_ = 0;
at_end_ = true;
current_state_ = 0;
return;
}

Expand All @@ -157,7 +155,7 @@ class StateTraverser final {

label_t GetStateLabel() const { return current_label_; }

operator bool() const { return !at_end_; }
operator bool() const { return current_state_ != 0; }

/**
* Set the minimum weight states must be greater or equal to.
Expand All @@ -168,14 +166,11 @@ class StateTraverser final {
*/
inline void SetMinWeight(uint32_t min_weight) {}

bool AtEnd() const { return at_end_; }

private:
automata_t fsa_;
uint64_t current_state_;
uint32_t current_weight_;
label_t current_label_;
bool at_end_;
traversal::TraversalStack<TransitionT> stack_;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
//

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include <boost/test/unit_test.hpp>

Expand All @@ -34,14 +36,14 @@ BOOST_AUTO_TEST_SUITE(ComparableStateTraverserTests)

BOOST_AUTO_TEST_CASE(StateTraverserCompatibility) {
std::vector<std::string> test_data = {"aaaa", "aabb", "aabc", "aacd", "bbcd"};
testing::TempDictionary dictionary(&test_data);
automata_t f = dictionary.GetFsa();
const testing::TempDictionary dictionary(&test_data);
const automata_t f = dictionary.GetFsa();

ComparableStateTraverser<StateTraverser<>> s(f);

BOOST_CHECK_EQUAL('a', s.GetStateLabel());
BOOST_CHECK_EQUAL(1, s.GetDepth());
BOOST_CHECK(!s.AtEnd());
BOOST_CHECK(s);
BOOST_CHECK(s);

s++;
Expand Down Expand Up @@ -101,25 +103,25 @@ BOOST_AUTO_TEST_CASE(StateTraverserCompatibility) {
// traverser shall be exhausted
s++;
BOOST_CHECK_EQUAL(0, s.GetStateLabel());
BOOST_CHECK(s.AtEnd());
BOOST_CHECK(!s);
BOOST_CHECK(!s);
BOOST_CHECK_EQUAL(0, s.GetDepth());
s++;
BOOST_CHECK_EQUAL(0, s.GetStateLabel());
BOOST_CHECK(s.AtEnd());
BOOST_CHECK(!s);
BOOST_CHECK_EQUAL(0, s.GetDepth());
}

BOOST_AUTO_TEST_CASE(StateTraverserCompatSomeTraversalWithPrune) {
std::vector<std::string> test_data = {"aaaa", "aabb", "aabc", "aacd", "bbcd"};
testing::TempDictionary dictionary(&test_data);
automata_t f = dictionary.GetFsa();
const testing::TempDictionary dictionary(&test_data);
const automata_t f = dictionary.GetFsa();

ComparableStateTraverser<StateTraverser<>> s(f);

BOOST_CHECK_EQUAL('a', s.GetStateLabel());
BOOST_CHECK_EQUAL(1, s.GetDepth());
BOOST_CHECK(!s.AtEnd());
BOOST_CHECK(s);

s++;
BOOST_CHECK_EQUAL('a', s.GetStateLabel());
Expand All @@ -140,7 +142,7 @@ BOOST_AUTO_TEST_CASE(StateTraverserCompatSomeTraversalWithPrune) {
BOOST_CHECK_EQUAL(2, s.GetDepth());
BOOST_CHECK_EQUAL(2, s.GetStateLabels().size());
s++;
BOOST_CHECK(!s.AtEnd());
BOOST_CHECK(s);

BOOST_CHECK_EQUAL('c', s.GetStateLabel());
BOOST_CHECK_EQUAL(3, s.GetDepth());
Expand All @@ -165,65 +167,65 @@ BOOST_AUTO_TEST_CASE(StateTraverserCompatSomeTraversalWithPrune) {
// traverser shall be exhausted
s++;
BOOST_CHECK_EQUAL(0, s.GetStateLabel());
BOOST_CHECK(s.AtEnd());
BOOST_CHECK(!s);
BOOST_CHECK_EQUAL(0, s.GetDepth());
s++;
BOOST_CHECK_EQUAL(0, s.GetStateLabel());
BOOST_CHECK(s.AtEnd());
BOOST_CHECK(!s);
BOOST_CHECK_EQUAL(0, s.GetDepth());
}

BOOST_AUTO_TEST_CASE(StateTraverserCompatLongkeys) {
std::string a(1000, 'a');
std::string b(1000, 'b');
const std::string a(1000, 'a');
const std::string b(1000, 'b');

std::vector<std::string> test_data = {a, b};
testing::TempDictionary dictionary(&test_data);
automata_t f = dictionary.GetFsa();
const testing::TempDictionary dictionary(&test_data);
const automata_t f = dictionary.GetFsa();

ComparableStateTraverser<StateTraverser<>> s(f);

for (int i = 1; i <= 1000; ++i) {
BOOST_CHECK_EQUAL('a', s.GetStateLabel());
BOOST_CHECK_EQUAL(i, s.GetDepth());
s++;
BOOST_CHECK(!s.AtEnd());
BOOST_CHECK(s);
}

for (int i = 1; i <= 1000; ++i) {
BOOST_CHECK_EQUAL('b', s.GetStateLabel());
BOOST_CHECK_EQUAL(i, s.GetDepth());
s++;
if (i != 1000) {
BOOST_CHECK(!s.AtEnd());
BOOST_CHECK(s);
} else {
BOOST_CHECK(s.AtEnd());
BOOST_CHECK(!s);
}
}

// traverser shall be exhausted
s++;
BOOST_CHECK_EQUAL(0, s.GetStateLabel());
BOOST_CHECK(s.AtEnd());
BOOST_CHECK(!s);
BOOST_CHECK_EQUAL(0, s.GetDepth());
s++;
BOOST_CHECK_EQUAL(0, s.GetStateLabel());
BOOST_CHECK(s.AtEnd());
BOOST_CHECK(!s);

BOOST_CHECK_EQUAL(0, s.GetDepth());
}

BOOST_AUTO_TEST_CASE(StateTraverserCompatZeroByte) {
std::vector<std::string> test_data = {std::string("\0aaaa", 5), std::string("aa\0bb", 5), "aabc", "aacd",
std::string("bbcd\0", 5)};
testing::TempDictionary dictionary(&test_data);
automata_t f = dictionary.GetFsa();
const testing::TempDictionary dictionary(&test_data);
const automata_t f = dictionary.GetFsa();

ComparableStateTraverser<StateTraverser<>> s(f);

BOOST_CHECK_EQUAL('\0', s.GetStateLabel());
BOOST_CHECK_EQUAL(1, s.GetDepth());
BOOST_CHECK(!s.AtEnd());
BOOST_CHECK(s);

s++;
BOOST_CHECK_EQUAL('a', s.GetStateLabel());
Expand Down Expand Up @@ -311,23 +313,23 @@ BOOST_AUTO_TEST_CASE(StateTraverserCompatZeroByte) {
s++;
BOOST_CHECK_EQUAL(0, s.GetStateLabel());
BOOST_CHECK_EQUAL(0, s.GetDepth());
BOOST_CHECK(s.AtEnd());
BOOST_CHECK(!s);

s++;
BOOST_CHECK_EQUAL(0, s.GetStateLabel());
BOOST_CHECK_EQUAL(0, s.GetDepth());
BOOST_CHECK(s.AtEnd());
BOOST_CHECK(!s);
}

BOOST_AUTO_TEST_CASE(same_data) {
std::vector<std::string> test_data = {"aaaa", "aabb", "aabc", "aacd", "bbcd"};
testing::TempDictionary dictionary(&test_data);
automata_t f = dictionary.GetFsa();
const testing::TempDictionary dictionary(&test_data);
const automata_t f = dictionary.GetFsa();

ComparableStateTraverser<StateTraverser<>> t1(f, false, 0);
ComparableStateTraverser<StateTraverser<>> t2(f, false, 0);
ComparableStateTraverser<StateTraverser<>> t3(f, false, 1);
ComparableStateTraverser<StateTraverser<>> t4(f, false, 1);
const ComparableStateTraverser<StateTraverser<>> t4(f, false, 1);

BOOST_CHECK((t1 < t2) == false);
BOOST_CHECK((t2 < t1) == false);
Expand Down Expand Up @@ -371,12 +373,12 @@ BOOST_AUTO_TEST_CASE(same_data) {

BOOST_AUTO_TEST_CASE(interleave1) {
std::vector<std::string> test_data1 = {"aaaa", "aabb", "aabc", "aacd", "bbcd"};
testing::TempDictionary dictionary1(&test_data1);
automata_t f1 = dictionary1.GetFsa();
const testing::TempDictionary dictionary1(&test_data1);
const automata_t f1 = dictionary1.GetFsa();

std::vector<std::string> test_data2 = {"aabb", "aabd", "abcd", "bbcd"};
testing::TempDictionary dictionary2(&test_data2);
automata_t f2 = dictionary2.GetFsa();
const testing::TempDictionary dictionary2(&test_data2);
const automata_t f2 = dictionary2.GetFsa();

ComparableStateTraverser<StateTraverser<>> t1(f1, false, 0);
ComparableStateTraverser<StateTraverser<>> t2(f2, false, 1);
Expand Down Expand Up @@ -415,12 +417,12 @@ BOOST_AUTO_TEST_CASE(interleave1) {

BOOST_AUTO_TEST_CASE(interleave2) {
std::vector<std::string> test_data1 = {"aaaa", "aabb", "aabc", "aacd", "bbcd"};
testing::TempDictionary dictionary1(&test_data1);
automata_t f1 = dictionary1.GetFsa();
const testing::TempDictionary dictionary1(&test_data1);
const automata_t f1 = dictionary1.GetFsa();

std::vector<std::string> test_data2 = {"aaab", "aabd", "abcd", "bbcd"};
testing::TempDictionary dictionary2(&test_data2);
automata_t f2 = dictionary2.GetFsa();
const testing::TempDictionary dictionary2(&test_data2);
const automata_t f2 = dictionary2.GetFsa();

ComparableStateTraverser<StateTraverser<>> t1(f1, false, 0);
ComparableStateTraverser<StateTraverser<>> t2(f2, false, 1);
Expand Down Expand Up @@ -449,12 +451,12 @@ BOOST_AUTO_TEST_CASE(interleave2) {

BOOST_AUTO_TEST_CASE(nearTraversalSpecialization) {
std::vector<std::string> test_data1 = {"aaaa", "aabb", "aabc", "aacd", "bbcd", "cdefgh"};
testing::TempDictionary dictionary1(&test_data1);
automata_t f1 = dictionary1.GetFsa();
const testing::TempDictionary dictionary1(&test_data1);
const automata_t f1 = dictionary1.GetFsa();

std::vector<std::string> test_data2 = {"abbb", "aabc", "bbcd", "aaceh", "cdefgh"};
testing::TempDictionary dictionary2(&test_data2);
automata_t f2 = dictionary2.GetFsa();
const testing::TempDictionary dictionary2(&test_data2);
const automata_t f2 = dictionary2.GetFsa();

std::shared_ptr<std::string> near_key = std::make_shared<std::string>("aace");

Expand Down Expand Up @@ -537,12 +539,12 @@ BOOST_AUTO_TEST_CASE(nearTraversalSpecialization) {

BOOST_AUTO_TEST_CASE(nearTraversalSpecialization2) {
std::vector<std::string> test_data1 = {"aaaa", "aabb", "aabc", "aacd", "bbcd", "cdefgh"};
testing::TempDictionary dictionary1(&test_data1);
automata_t f1 = dictionary1.GetFsa();
const testing::TempDictionary dictionary1(&test_data1);
const automata_t f1 = dictionary1.GetFsa();

std::vector<std::string> test_data2 = {"abbb", "aabc", "bbcd", "aaceh", "aacexyz", "aacexz", "cdefgh"};
testing::TempDictionary dictionary2(&test_data2);
automata_t f2 = dictionary2.GetFsa();
const testing::TempDictionary dictionary2(&test_data2);
const automata_t f2 = dictionary2.GetFsa();

std::shared_ptr<std::string> near_key = std::make_shared<std::string>("aace");

Expand Down
Loading
Loading