Exemplo n.º 1
0
void FindMinGreaterThen() {
    auto arr = std::vector<int> {
        //  0   1   2   3   4   5   6   7   8   9  10  11
        6, 20, 64, 61, 21, 63, 66, 69, 67, 75, 72,  5,
    };
    int pivot = 6;
    int rightAnswer = 20;
    int answer = *NP::FindMinGreaterThen(arr.begin(), arr.begin() + 4, pivot);
    if (answer != rightAnswer) {
        throw TSimpleException("[FindMinGreaterThen]")
                << " Wrong answer: " << answer
                << " Expected: " << rightAnswer
                ;
    }
    pivot = 62;
    rightAnswer = 63;
    answer = *NP::FindMinGreaterThen(arr.begin(), arr.begin() + 6, pivot);
    if (answer != rightAnswer) {
        throw TSimpleException("[FindMinGreaterThen]")
                << " Wrong answer: " << answer
                << " Expected: " << rightAnswer
                ;
    }
    pivot = 67;
    rightAnswer = 69;
    answer = *NP::FindMinGreaterThen(arr.begin(), arr.end(), pivot);
    if (answer != rightAnswer) {
        throw TSimpleException("[FindMinGreaterThen]")
                << " Wrong answer: " << answer
                << " Expected: " << rightAnswer
                ;
    }
}
Exemplo n.º 2
0
void
fuzzySearch(
    std::string text,
    std::string pattern,
    std::unordered_set<size_t> rightStarts,
    std::unordered_set<std::string> rightStrings
) {
    TFuzzySearch searcher(text, '?');
    std::vector<TFuzzySearch::TSubstring> rez = searcher.search(pattern);

    std::unordered_set<size_t> starts;
    std::unordered_set<std::string> strs;
    for (const auto& r : rez) {
        starts.insert(r.start);
        strs.insert(r.copy());
    }
    if (strs != rightStrings) {
        throw TSimpleException("wrong string answer")
            << " Wrong answer: " << strs
            << " Expected: " << rightStrings;
    }
    if (starts != rightStarts) {
        throw TSimpleException("wrong position answer")
            << " Wrong answer: " << starts
            << " Expected: " << rightStarts;
    }
}
Exemplo n.º 3
0
TEdge* TEdge::split(size_t cursor) {
    if (!subString.positionIsValid(cursor)) {
        throw TSimpleException()
            << "invalid position for split"
            << "; start: " << subString.start
            << "; cur: " << cursor
            << "; end: " << subString.end
        ;
    }

    TSubstring downPart(subString);
    downPart.start = downPart.start + cursor;
    subString.end = subString.start + cursor;

    /*dbg*/ std::cerr << "split through cur: " << cursor << std::endl;
    /*dbg*/ std::cerr << "up interval: "
    /*dbg*/     << "; start: " << subString.start
    /*dbg*/     << "; end: " << subString.end
    /*dbg*/     << std::endl
    /*dbg*/ ;
    /*dbg*/ std::cerr << "down interval: "
    /*dbg*/     << "; start: " << downPart.start
    /*dbg*/     << "; end: " << downPart.end
    /*dbg*/     << std::endl
    /*dbg*/ ;

    std::unique_ptr<TNode> newNode(new TNode(this));
    newNode->addEdge(downPart);
    newNode->get(downPart.head())->endNode.swap(endNode);
    endNode.swap(newNode);
    return endNode->get(downPart.head());
}
Exemplo n.º 4
0
void NextInOrder() {
    auto arr = std::vector<int> {0, 1, 2, 3};
    auto rightAnswer = std::vector<int> {0, 1, 3, 2};
    if (!NP::NextInOrder(arr)) {
        throw TSimpleException("[NextInOrder]")
                << " Is not last permutation";
    }
    if (rightAnswer != arr) {
        throw TSimpleException("[NextInOrder]")
                << " Wrong answer: " << arr
                << " Expected: " << rightAnswer;
    }

    rightAnswer = std::vector<int> {0, 2, 1, 3};
    if (!NP::NextInOrder(arr)) {
        throw TSimpleException("[NextInOrder]")
                << " Is not last permutation";
    }
    if (rightAnswer != arr) {
        throw TSimpleException("[NextInOrder]")
                << " Wrong answer: " << arr
                << " Expected: " << rightAnswer;
    }

    rightAnswer = std::vector<int> {0, 2, 3, 1};
    if (!NP::NextInOrder(arr)) {
        throw TSimpleException("[NextInOrder]")
                << " Is not last permutation";
    }
    if (rightAnswer != arr) {
        throw TSimpleException("[NextInOrder]")
                << " Wrong answer: " << arr
                << " Expected: " << rightAnswer;
    }

    rightAnswer = std::vector<int> {0, 3, 1, 2};
    if (!NP::NextInOrder(arr)) {
        throw TSimpleException("[NextInOrder]")
                << " Is not last permutation";
    }
    if (rightAnswer != arr) {
        throw TSimpleException("[NextInOrder]")
                << " Wrong answer: " << arr
                << " Expected: " << rightAnswer;
    }

    arr = std::vector<int> {3, 2, 1, 0};
    if (NP::NextInOrder(arr)) {
        throw TSimpleException("[NextInOrder]")
                << "Permutation have to be last";
    }
}
Exemplo n.º 5
0
void Test6() {
    auto answ = BraceExpansion("");
    if (answ.size() != 1 || !answ[0].empty()) {
        throw TSimpleException()
            << "Wrong answer: " << answ
            << ", expected: \"\"";
    }
}
Exemplo n.º 6
0
void Test5() {
    auto answ = BraceExpansion("{0..999}abcdef{0..99}");
    if (100000 != answ.size()) {
        throw TSimpleException()
            << "Wrong answer: " << answ.size()
            << ", expected: " << 100000;
    }
}
Exemplo n.º 7
0
void Test3() {
    auto rightAnswer = std::vector<std::string>{
        "-2abc",
        "-1abc",
        "0abc",
    };
    auto answ = BraceExpansion("{-2..0}abc");
    if (rightAnswer != answ) {
        throw TSimpleException()
            << "Wrong answer: " << answ
            << ", expected: " << rightAnswer;
    }
}
Exemplo n.º 8
0
void Test0() {
    auto rightAnswer = std::vector<std::string>{
        "abc1",
        "abc2",
        "abc3",
    };
    auto answ = BraceExpansion("abc{1..3}");
    if (rightAnswer != answ) {
        throw TSimpleException()
            << "Wrong answer: " << answ
            << ", expected: " << rightAnswer;
    }
}
Exemplo n.º 9
0
void Test2() {
    auto rightAnswer = std::vector<std::string>{
        "a-1abc1c",
        "a-1abc2c",
        "a0abc1c",
        "a0abc2c",
        "a1abc1c",
        "a1abc2c",
    };
    auto answ = BraceExpansion("a{-1..1}abc{1..2}c");
    if (rightAnswer != answ) {
        throw TSimpleException()
            << "Wrong answer: " << answ
            << ", expected: " << rightAnswer;
    }
}
Exemplo n.º 10
0
void
aazzTest() {
    std::string str = "aazz";
    std::string rightAnswer =
        "[0:0] \n"
        "  ---> \n"
        "      -(s): [5:12] ssippi`\n"
    ;
    TSuffixTreeBase stree(str);
    std::stringstream out;
    stree.show(out);
    std::string answer = out.str();
    if (rightAnswer != answer) {
        throw TSimpleException("[tree test]")
            << " Wrong answer: " << answer
            << " Expected: " << rightAnswer;
    }
}
Exemplo n.º 11
0
void
mississippiTest() {
    std::string str = "mississippi";
    std::string rightAnswer =
        "[0:0] \n"
        "  ---> \n"
        "  -(`): [11:12] `\n"
        "  -(i): [1:2] i\n"
        "    ---> \n"
        "    -(`): [11:12] `\n"
        "    -(p): [8:12] ppi`\n"
        "    -(s): [2:5] ssi\n"
        "      ---> si\n"
        "      -(p): [8:12] ppi`\n"
        "      -(s): [5:12] ssippi`\n"
        "  -(m): [0:12] mississippi`\n"
        "  -(p): [8:9] p\n"
        "    ---> \n"
        "    -(i): [10:12] i`\n"
        "    -(p): [9:12] pi`\n"
        "  -(s): [2:3] s\n"
        "    ---> \n"
        "    -(i): [4:5] i\n"
        "      ---> i\n"
        "      -(p): [8:12] ppi`\n"
        "      -(s): [5:12] ssippi`\n"
        "    -(s): [3:5] si\n"
        "      ---> i\n"
        "      -(p): [8:12] ppi`\n"
        "      -(s): [5:12] ssippi`\n"
    ;
    TSuffixTreeBase stree(str);
    std::stringstream out;
    stree.show(out);
    std::string answer = out.str();
    if (rightAnswer != answer) {
        throw TSimpleException("[mississippi test]")
            << " Wrong answer: " << answer
            << " Expected: " << rightAnswer;
    }
}
Exemplo n.º 12
0
void
TTreeBase::ukkonenPush(
    std::deque<TUkkonenBuildCursor>& cursors,
    size_t position
) {
    cursors.emplace_back(TUkkonenBuildCursor(&rootEdge));
    TNode* nodeForLink = nullptr;

    while (cursors.front().deleted) {
        cursors.pop_front();
    }
    //*dbg*/ size_t counter = 0;
    for (auto& cursor : cursors) {
        if (cursor.deleted) {
            //*dbg*/ std::cerr << counter++ << ": skip deleted cursor\n";
            /*dbg*/ throw TSimpleException("impossible situation");
            continue;
        }
        //*dbg*/ else {
        //*dbg*/     std::cerr << counter++ << ": cursor: \n";
        //*dbg*/ }
        /*dbg*/ std::cerr << "step " << cursor.edge->subString.str << std::endl;
        /*dbg*/ std::cerr << "step edge ptr " << cursor.edge << std::endl;
        int stepType = cursor.step(position);
        /*dbg*/ std::cerr << "step: " << stepType << "\n\n";

        if (stepType > 1) {
            TNode* currentNode = cursor.edge->parentNode;
            if (nodeForLink != nullptr && nodeForLink->suffixLink == nullptr) {
                //*dbg*/ std::cerr << "link\n";
                nodeForLink->suffixLink = currentNode;
            }
            nodeForLink = currentNode;
        }
        if (stepType == 2) {
            cursor.deleted = true;
        }
    }
}