39 #define POINT_TEST(p) { if ((p)==NULL) {cout<<#p<<"[New Memory Error]"<<endl; exit(0);} } 41 #define SAFE_NEW(p, Type) {p = new Type; POINT_TEST(p)} 42 #define SAFE_DNEW(PType, p, Type) { PType p = new Type; POINT_TEST(p) } 43 #define SAFE_NEW_ARRAY(p, Type, n) { p = new Type[n]; POINT_TEST(p) } 44 #define SAFE_NEW_DARRAY(p, Type, n, m) { p=new Type*[n]; for(int i=0; i<n; i++){p[i]=new Type[m];}; POINT_TEST(p) } 48 #define SAFE_DELETE(p) {if (p) delete (p); (p)=NULL;} 50 #define SAFE_DELETE_ARRAY(p) {if(p) delete [](p); (p)=NULL;} 51 #define SAFE_DELETE_DARRAY(p, n) { if(p){ for(int i=0; i<n; i++){delete [](p)[i];} delete [](p); (p)=NULL; } } 52 #define SAFE_DEL_POINTER_ARRAY(a) for (int i=0; i<a.GetNum(); i++) { SAFE_DELETE(a[i]); } a.Clean(); 58 #define DEFAULE_VECTOR_SIZE 16 59 #define VECTOR_bits2Size(n) (1<<n) 83 m_nBits = Size2Bits(size);
89 if (m_pBuffer != NULL)
97 inline T*
GetBuffer(
int i = 0)
const {
return m_pBuffer + i; }
110 int nNewBits = Size2Bits(i + 1);
116 for (
int n = 0; n<nSize; n++)
117 pNew[n] = m_pBuffer[n];
130 for (
int n = 1;; ++n) {
157 if (m_pBuffer == NULL) {
161 else if (m_nBits < vector.
m_nBits) {
173 if (m_pBuffer == NULL) {
177 else if (m_nBits < vector.
m_nBits) {
204 template <
typename T>
218 Array(T* pbuf,
int n) { Copy(pbuf, n); }
222 T& operator [](
int i)
225 cout <<
"[warning] the index less than 0: i=" << i << endl;
238 void SetNum(
int n) { this->Get(n - 1); m_nTop = n - 1; }
240 int GetNum()
const {
return m_nTop + 1; }
242 void Add(T t) { (*this)[m_nTop + 1] = t; }
244 T&
Add() {
return (*
this)[m_nTop + 1]; }
249 for (
int i = 0; i < GetNum(); i++) {
256 T&
End() {
return (*
this)[m_nTop]; }
266 void Copy(
const T* pbuf,
int n)
269 for (
int i = 0; i < n; i++)
270 (*
this)[i] = pbuf[i];
279 void operator = (
const Array<T> &array) { Copy(array); }
281 operator T* () {
return this->GetBuffer(); }
285 for (
int i = 0; i < GetNum(); i++) {
286 os << this->Get(i) <<
" ";
300 for (
int i = 0; i < GetNum(); i++) {
301 if ((*
this)[i] == t) {
311 for (
int i = 0; i < GetNum(); i++) {
324 for (
int i = 1; i < GetNum(); i++) {
325 if (this->Get(i) > this->Get(idx)) {
329 return this->Get(idx);
340 for (
int i = 1; i < GetNum(); i++) {
341 if (this->Get(i) < this->Get(idx)) {
345 return this->Get(idx);
355 template <
typename T>
380 template <
typename T>
395 this->Get(m_nTop) = p_t;
405 *p_pT = this->Get(m_nTop - 1);
415 return this->Get(m_nTop - 1);
430 template <
typename T>
443 void Clean() { m_nTop = 0; m_nBottom = 0; }
447 this->Get(m_nTop) = p_t;
453 if (m_nBottom == m_nTop)
456 *p_pT = this->Get(m_nBottom);
463 return m_nBottom == m_nTop;
466 int GetNum() {
return m_nTop - m_nBottom; }
478 template <
typename T>
514 this->Get(nTail) = t;
515 nTail = (nTail + 1) % nSize;
528 if (nHead == nTail) {
533 if (t) *t = this->Get(nHead);
534 nHead = (nHead + 1) % nSize;
539 if (t) *t = this->Get(nHead);
540 nHead = (nHead + 1) % nSize;
546 bool IsEmpty() {
return (!bFull) && nHead == nTail; }
550 int GetNum() {
return (bFull) ? nSize : max(nTail - nHead, nTail + nSize - nHead); }
558 for (
int i = 0; i < nSize; i++)
562 for (
int i = nHead; i != nTail; i = (i + 1) % nSize)
570 template <
typename T>
574 for (
int i = 0; i < len; i++) {
575 sum += pVec[i] * pVec[i];
580 template <
typename T>
581 double VecDot(T *pVec1, T *pVec2,
int len)
584 for (
int i = 0; i < len; i++) {
585 sum += pVec1[i] * pVec2[i];
590 template <
typename T>
594 for (
int i = 0; i < len; i++) {
595 sum += pow(pVec1[i] - pVec2[i], 2);
600 template <
typename T>
603 double sum = Dot(pVec1, pVec2, len);
604 return sum / Norm(pVec1, len) / Norm(pVec2, len);
607 template <
typename T>
616 char *pStrDup = strdup(pStr);
617 char *p = strtok(pStrDup,
"[], ");
619 aStr.
Add() = strdup(p);
620 p = strtok(NULL,
"[], ");
624 for (
int i = 0; i < aStr.
GetNum(); i++) {
626 char *p = strtok(aStr[i],
": ");
629 p = strtok(NULL,
": ");
634 case 1: a.
Add(d[0]);
break;
636 for (t = d[0]; t <= d[1]; t += 1)
640 for (t = d[0]; t <= d[2]; t += d[1])
644 cout <<
"Error vector expression!! =>" << pStr << endl;
650 for (
int i = 0; i < aStr.
GetNum(); i++) {
656 template <
typename T>
663 template <
typename T>
666 if (a > b)
return -1;
677 template <
typename T>
690 while (first < last && compar(p[last], key) >= 0)
697 while (first < last && compar(p[first], key) <= 0)
706 Qsort(p, low, first - 1, compar);
707 Qsort(p, first + 1, high, compar);
710 template <
typename T>
void Output(ostream &os)
output the array
bool Out(T *p_pT)
Move a value outof queue.
void Copy(const T *pbuf, int n)
Copy the array to current array.
void MemCopy(Array< T > &array)
using memcpy to copy a array.
void Copy(const Array< T > &array)
Copy the array to current array.
A queue based the dynamic memory mangement.
T & Get(int i)
get the value at position i
T & Add()
Add a value to the tail of array, example 'a.Add() = t'.
void Clean()
Clean the queue, don't release the memory.
int Size2Bits(int size)
Transform the size to bit.
Array(int size=DEFAULE_VECTOR_SIZE)
constructor
int m_nTop
Record the top of the array.
double VecDot(T *pVec1, T *pVec2, int len)
[Vec-function] v1*v2^T
#define DEFAULE_VECTOR_SIZE
defualt vector size
CirQueue(int size=10)
constructor
void Clean()
clean the queue. Donot release the memory
bool IsEmpty()
Return if the queue is empty.
void Input(istream &is)
input the array
void Qsort(Array< T > &a, int(*compar)(const T &, const T &)=Compar_Inc)
Quick sork, redefine for class Array.
double VecAngle(T *pVec1, T *pVec2, int len)
[Vec-function] the cos of the angle of v1 and v2
T & End()
Get the value at the tail of array.
int nTail
the tail of queue
bool IsFull()
if the queue is full
bool Pop(T *p_pT)
Pop a value outof the stack.
T * GetBuffer(int i=0) const
get the buffer pointer
int Compar_Inc(const T &a, const T &b)
comparing function for increase sort.
T * m_pBuffer
buffer pointer
T Sum()
summate all the values in the array
#define VECTOR_bits2Size(n)
transform the bit to memory size
int GetNum()
Get number of values.
Array(T *pbuf, int n)
constructor
bool Out(T *t=NULL)
Remove a value outof queue.
void Insert(T t)
insert a value. Avoid repeating
T & Get(int i, int j)
Get the value at position i row and j column.
T Min(int &idx)
Get the minine value.
void In(T t)
Add a value into queue.
int Find(T t)
Find a value and return the position.
this is the basic class of Array/Stack/Queue. Realize the dynamic memory management ...
void Zero()
Fill all the memory to ZERO. call 'memset'.
void SetNum(int n)
Set Array number, to melloc enough memory.
bool IsEmpty()
if the queue is empty
void Copy(const Vector< T > &vector)
Copy a vector to another, using '=' to set values.
void Clean()
clean the stack. Donot release the memory
void Clean()
Clean the array. Just set the top of array to -1 and donot release the memory.
VecUnfold(cfg_strWriteAtIter, pFunc->m_aWriteAtIter)
void Push(T p_t)
Push a value into the stack.
void Add(Array< T > &a)
Add a array to the tail of current array.
double VecDiff(T *pVec1, T *pVec2, int len)
[Vec-function] |v1-v2|
int GetNum() const
Get Array number.
void Add(T t)
Add a value to the tail of array.
int m_nBits
bit number of memory size
void Init(int size)
Init the queue.
T Top()
Get the Top value of stack.
int nHead
the head of queue
int m_nTop
the top of the queue
void Fill(T m)
set all the values to m
int Size() const
get the memory size
void BufferOutside(T *&p_pt)
Set the buffer pointer to p_pt, and re-init the vector.
Vector(int size=DEFAULE_VECTOR_SIZE)
constructor, do not init the memeory to zero.
void In(T p_t)
Add a value into queue.
T GetSum()
Summary all the values if queue.
int GetSize()
Get the buffer size of queue.
void MemCopy(Vector< T > &vector)
Copy a vector to another, using 'memcpy' to set values.
T Max(int &idx)
Get the maximum value.
double VecNorm(T *pVec, int len)
[Vec-function] sqrt(v*v^T);
define all the code written by Bin Wang.
Array(Array &array)
constructor
int GetNum() const
Get the number of the stack.
opt Add(wbOPT_STRING, "feat", &cfg_pathFeatStyle, "a feature style file. Set this value will disable -order")
int Compar_Dec(const T &a, const T &b)
comparing function for decrease sort
int GetNum()
Return the value number.