You need to sign in or sign up before continuing.
XList.cpp 7.9 KB
Newer Older
xiaotong committed
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 34 35 36 37 38 39 40 41 42 43 44
/* 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.
 */

/*
 *
 * Implementation of list that keeps data items
 *
 * $Created by: XIAO Tong (xiaotong@mail.neu.edu.cn) 2018-04-17
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "XList.h"
#include "XGlobal.h"

#include "wchar.h"
#include "locale.h"
#if !defined( WIN32 ) && !defined( _WIN32 )
    #include "sys/time.h"
    #include "time.h"
    #include "iconv.h"
#else
    #include "time.h"
#endif

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

45 46
XList NULLList;

xiaotong committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 105 106 107 108 109 110 111 112 113 114 115
/* constructor */
XList::XList()
{
    mem    = NULL;
    maxNum = 0;
    count  = 0;
    items  = NULL;
    isIntList = false;
}

/* 
constructor 
>> myMaxNum - maximum number of items to keep
>> isIntListOrNot - specify if the list keeps int items
*/
XList::XList(int myMaxNum, bool isIntListOrNot)
{
    mem    = NULL;
    maxNum = myMaxNum;
    count  = 0;
    items  = new void*[myMaxNum];
    isIntList = isIntListOrNot;
}

/* 
constructor 
>> myMaxNum - maximum number of items to keep
>> myMem - the memory pool used for data allocation
>> isIntListOrNot - specify if the list keeps int items
*/
XList::XList(int myMaxNum, XMem * myMem, bool isIntListOrNot)
{
    mem    = myMem;
    maxNum = myMaxNum;
    count  = 0;
    items  = (void**)mem->Alloc(mem->devID, sizeof(void*) * maxNum);
    isIntList = isIntListOrNot;
}

/* de-constructor */
XList::~XList()
{
    if(isIntList){
        for(int i = 0; i < count; i++){
            int * p = (int*)items[i];
            delete[] p;
        }
    }
    if(mem == NULL)
        delete[] items;
}

/* 
allocate the data array for the list
>> myMaxNum - maximum number of items to keep
>> isIntListOrNot - specify if the list keeps int items
*/
void XList::Create(int myMaxNum, XMem * myMem)
{
    mem    = myMem;
    maxNum = myMaxNum;
    count  = 0;
    items  = (void**)mem->Alloc(mem->devID, sizeof(void*) * maxNum);
}

/*
add an item into the list
>> item - pointer to the item
*/
116
void XList::Add(const void * item)
xiaotong committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130
{
    if( count == maxNum ){
        void ** newItems;
        if( mem == NULL )
            newItems = new void*[maxNum * 2 + 1];
        else
            newItems = (void**)mem->Alloc(mem->devID, sizeof(void*) * (maxNum * 2 + 1));
        memcpy(newItems, items, sizeof(void*) * maxNum);
        if( mem == NULL )
            delete[] items;
        items = newItems;
        maxNum = maxNum * 2 + 1;
    }
    
131 132
    MTYPE p = (MTYPE)item;
    items[count++] = (MTYPE*)p;
xiaotong committed
133 134 135 136 137 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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

}

/* 
add a number of items into the list 
>> inputItems - pointer to the array of items
>> inputItemCount - number of input items
*/
void XList::Add(void ** inputItems, int inputItemCount)
{
    if( count + inputItemCount >= maxNum ){
        int newMaxNum = (count + inputItemCount) * 2 + 1;
        void ** newItems;
        if( mem == NULL )
            newItems = new void*[newMaxNum];
        else
            newItems = (void**)mem->Alloc(mem->devID, sizeof(void*) * newMaxNum);
        memcpy(newItems, items, sizeof(void*) * maxNum);
        if( mem == NULL )
            delete[] items;
        items = newItems;
        maxNum = newMaxNum;
    }
    memcpy(items + count, inputItems, sizeof(void*) * inputItemCount);
    count += inputItemCount;
}

/*
append a list to the current list
>> l - the list we use to append
*/
void XList::AddList(XList * l)
{
    Add(l->items, l->count);
}

/*
add an integer-typed item into the list
>> item - pointer to the item
*/
void XList::AddInt(int i)
{
    CheckNTErrors(isIntList, "An int list is required!");

    int * a = new int[1];
    *a = i;
    Add(a);
}

/*
insert an item to the given position of the list
>> pos - the position
>> item - the item for insertion
*/
void XList::Insert(int pos, void * item)
{
    if( count == maxNum ){
        void ** newItems;
        if( mem == NULL )
            newItems = new void*[maxNum * 2 + 1];
        else
            newItems = (void**)mem->Alloc(mem->devID, sizeof(void*) * (maxNum * 2 + 1));
        memcpy(newItems, items, sizeof(void*) * maxNum);
        if( mem == NULL )
            delete[] items;
        items = newItems;
        maxNum = maxNum * 2 + 1;
    }

    for(int i = count - 1; i >= pos; i--)
        items[i + 1] = items[i];
    items[pos] = item;
    count++;
}

/* get the item at position i */
209
void * XList::GetItem(int i) const
xiaotong committed
210
{
xiaotong committed
211 212
    CheckNTErrors(i >= 0 && i < count, "Index of a list item is out of scope!");
    return items[i];
xiaotong committed
213 214 215 216 217 218
}

/* get the integer-typed item at position i */
int XList::GetItemInt(int i)
{
    CheckNTErrors(isIntList, "An int list is required!");
xiaotong committed
219 220
    CheckNTErrors(i >= 0 && i < count, "Index of a list item is out of scope!");
    return *(int*)(items[i]);
xiaotong committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
}

/* set the item at position i */
void XList::SetItem(int i, void * item)
{
     if( i >= 0 && i < count )
        items[i] = item;
}

/* get the integer-typed item at position i */
void XList::SetItemInt(int i, int item)
{
    CheckNTErrors(isIntList, "An int list is required!");

    if( i >= 0 && i < count )
        *(int*)(items[i]) = item;
}

/* 
find the position of the first matched item 
>> item - the item for matching
<< the position where we hit the item (if any)
*/

int XList::FindFirst(void * item)
{
    for(int i = 0;i < count; i++){
        if(item == items[i])
            return i;
    }
    return -1;
}

/* clear the data array */
void XList::Clear()
{
    if(isIntList){
        for(int i = 0; i < count; i++){
            delete[] (int*)items[i];
        }
        count = 0;
    }
    else
        count = 0;
}

/* delete the data array as well as the string arrays kept in it */
void XList::ClearStringList()
{
    if(mem == NULL){
        for(int i = 0; i < count; i++){
            delete[] (char*)items[i];
        }
    }
    count = 0;
}

/* 
sort the list 
>> itemSize - size of an item
>> comp - the comparison function used in sorting
*/
void XList::Sort(int itemSize, ListCompare comp)
{
    qsort(items, count, itemSize, comp);
}

/* reverse the list */
void XList::Reverse()
{
    int half = count/2;
    for(int i = 0; i < half; i++){
        void * tmp = items[i];
        items[i] = items[count - i - 1];
        items[count - i - 1] = tmp;
    }
}

/* remove the item at position i */
void XList::Remove(int i)
{
    if(i >= count || i < 0)
        return;

    memcpy(items + i, items + i + 1, sizeof(void*) * (count - i - 1));
    
    count--;
}

/* 
copy the list 
>> myMem - memory pool used for allocating the data in the new list
<< hard copy of the list
*/
XList * XList::Copy(XMem * myMem)
{
    XList * newList = new XList(maxNum, myMem);
    for(int i = 0; i < count; i++){
        newList->Add(GetItem(i));
    }
    return newList;
}

/* 
shuffle the list
>> nround - number of rounds for shuffling
>> beg - where we start
>> len - how many items are used in shuffling
*/
void XList::Shuffle(int nround, int beg, int len)
{
    if(beg < 0){
        beg = 0;
        len = count;
    }

    if(beg + len > count)
        return;

    srand((unsigned int)time(NULL));

    for(int k = 0; k < nround; k++){
        /* Fisher¨CYates shuffle */
        for(int i = 0; i < len; i++){
            float a = (float)rand()/RAND_MAX;
            size_t j = (unsigned int) (a*(i+1));
            void* t = items[beg + j];
            items[beg + j] = items[beg + i];
            items[beg + i] = t;
        }
    }
}

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