/*
 * 
 *@DESC : Copying trie is a recursive function, checking if it is NULL do nothing, 
 *        if not create a new node and assign it to original (priority and value) and then recursive call to all the prev node and next node in the trie.
 * 
 *@Input : Original Node.
 * 
 *@Output: Returning new node.
 * 
 */
Node<char>* Trie::copyTrie(Node<char> *original)
{
    if(original == NULL)
        return NULL;

    Node<char> *node = new Node<char>();
    node->value = original->value;
    node->priority = original->priority;

    node->prev = copyTrie(original->prev);
    delete original->prev;
    node->next = copyTrie(original->next);
    delete original->next;
    return node;
}
/*
 * 
 *@DESC : Operator Overloading, overloads = operator, It calls copyTrie.
 * 
 *@Input : cnode
 * 
 *@Output: returning trie pointer.
 * 
 */
Trie& Trie::operator=(const Trie& cnode)
{
    if (this != &cnode)
    {
        Node<char> *temp = copyTrie(cnode.getRoot());
        delete cnode.root;
        this->root = temp;
    }
    return *this;
}
예제 #3
0
    void align_template(Iterator begin, Iterator end,
                        int gap, score_matrix const & matrix,
                        Callable & callfunc)
    {
        typedef std::pair<std::string, simple_matrix2i> node_cache_type;

        std::string query(begin, end);
        alicont ali(query, gap, matrix);
        trie<node_cache_type>* tmp;
        copyTrie(&tmp);

        std::string target;
        std::stack<size_t> index_stack;

        for (trie<node_cache_type>::iterator i = tmp->begin() + 1;
                                             i != tmp->end();
                                             ++i)
        {
            target.push_back(i.symbol());
            if (!index_stack.empty() && i.prev().fork() &&
                    i.prev().index() != index_stack.top())
            {
                while (!index_stack.empty() && index_stack.top() != i.prev().index())
                {
                    *(tmp->iter(index_stack.top())) = node_cache_type();
                    index_stack.pop();
                    ali.pop();
                }
            }
            if (i.leaf())
            {
                *i = std::move(std::make_pair(target, ali.score(target)));
                ali.push(i->first, &(i->second));
                index_stack.push(i.index());
                target.clear();
                alignment_result res = ali.alignment();
                res.target_id = iter(i.index())->back().record;
                if (!callfunc(std::move(res)))
                {
                    break;
                }
            }
            else if (i.fork())
            {
                *i = std::move(std::make_pair(target, ali.score(target)));
                ali.push(i->first, &(i->second));
                index_stack.push(i.index());
                target.clear();
            }
        }

        delete tmp;
    }
예제 #4
0
    std::vector<index_type> find(Iterator begin, Iterator end)
    {
        typedef typename trie<bool>::iterator tb_iterator;

        std::vector<index_type> result;
        trie<bool>* tmp = nullptr;
        copyTrie(&tmp);

        size_t deep = 0;
        const std::set<index_type>* nodes = nullptr;
        for (Iterator iter = begin; iter != end; ++iter)
        {
            nodes = m_stat.get(iter, end);
            if (nodes == nullptr)
            {
                nodes = m_stat.get(iter-1, end);
                break;
            }
            for (auto niter = nodes->begin(); niter != nodes->end(); ++niter)
            {
                tb_iterator t(tmp, *niter);
                *t = true;
            }
            deep++;
        }

        if (nodes == nullptr)
        {
            return result;
        }

        for (auto niter = nodes->begin(); niter != nodes->end(); ++niter)
        {
            bool breakflag = false;
            tb_iterator t(tmp, *niter);
            for (size_t i = deep-1; i != 0; --i)
            {
                --t;
                if (t == tmp->end() || !*t)
                {
                    breakflag = true;
                    break;
                }
            }
            if (!breakflag)
            {
                result.push_back(t.index());
            }
        }

        delete tmp;
        return result;
    }