Commit a864edb1 by xiaotong

new methods of XList: AddInt, AddFloat and AddLLong

parent 53c5f571
...@@ -119,6 +119,28 @@ TensorListBase<T>::~TensorListBase() ...@@ -119,6 +119,28 @@ TensorListBase<T>::~TensorListBase()
items = NULL; items = NULL;
} }
/*
reallocate
>> itemNum - the number of items
*/
template <typename T>
void TensorListBase<T>::Reallocate(int itemNum)
{
if (maxNum < itemNum) {
T* newItems;
newItems = (T*)realloc(items, sizeof(T) * itemNum);
if (newItems != NULL)
items = newItems;
else {
newItems = (T*)malloc(sizeof(T) * itemNum);
memcpy(newItems, items, count * sizeof(T));
free(items);
items = newItems;
}
maxNum = itemNum;
}
}
/* /*
add an item into the list add an item into the list
...@@ -180,6 +202,39 @@ void TensorListBase<T>::Add(const T& item) ...@@ -180,6 +202,39 @@ void TensorListBase<T>::Add(const T& item)
items[count++] = item; items[count++] = item;
} }
/* add an item (as an integer) into the list */
template <typename T>
void TensorListBase<T>::AddInt(const int item)
{
if (count == maxNum)
Reallocate(count * 2 + 1);
*(int*)(items + count) = item;
count++;
}
/* add an item (as a float) into the list */
template <typename T>
void TensorListBase<T>::AddFloat(const float item)
{
if (count == maxNum)
Reallocate(count * 2 + 1);
*(float*)(items + count) = item;
count++;
}
/* add an item (as a long long) into the list */
template <typename T>
void TensorListBase<T>::AddLLong(const long long item)
{
if (count == maxNum)
Reallocate(count * 2 + 1);
*(long long*)(items + count) = item;
count++;
}
/* /*
add a number of items into the list add a number of items into the list
>> inputItems - pointer to the array of items >> inputItems - pointer to the array of items
......
...@@ -75,6 +75,9 @@ public: ...@@ -75,6 +75,9 @@ public:
/* de-constructor */ /* de-constructor */
~TensorListBase(); ~TensorListBase();
/* reallocate */
void Reallocate(int itemNum);
/* add an item into the list */ /* add an item into the list */
void Add(T&& item); void Add(T&& item);
...@@ -84,6 +87,15 @@ public: ...@@ -84,6 +87,15 @@ public:
/* add an item into the list */ /* add an item into the list */
void Add(const T& item); void Add(const T& item);
/* add an item (as an integer) into the list */
void AddInt(const int item);
/* add an item (as a float) into the list */
void AddFloat(const float item);
/* add an item (as a long long) into the list */
void AddLLong(const long long item);
/* add a number of items into the list */ /* add a number of items into the list */
void Add(const T* inputItems, int inputItemCount); void Add(const T* inputItems, int inputItemCount);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论