smp_datastruct.h
来自「2002年」· C头文件 代码 · 共 217 行
H
217 行
#ifndef __SMP_DATASTRUCT_H__
#define __SMP_DATASTRUCT_H__
#include "smp_func.h"
template <class Type> class TVector{
public:
Type x;
Type y;
TVector<Type>(Type x = 0, Type y = 0) { this->x = x; this->y = y; }
TVector<Type> operator -() const{ return TVector<Type>(-x, -y);}
TVector<Type> operator +(const TVector<Type> &a) const{ return TVector<Type>(x + a.x, y + a.y); }
TVector<Type> operator -(const TVector<Type> &a) const{ return TVector<Type>(x - a.x, y - a.y); }
TVector<Type> operator *(const Type &a) const{ return TVector<Type>(x * a, y * a); }
TVector<Type> operator *(const TVector<Type> &a)const { return TVector<Type>(x * a.x, y * a.y); }
TVector<Type> operator /(const Type &a) const{ return TVector<Type>(x / a, y / a); }
TVector<Type> operator /(const TVector<Type> &a) const{ return TVector<Type>(x / a.x, y / a.y); }
void operator =(const Type &a) { x = a; y = a; }
void operator +=(const TVector<Type> &a) { x += a.x; y += a.y; }
void operator +=(const Type &a) { x += a; y += a; }
void operator -=(const TVector<Type> &a) { x -= a.x; y -= a.y; }
void operator -=(const Type &a) { x -= a; y -= a; }
void operator *=(const Type &a) { x *= a; y *= a; }
void operator /=(const Type &a) { x /= a; y /= a; }
bool operator !=(const TVector<Type> &a) { return (x != a.x) || (y != a.y); }
bool operator !=(const Type &a) { return (x != a) || (y != a); }
bool operator ==(const TVector<Type> &a) { return (x == a.x) && (y == a.y); }
inline Type mod() const{ return Type(sqrt(x*x+y*y)); }
inline Type mod2() const{ return x*x + y*y; }//square mod
void Normalize(){ if (mod2() != 0) (*this) /= mod(); }
inline Type disaeolus(TVector<Type>& pos) const{return Max((Type)fabs(x-pos.x),(Type)fabs(y-pos.y)); }
inline Type dis2aeolus(TVector<Type>& pos) const{return (Type)(fabs(x-pos.x)+fabs(y-pos.y)); }
Type dist(const TVector<Type>& pos) const{ return (*this-pos).mod(); }
Type dist2(const TVector<Type> &a) const{ return (*this-a).mod2(); }//square dist
TVector<Type> Rotate(float ang){ return TVector<Type>(Cos(ang)*x - Sin(ang)*y,Sin(ang)*x + Cos(ang)*y); }
float Angle() const{ return ATan2(x,y); }
bool InFrontOf(const TVector<Type> &a) const{ return bool(x > a.x + 1); }
bool InFrontOf(const Type &a) const{ return bool(x > a + 1); }
bool Behind(const TVector<Type> &a) const{ return bool(x < a.x - 1); }
bool Behind(const Type &a) const{ return bool(x < a - 1); }
};
typedef TVector<float> Vector;
template <class Type, int size> class Index{
protected:
Type data[size];
int num_data;
public:
Index(){ Reset();}
inline void Reset(){num_data = 0;}
inline Type& Data(int idx){
#ifdef _DEBUG_MOD
if(idx >= size || idx < 0)
return data[0];
#endif
return data[idx];
}
inline void Add(const Type& v){
if(num_data >= size) return;
data[num_data++] = v;
}
inline void RmTail(int counts = 1){
num_data -= counts;
#ifdef _DEBUG_MOD
if(num_data < 0) num_data = 0;
#endif
}
//Note: The last data is moved to the deleted pos.
inline bool Delete(int idx){
if(idx >= num_data || idx<0) return false;
if(idx != num_data - 1){
data[idx] = data[num_data-1];
}
num_data --;
}
void operator = (const Index<Type, size>& r){
num_data = r.Num_datas();
for(int i=0; i<num_data; i++){
data[i] = r.data[i];
}
}
inline int Num_datas() const{return num_data;}
inline int Maxsize() const{return size;}
inline bool IsFull() const{return bool(num_data>=size);}
inline bool IsEmpty() const{return bool(num_data <=0);}
};
template <class Type, int size> class CircularArray{
protected:
int count;
int current_idx;
Type data[size];
public:
inline void Reset(){
count = 0;
current_idx = -1;
}
CircularArray(){ Reset();}
inline void Push(const Type& v){
count ++;
current_idx = RecurrentNext(current_idx, size);
data[current_idx] = v;
}
inline bool IsDataKnown(int prev){return bool(prev<count && prev <=size);}
inline Type& Data(int prev){
return data[(current_idx-prev+size)%size];
}
};
/* Timed Data */
template <class Data> class TimedData{
public:
Data data;
int time;
TimedData(int time = -2){ this->time = time;}
TimedData(const Data& data, int time){ this->data = data; this->time = time;}
void Setdata(const Data& data, int time){ this->data = data; this->time = time;}
void InvalidTime(){time = -1;}
void operator = (const Data& d){ data = d;}
TimedData operator + (TimedData d){ return TimedData(data + d, time);}
TimedData operator - (TimedData d){ return TimedData(data - d, time);}
TimedData operator / (TimedData d){ return TimedData(data / d, time);}
TimedData operator * (TimedData d){ return TimedData(data * d, time);}
};
/************* TimedDataArray **********************/
/*存储对象:带时间的数据T,
索引: 时间
*/
template <class T, int size> class TimedDataArray{
private:
typedef TimedData<T> _Tdata;
_Tdata data[size];
inline int Index(int idx){return idx % size >=0 ? idx % size : idx % size + size;}
public:
inline _Tdata& TimedData(int time){return data[Index(time)];}
inline T& Data(int time){return data[Index(time)].data;}
inline T ChkData(int time){return IsDataKnown(time) ? data[Index(time)].data : T(0);}
inline bool IsDataKnown(int time){return data[Index(time)].time == time;}
inline void Setdata(const T& data, int time){this->data[Index(time)].data = data; this->data[Index(time)].time = time;}
};
/*存储对象: 带时间的, 大小为Tsize的数据数组
索引: 时间+数组索引
*/
template <class T, int Tsize, int size> class Timed2D_Array{
private:
TimedData<T[Tsize]> data[size];
inline int Index(int idx){return idx % size >=0 ? idx % size : idx % size + size;}
public:
//inline T[]& Data(int time){return data[Index(time)].data;}
inline T& Data(int idx, int time){return data[Index(time)].data[idx];}
inline bool IsDataKnown(int time){return data[Index(time)].time == time;}
inline void Setdata(const T& data, int idx, int time){this->data[Index(time)].data[idx] = data; this->data[Index(time)].time = time;}
};
/*存储对象: 带时间的对索引数据
索引: 时间 + (idx1, idx2)
*/
template <class T, int size> class TimedDataTri2DArray{
private:
TimedData<T> data[size * (size -1) /2 + 1];
inline int Index(int idx1, int idx2){
if (!IsValidIdx(idx1, idx2))
return size * (size -1) /2;//invalid unit
if (idx1 > idx2)
Swap(idx1, idx2);
return (2 * size - 1 - idx1) * idx1 /2 + idx2 - idx1 - 1;
}
public:
inline bool IsValidIdx(int idx1, int idx2){
if (idx1 < 0 || idx1 >= size){
//wrong idx-1
return false;
}
if (idx2 < 0 || idx2 >= size){
//wrong idx-2
return false;
}
if (idx1 == idx2){
//wrong
return false;
}
return true;
}
bool IsDataKnown(int idx1, int idx2, int time){
return data[Index(idx1, idx2)].time == time;
}
inline void SetData(const T& Data, int idx1, int idx2, int time){
data[Index(idx1, idx2)].data = Data;
data[Index(idx1, idx2)].time = time;
}
inline void SetTime(int idx, int idx2, int time){
data[index(idx1, idx2)].time = time;
}
inline TimedData<T>& Element(int idx1, int idx2){ return data[Index(idx1, idx2)];}
inline T& Data(int idx1, int idx2){return data[Index(idx1, idx2)].data;}
};
#endif //__SMP_DATASTRUCT_H__
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?