XHeap.cpp 4.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/* NiuTrans.Tensor - an open-source tensor library
 * Copyright (C) 2017, Natural Language Processing Lab, Northestern University. 
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 *
 * As it is, this is a heap.
 *
 * $Created by: XIAO Tong (xiaotong@mail.neu.edu.cn) 2017-12-20
 *
 */

#include "XGlobal.h"
#include "XHeap.h"

/* the nts (NiuTrans.Tensor) namespace */
namespace nts{

/* constructor */
template<HeapType hType, typename T>
34 35 36 37 38 39
XHeap<hType, T>::XHeap()
{
}

/* constructor */
template<HeapType hType, typename T>
40 41
XHeap<hType, T>::XHeap(int mySize, XMem * myMem)
{
42
    Init(mySize, myMem);
43 44 45 46 47 48 49 50 51 52
}

/* deconstructor */
template<HeapType hType, typename T>
XHeap<hType, T>::~XHeap()
{
    delete[] items;
}

template<HeapType hType, typename T>
53 54 55 56 57 58 59 60 61 62 63 64 65
void XHeap<hType, T>::Init(int mySize, XMem * myMem)
{
    mem = myMem;
    size = mySize;
    count = 0;

    if (mem == NULL)
        items = new HeapNode<T>[mySize];
    else
        mem->Alloc(mem->devID, mySize * sizeof(T));
}

template<HeapType hType, typename T>
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
void XHeap<hType, T>::Clear(T initValue)
{
    count = 0;
    for (int i = 0; i < size; i++) {
        items[i].index = 0;
        items[i].value = initValue;
    }
}

/* compare node i and node j */
template<HeapType hType, typename T>
_XINLINE_ bool XHeap<hType, T>::Compare(int i, int j)
{
    if (hType == MIN_HEAP)
        return items[i].value < items[j].value;
    else
        return items[j].value < items[i].value;
}

/* top most item */
template<HeapType hType, typename T>
_XINLINE_ HeapNode<T> XHeap<hType, T>::Top()
{
    HeapNode<T> node = items[0];
    return node;
}

/* last item */
template<HeapType hType, typename T>
_XINLINE_ HeapNode<T> XHeap<hType, T>::End()
{
    HeapNode<T> node = items[count - 1];
    return node;
}

/* push an item into the heap */
template<HeapType hType, typename T>
_XINLINE_ void XHeap<hType, T>::Push(HeapNode<T> node)
{
xiaotong committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    if (count < size) {
        items[count] = node;
        Up(count);
        count++;
    }
    else if(count == size){
        HeapNode<T> & item0 = items[0];
        if (hType == MIN_HEAP && item0.value >= node.value)
            return;
        else if (hType == MAX_HEAP && item0.value <= node.value)
            return;
        items[0] = node;
        Down(0);
    }
    else {
        ShowNTErrors("Overflow of the heap!");
    }
    
123 124 125 126 127 128 129 130 131 132 133 134 135 136
}

/* replace the top-most item and update the heap */
template<HeapType hType, typename T>
_XINLINE_ void XHeap<hType, T>::ReplaceTop(HeapNode<T> node)
{
    items[0] = node;
    Down(0);
}

/* pop the top most item */
template<HeapType hType, typename T>
_XINLINE_ HeapNode<T> XHeap<hType, T>::Pop()
{
xiaotong committed
137
    CheckNTErrors(count > 0, "Empty heap!");
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    HeapNode<T> node = items[0];
    items[0] = items[count - 1];
    count--;
    items[count].index = 0;
    items[count].value = 0;
    Down(0);
    return node;
}

/* move item k down the tree */
template<HeapType hType, typename T>
_XINLINE_ void XHeap<hType, T>::Down(int k)
{
    int i = k;
    while (2 * i + 1 < count) {
        int l = 2 * i + 1, r = 2 * i + 2;
        int m = (r >= count || Compare(l, r)) ? l : r;
        if (Compare(i, m))
            break;
        HeapNode<T> tmp = items[i];
        items[i] = items[m];
        items[m] = tmp;
        i = m;
    }
}

/* move item k up the tree */
template<HeapType hType, typename T>
_XINLINE_ void XHeap<hType, T>::Up(int k)
{
    int i = k;
    int parent = (i - 1) / 2;
    while (i > 0 && !Compare(parent, i)) {
        HeapNode<T> tmp = items[i];
        items[i] = items[parent];
        items[parent] = tmp;
        i = parent;
        parent = (i - 1) / 2;
    }
}

/* explicit instantiation */
template class XHeap<MAX_HEAP, float>;
template class XHeap<MAX_HEAP, double>;
template class XHeap<MAX_HEAP, int>;
template class XHeap<MIN_HEAP, float>;
template class XHeap<MIN_HEAP, double>;
template class XHeap<MIN_HEAP, int>;

} /* end of the nts (NiuTrans.Tensor) namespace */