void push_back(T incoming_node_id)
    {
        std::uint64_t node_id = static_cast<std::uint64_t>(incoming_node_id);

        // mask incoming values, just in case they are > bitsize
        const std::uint64_t incoming_mask = static_cast<std::uint64_t>(pow(2, BITSIZE)) - 1;
        node_id = node_id & incoming_mask;

        const std::size_t available = (PACKSIZE - BITSIZE * num_elements) % ELEMSIZE;

        if (available == 0)
        {
            // insert ID at the left side of this element
            std::uint64_t at_left = node_id << (ELEMSIZE - BITSIZE);

            add_last_elem(at_left);
        }
        else if (available >= BITSIZE)
        {
            // insert ID somewhere in the middle of this element; ID can be contained
            // entirely within one element
            const std::uint64_t shifted = node_id << (available - BITSIZE);

            replace_last_elem(vec_back() | shifted);
        }
        else
        {
            // ID will be split between the end of this element and the beginning
            // of the next element
            const std::uint64_t left = node_id >> (BITSIZE - available);

            std::uint64_t right = node_id << (ELEMSIZE - (BITSIZE - available));

            replace_last_elem(vec_back() | left);
            add_last_elem(right);
        }

        num_elements++;
    }
Exemple #2
0
void DivideAndConquerSort(std::vector<int>& sort_vec)
{
    if (sort_vec.size() <= 1) {
        return;
    }
    unsigned long idx_mid = sort_vec.size() / 2;
    //divide1
    std::vector<int> vec_front(sort_vec.begin(), sort_vec.begin() + idx_mid);
    DivideAndConquerSort(vec_front);
    //divide2
    std::vector<int> vec_back(sort_vec.begin() + idx_mid, sort_vec.end());
    DivideAndConquerSort(vec_back);
    //conquer
    std::vector<int>::iterator iter_front = vec_front.begin();
    std::vector<int>::iterator iter_back  = vec_back.begin();
    for (std::vector<int>::iterator iter = sort_vec.begin(); iter < sort_vec.end(); iter++) {
        if ((iter_front != vec_front.end()) && (iter_back != vec_back.end())) {
            if (*iter_front <= *iter_back) {
                *iter = *iter_front;
                iter_front++;
            }
            else{
                *iter = *iter_back;
                iter_back++;
            }
            continue;
        }
        if (iter_front == vec_front.end() && iter_back != vec_back.end()) {
            *iter = *iter_back;
            iter_back++;
            continue;
        }
        if (iter_back == vec_back.end() && iter_front != vec_front.end()) {
            *iter = *iter_front;
            iter_front++;
            continue;
        }
    }
}