Commit 97da3265 by xiaotong

updates of GetItem and SetItem in List

parent 9b3209d8
...@@ -274,16 +274,64 @@ void TensorListBase<T>::Insert(int pos, T&& item) ...@@ -274,16 +274,64 @@ void TensorListBase<T>::Insert(int pos, T&& item)
/* get the item at position i */ /* get the item at position i */
template <typename T> template <typename T>
T& TensorListBase<T>::GetItem(int i) const inline T& TensorListBase<T>::GetItem(int i) const
{ {
CheckNTErrors(i >= -count && i < count, "Index of a list item is out of scope!"); CheckNTErrors(i >= -count && i < count, "Index of a list item is out of scope!");
CheckNTErrors(count > 0, "Cannt index the item in an empty list!"); CheckNTErrors(count > 0, "Cannot index the item in an empty list!");
if (i < 0) if (i < 0)
return items[count + i]; return items[count + i];
else else
return items[i]; return items[i];
} }
/* get the item at position i and force it to an integer */
template <typename T>
inline int TensorListBase<T>::GetItemInt(int i) const
{
CheckNTErrors(i >= -count && i < count, "Index of a list item is out of scope!");
CheckNTErrors(count > 0, "Cannot index the item in an empty list!");
if (i < 0)
return 0;
else {
T r = items[i];
void * p = &r;
return *(int*)p;
}
}
/* get the item at position i and force it to a float number */
template <typename T>
inline float TensorListBase<T>::GetItemFloat(int i) const
{
CheckNTErrors(i >= -count && i < count, "Index of a list item is out of scope!");
CheckNTErrors(count > 0, "Cannot index the item in an empty list!");
if (i < 0)
return 0;
else {
T r = items[i];
void * p = &r;
return *(float*)p;
}
}
/* get the item at position i and force it to an long long number */
template <typename T>
inline long long TensorListBase<T>::GetItemLLong(int i) const
{
CheckNTErrors(i >= -count && i < count, "Index of a list item is out of scope!");
CheckNTErrors(count > 0, "Cannot index the item in an empty list!");
if (i < 0)
return 0;
else {
T r = items[i];
void * p = &r;
return *(long long*)p;
}
}
/* set the item at position i */ /* set the item at position i */
template <typename T> template <typename T>
inline void TensorListBase<T>::SetItem(int i, const T& item) inline void TensorListBase<T>::SetItem(int i, const T& item)
...@@ -299,6 +347,33 @@ inline void TensorListBase<T>::SetItem(int i, T&& item) ...@@ -299,6 +347,33 @@ inline void TensorListBase<T>::SetItem(int i, T&& item)
items[i] = item; items[i] = item;
} }
/* set the item (as an integer) at position i */
template<typename T>
inline void TensorListBase<T>::SetItemInt(int i, const int item)
{
if (i >= 0 && i < count) {
*(int*)(items + i) = item;
}
}
/* set the item (as a float) at position i */
template<typename T>
inline void TensorListBase<T>::SetItemFloat(int i, const float item)
{
if (i >= 0 && i < count) {
*(float*)(items + i) = item;
}
}
/* set the item (as a long long) at position i */
template<typename T>
inline void TensorListBase<T>::SetItemLLong(int i, const long long item)
{
if (i >= 0 && i < count) {
*(long long*)(items + i) = item;
}
}
/* /*
find the position of the first matched item find the position of the first matched item
>> item - the item for matching >> item - the item for matching
......
...@@ -99,12 +99,30 @@ public: ...@@ -99,12 +99,30 @@ public:
/* get the item at position i */ /* get the item at position i */
T& GetItem(int i) const; T& GetItem(int i) const;
/* get the item at position i and force it to an integer */
int GetItemInt(int i) const;
/* get the item at position i and force it to a float number */
float GetItemFloat(int i) const;
/* get the item at position i and force it to an long long number */
long long GetItemLLong(int i) const;
/* set the item at position i */ /* set the item at position i */
void SetItem(int i, const T& item); void SetItem(int i, const T& item);
/* set the item at position i */ /* set the item at position i */
void SetItem(int i, T&& item); void SetItem(int i, T&& item);
/* set the item (as an integer) at position i */
void SetItemInt(int i, const int item);
/* set the item (as a float) at position i */
void SetItemFloat(int i, const float item);
/* set the item (as a long long) at position i */
void SetItemLLong(int i, const long long item);
/* find the position of the first matched item */ /* find the position of the first matched item */
int FindFirst(const T& item); int FindFirst(const T& item);
...@@ -135,7 +153,13 @@ public: ...@@ -135,7 +153,13 @@ public:
/* short */ /* short */
T& operator[] (int i) const { return GetItem(i); }; T& operator[] (int i) const { return GetItem(i); };
T& Get(int i) const { return GetItem(i); }; T& Get(int i) const { return GetItem(i); };
int GetInt(int i) const { return GetItemInt(i); };
float GetFloat(int i) const { return GetItemFloat(i); };
long long GetLLong(int i) const { return GetItemLLong(i); };
void Set(int i, T item) { SetItem(i, item); }; void Set(int i, T item) { SetItem(i, item); };
void SetInt(int i, int item) { SetItemInt(i, item); };
void SetFloat(int i, float item) { SetItemFloat(i, item); };
void SetLLong(int i, long long item) { SetItemLLong(i, item); };
}; };
struct XTensor; struct XTensor;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论