Beispiel #1
0
 void insert(int d) {
   if( d <= data) {
     if (left != NULL) left->insert(d);
     else left = new RankNode(d);
     left_size++;
   } else {
     if (right != NULL) right->insert(d);
     else right = new RankNode(d);
   }
 }
Beispiel #2
0
 int getRank(int d) {
   if (d == data) {
     return left_size;
   } else if (d < data) {
     if (left == NULL) return -1;
     else return left->getRank(d);
   } else {
     int right_rank = (right == NULL ? -1 : right->getRank(d));
     if (right_rank == -1) return -1;
     else return left_size + 1 + right_rank;
   }
 }
 int getRank(int d) {
     if (d == data) {
         return left_size;
     } else if (d < data) {
         if (left == nullptr) {
             return -1;
         } else {
             return left->getRank(d);
         }
     } else {
         int right_rank = right == nullptr ? -1 : right->getRank(d);
         if (right_rank == -1) return -1;
         else return left_size + 1 + right_rank;
     }
 }
Beispiel #4
0
 void track(int number) {
   if (root == NULL) {
     root = new RankNode(number);
   } else {
     root->insert(number);
   }
 }
 void insert(int d) {
     if (d <= data) {
         if (left != nullptr) {
             left->insert(d);
         } else {
             left = new RankNode(d);
             left_size++;
         }
     } else {
         if (right != nullptr) {
             right->insert(d);
         } else {
             right = new RankNode(d);
         }
     }
 }
Beispiel #6
0
 int getRankOfNumber(int number) {
   return root->getRank(number);
 }
Beispiel #7
0
int RankBlock::set_node(const RankNode& node)
{
    if (m_rank_mgr == nullptr)
    {
        // shouldn't be here. 
        cout << "m_rank_mgr not set." << endl;
        return -1;
    }

    int offset = 0;
    if (m_node_list.size() == 0)
    {
        // first item
        m_node_list.push_front(node);

        m_keys.insert(node.data().id);
        m_value_range.first = m_value_range.second = node.data().value; 
        m_start_index = 1;

        return 0;
    }
    else {
        if (m_node_list.size() >= BlockExtentSize)
        {
            // shouldn't be here. 
            cout << "Block size error." << endl;
            return -1;
        }
        else
        {
            // find pos, and insert
            if (node.data().value < (*m_node_list.rbegin()).data().value)
            {
                m_node_list.push_back(node);
                m_value_range.second = node.data().value;
                offset = m_node_list.size() - 1;
                m_keys.insert(node.data().id);
            }
            else if (node.data().value > (*m_node_list.begin()).data().value)
            {
                m_node_list.push_front(node);
                m_value_range.first = node.data().value;
                offset = 0;
                m_keys.insert(node.data().id);
            }
            else
            {
                assert(m_node_list.size() >= 2);
                NodeList::iterator itor = m_node_list.begin();
                
                bool  ok = false;
                ValueType  tmp_value = node.data().value;
                //cout << "value: " << tmp_value << endl;
                for (; itor != m_node_list.end(); ++itor)
                {
                    ++offset;
                    if (tmp_value >= (*itor).data().value)
                    {
                        // insert before next
                        //cout << "block size1:" << m_node_list.size() << endl;
                        m_node_list.insert(itor, node);
                        //cout << "block size2:" << m_node_list.size() << endl;
                        m_keys.insert(node.data().id);
                        ok = true;
                        break;
                    }
                }
                if (!ok)
                {
                    cout << "something wrong happened." << endl;
                }
            }   
        }
    }

    if (m_node_list.size() == BlockExtentSize)
    {
        m_rank_mgr->fission(this);
    }

    //cout << "block size:" << m_node_list.size() << endl;

    return 0;
}