下沙论坛

标题: 一个方便好用的数组类 [打印本页]

作者: yzhlinux    时间: 2005-1-10 13:45
标题: 一个方便好用的数组类

to Whanxy

; z! d$ V+ F/ [

int N; / R A j( G, y! ZN=StrToInt(Edit1->Text); # h6 U3 R+ b! q0 m5 @1 eint RESULT[N];1 L* j$ d; W- c4 { 哈哈,这样的代码很有意思哦. ! w- m9 f9 Y* O2 v在c++里没有现成的类似vb的redim那样可以自由定义数组边界的办法,很多人提供了一些数组类以实现你这样的需求,同样微软也提供了这样的模板类 CArray ,你可以去参考这个摸板,不过 CArray 写的不够好只能给单线程的地方使用(后来个跟踪进去才发现是它的问题,没把偶气死)并且不能很好的支持使用 & 引用内部的成员(底层机制的问题),我从新改写了它的底层实现,写了一个摸板类,给你用正好满足你的需求.:, [+ P2 |* |6 ^! c7 q! C3 U #if !defined(CYCArray_INCLUDED) 0 C/ r8 S- L$ \; y#define CYCArray_INCLUDED* t H- }7 a' \; Q #include "afxtempl.h" ) [+ j2 [. H$ [: `# F/**" {2 X0 q2 O; f% ]8 S * 动态数组的模板类 (Write By yzhlinux ,you could use it any time any way if you could email me about it yzhlinux@hotmail.com) # k* f7 ?9 Z: q7 D0 A( A* 1.支持字符索引" c( q: k9 M9 O * 2.方便的添加删除修改任意一项; `) Z; N B, i' @ * 最后更新 2004-8-9 6 r* P2 X$ n; z" k6 \1 S**1.优化了字符索引的运作方式,使用数组存储 # T, n) B+ }$ Z) m5 W) W1 h**2.重写了底层数据的存储,将连续性的存储方式改为了非连续,; |4 I+ G' v8 G' R- k' x *** 从而很好有效地支持了“引用”,并且让数据的删除增加变的更为快速 % b7 D& X X4 j( x/////7 k- T) X. H' H7 Y T% } * 用法句举例1: ; q/ d U0 t- [% o/ G1 t; A$ t! d* YCArray<int,int> test1 ; 9 f0 v& }4 e1 ~* test1.Add("Number2",4);$ a- `* I$ T; ?* p * test1.Add("Number1",2);; ]( L: C2 _+ U7 t `2 K * printf("%d %d",test1["Number1"],test1["Number2"]);8 V g% d! N5 k$ H1 {7 y- j * 显示: 8 ?5 m+ b7 Y0 l6 o3 f5 L* 2 4) ^' h+ P; x' i+ {# X/ S+ } ///// : E( p1 t; P0 \! U* z+ h; D. n* 用法句举例2: ! p: F' ]3 ]' I+ ~* YCArray<CString,CString> test2 ; {9 z D+ t. ~. w8 H0 d* test2.Add("string2","hahahaha"); 6 X8 g9 h4 K8 d5 O8 Z" K( a* test2.Add("string1","yes yes yes yes"); ! j; f+ R& U8 P3 z! g) O" f) k* printf("%s %s %s",test2["string1"],test2["string2],test2[0]);/ P4 \, c/ Z8 v6 O6 a * 显示:4 ?. S8 e0 a3 `+ s* k* M: Q" Q * yes yes yes yes hahahaha yes yes yes yes + _# ^; X# H* G( y3 l( F7 S///// ; ~" j* W4 m, `4 k8 t, |! ]% F* 用法句举例3:( A6 P. Q! Z9 h$ d/ e * YCArray<CString,CString> test3 ;. k9 v' N6 G8 {+ E& t * test3.Add("string2","hahahaha"); ! E; m& @7 K' I' C x0 I8 Y* test3.Add("string1","yes yes yes yes"); : K- g" C- s+ A8 j; ^; l3 W5 D' p* test3.Add("","no no no"); 8 c: G" `. b2 `5 `. n* test3.AddR("string2","yes yes yes yes"); //AddR方法会去搜索字符索引,如果存在则替换 8 Z/ W5 [7 G; D6 ~* printf("%s %s %s",test3["string1"],test3["string2],test3[2]); * q0 Z6 a2 f* G) y* 显示:9 k) a, H% t# V * yes yes yes yes yes yes yes yes no no no % h6 Y2 L' c9 i6 F8 m6 B**/% \, E5 N7 C `" d& Z0 g5 \ /////////////////////////////////////////////////////////////////////////////: P7 v. ^! X3 e; v9 a. b // YCArray<TYPE, ARG_TYPE> 0 o; V& b) V R: |#include <afxmt.h>0 ?* D6 W: M6 u" ^3 a* S/ d template<class TYPE, class ARG_TYPE> ) [, H. h$ S" r9 j( mclass YCArray : public CObject7 ]1 {4 c6 q" N2 h { v' a/ A% n: h8 \ Y//friend YCArray<TYPE, ARG_TYPE>;7 m, l/ ?2 D1 k7 X CCriticalSection YCArray_Add;/ `5 t' _1 [4 N" J' T$ B public:$ q; b$ g# B0 i2 |/ [ // Attributes 7 R4 f) W8 ?/ b3 H S$ w7 _$ Gint GetSize() const;; J) w* q8 I5 f1 w( n' d; N int GetUpperBound() const;' ~; v8 T' n8 G5 s6 Y4 g void SetSize(int nNewSize, int nGrowBy = -1);

- v/ A1 x0 B% o( u7 c; j$ n

// Operations 0 B/ U* c6 i; h' C; ?+ E+ S' [// Clean up 9 p6 T- h4 J2 F; t! l/ V3 evoid FreeExtra(); & Z& G7 l e$ [/ ~void RemoveAll();

$ b& g0 t( D, W: P7 V: h! e3 v

// Accessing elements P5 w' ^+ L, o5 E% x7 q5 F; ?) F# rTYPE GetAt(int nIndex) const; * L& L& v2 u; ^( Y, k' `7 Z/ f" z/ ivoid SetAt(int nIndex, ARG_TYPE newElement);2 S+ x) Y% y U1 V Z( m TYPE& ElementAt(int nIndex);

" Q9 V4 H, y# r; y

TYPE GetAt(CString cIndex) const;8 }2 [$ t: E2 `* j N6 B+ V% _- I void SetAt(CString cIndex, ARG_TYPE newElement);4 n( r- Y X( X! t/ p TYPE& ElementAt(CString cIndex);

& ~$ k! n& P2 ?* Z

// Direct Access to the element data (may return NULL)7 W7 |% j e: Z" F! O8 e! ] const TYPE** GetData() const; " @ M; `4 `5 ]TYPE** GetData();

" K* c3 J n- D( T

// Potentially growing the array 1 n8 e6 }5 R" U1 C( ^, zvoid SetAtGrow(int nIndex, ARG_TYPE newElement);' d& B9 M/ w* [, M9 Z void SetAtGrow(CString cIndex, ARG_TYPE newElement);7 i8 K2 c- e0 e+ C0 u int Add(ARG_TYPE newElement,CString cIndex = ""); $ S. T% v2 E) }1 o+ t# S bint AddR(ARG_TYPE newElement,CString cIndex = "")//如果存在就替换0 b* z, V* z5 s- u/ X { & W' L( b) j0 l% n7 t% ~ int nIndex = GetIndex(cIndex);+ _- J) v* S# D if(cIndex!=""&&nIndex >-1){3 U3 }/ Q K9 X& l3 g; ^! N+ u' M4 A operator[](nIndex)=newElement;/ ~. O! s. K. u6 ^; x }else{+ N1 A& v. x% v y# r& V nIndex = m_nSize;* d6 ?2 d# a0 n4 M% E; s SetAtGrow(nIndex, newElement);& M. @1 Z4 C0 r# X9 |" M6 k SETIndex(cIndex,nIndex); " c# d( q6 Q: B3 P return nIndex; 0 a3 V: O! B) d: f: ^: u } 3 A- Q9 f+ o _$ c! z}1 f4 a' w2 J1 f* p5 k2 G int AddM(ARG_TYPE newElement,CString cIndex = "") ! ]+ T8 P6 t! U. ?/ D{ . i4 G0 d c$ u E! I P7 @ static int nIndex ; ; v. g1 L9 z1 a0 J YCArray_Add.Lock();4 o8 o3 e& R# c0 r& i) a nIndex = m_nSize;6 N+ I# K' c- x: a/ a/ W SetAtGrow(nIndex, newElement); % w8 y+ a; s. `9 k2 ^' m9 _ YCArray_Add.Unlock(); F) y( Z+ c3 f7 \ SETIndex(cIndex,nIndex); / C! O' V+ i7 o6 \0 W% v! W. {9 t return nIndex; 2 _6 D* q) _& N& Y};4 Y9 j0 F( I# Z" y* j9 \ int Append(const YCArray& src);# w/ m8 ^2 f0 a void Copy(const YCArray& src);

3 M/ R, L. P" B$ |. a! Y

// overloaded operator helpers ; ]7 F2 j: T1 N+ ?3 nTYPE operator[](int nIndex) const;% [ ]# Q& m" x$ f TYPE& operator[](int nIndex); 0 V+ m/ \' c# D3 F6 N; \1 p" F& oTYPE operator[](CString cIndex) const;. d N3 K1 L8 B TYPE& operator[](CString cIndex);2 c( l: _) X3 @2 b! L" `% P //YCArray<TYPE,ARG_TYPE> operator=(YCArray<TYPE,ARG_TYPE>& tparr)const; ) f$ ^; c+ y; N* n- a7 l) PYCArray<TYPE,ARG_TYPE>& operator=(YCArray<TYPE,ARG_TYPE>& tparr); + ^) v8 V* X: j2 T8 s0 | R: \// Operations that move elements around ( E1 S! l8 G) F! k6 t9 fvoid InsertAt(int nIndex, ARG_TYPE newElement, int nCount = 1);2 S/ ]# o. H2 |" C4 W+ x6 l void RemoveAt(int nIndex, int nCount = 1);: x1 b, S$ D8 H' a void RemoveAt(CString cIndex,int nCount = 1); # w4 ~. J3 e& F0 x- Yvoid InsertAt(int nStartIndex, YCArray* pNewArray);' i5 o3 B) m- d* J: }: x# | void InsertAt(ARG_TYPE newElement,int nIndex,CString cIndex);

& O7 ~) m: A8 ]# Q* {

int GetIndex(CString cIndex);: O( o+ `( Z$ ~( I. Z; G( b CString GetIndex(int nIndex);

. X5 m. g) I* _3 p. f/ W# t

CString GetSign(); 7 d+ Y' T }$ f9 y0 jint SetSign(CString& sign);

1 y' Z9 g2 m6 N( A7 `

// Implementation4 K7 w* J8 p4 A; T protected:/ g9 ?6 B9 @6 ^% Q p3 } TYPE** Ym_pData; // the actual array of data! q: d7 f% J" X, j int m_nSize; // # of elements (upperBound - 1)) A2 E( w1 ^: |8 I9 t4 B4 w int m_nMaxSize; // max allocated- z! z' S, w' g( \ int m_nGrowBy; // grow amount! J- L( Z q! O! e5 ?% w4 a: ? private:7 T- m8 P- o9 \' r$ y int lock_sign;& h0 ?; V, ?! J& T$ N, \( u/ L* J CString Sign; 0 ?% ?. [6 _3 ?, a0 I9 n+ b# eint MAXSIGNTIME;

: N3 w' S+ _( {3 |4 F2 z

CString* strIndex;* D B2 b! ]8 t+ F* f6 D4 G& Q7 E int strIndexNum; . w% a, z0 f9 d' y3 S: bBOOL SetIndexSize(int nIndex); , {& N4 V- Y& g: [BOOL SETIndex(CString cIndex,int nIndex);$ K- U2 `) s$ Z7 R% w BOOL INSERTIndex(int nIndex,int nCount =1); ' q5 l& A. u+ E6 _/ I* w3 JBOOL DELIndex(int nIndex,int nCount = 1); 1 J2 `" P: W" B* m/ r" {# |BOOL DELIndex(CString cIndex,int nCount = 1);% q2 v8 K7 a5 f; r: S4 K/ y void DestructAllElement(int nIndex,int nCount)/ Q9 ~, a: @6 [8 T' g/ d {//销毁对象,包括每个指针指向的对象, l, p/ R6 \5 j% L; d DELIndex(nIndex,nCount); 6 z- d8 N7 {4 b3 H2 O ASSERT(nIndex >= 0); % d4 j. p% |- G# F; _& g, n5 h. L ASSERT(nCount >= 0);2 u8 X4 f) Z6 s; c) ]% q) ` ASSERT(nIndex + nCount <= m_nSize); 4 A# p/ ]. H2 o' R* K if(nCount>0&&m_nSize>0){ 3 e) S: ^" L& o4 E for(int i =nIndex;i<nIndex+nCount;i++){ , k) \7 ?6 x3 k9 S //Ym_pData->~TYPE(); // 由于ConstructAllElements 中是 Ym_pData[nIndex] = new TYPE;所以不需要Ym_pData->~TYPE() 4 U4 C2 b, A% K- \- m2 ^/ P) ` delete Ym_pData;: r0 `* J) ^+ ? @& n( M }2 S3 \% c5 g2 e" p! H } / ~+ C+ l/ X* V2 ?0 r) y}; ( y& N4 A5 Z: W, |2 j. X( d- Avoid ConstructAllElements(int nIndex,int nCount) m- y* G( m A1 Y. B{//创建对象,包括 new 出每个指针指向的对象5 w5 g9 P, c4 k" u( j n //nIndex = 0;3 [6 t: j& g' t% G+ R memset((void*)(Ym_pData+nIndex), 0, nCount * sizeof(TYPE*)); 9 A" M: E4 U! k5 u8 _ o+ n0 M9 h for (; nCount--; nIndex++) " a( V' T) }! c9 g# g Ym_pData[nIndex] = new TYPE; 6 v/ ~* @2 ?# ^5 d};; a. [; K* V5 X4 A3 P0 w7 G public:+ |2 K% b' I& }' f8 w" c2 m: b // Construction ) u. p# M1 w$ uYCArray(); & ]7 m0 W/ z& J" J f' _; F8 d, f8 m1 GYCArray(YCArray<TYPE, ARG_TYPE>& tp);: O/ {# U; w& S# I( R# E) a* u ~YCArray(); - q" I8 T1 h/ G3 b+ l Jvoid Serialize(CArchive&);$ G; c2 n) x) O #ifdef _DEBUG ) G# z; h( j6 _% J- T, p/ Wvoid Dump(CDumpContext&) const;6 ]- Z/ s) n, |: }. k. L2 q void AssertValid() const;7 R/ X# _) }- J, G" d #endif

1 M8 p" X5 v. L- o

};

% S# ~+ |; y& _1 ? ~+ G" {
[此贴子已经被作者于2005-1-10 14:04:39编辑过]
7 u9 y+ `7 ]- u% I R
作者: yzhlinux    时间: 2005-1-10 13:56

//接上面

( r; `1 Q: z6 Ctemplate<class TYPE, class ARG_TYPE> X* \2 F1 N# }1 U" Q8 v+ b$ wYCArray<TYPE, ARG_TYPE>::~YCArray(), |: D+ f8 W) C& O) [% x { 3 [: H+ h# S( s$ J7 T A% y ASSERT_VALID(this);

if (Ym_pData != NULL) % s& c& e, i+ ^ h' T& O9 V% C0 B( b% b {" {% }# \$ y7 j: j DestructAllElement(0,m_nSize);% G9 H# _, z) M# G- c0 c* z1 F! r* A //DestructElements<TYPE>(Ym_pData, m_nSize);) p+ c2 z0 m( E" d9 J: ] delete[] (BYTE*)Ym_pData;5 G. Q ~( O# x8 ^( l }. t2 D# y" E& m. T, U( x9 u% ~ }% z4 x& w/ [/ \& g7 G; `) I: | template<class TYPE, class ARG_TYPE> 6 z2 q! C9 j9 N7 c1 z$ jCString YCArray<TYPE, ARG_TYPE>::GetSign() # `" E a& a5 W: p0 g# b5 D{ + X8 f/ H. K; m6 o' g2 ? lock_sign=0; V) J5 ]1 r L return Sign; d- o6 g" F3 W7 J/ `} / [8 {$ Z; [- Vtemplate<class TYPE, class ARG_TYPE> 7 l3 [/ |2 \2 t# X5 s% Bint YCArray<TYPE, ARG_TYPE>::SetSign(CString& sign) 1 W, v7 ^7 C% x( x6 w# E1 Q5 ?6 q{; W8 i. u( {8 _4 n! ?* h8 @- Q9 ? int i=0; ; U* J0 s3 _; ?1 L5 }9 u while(lock_sign&&i<MAXSIGNTIME){ ~( Y1 r5 K! X- [; N/ G Sleep(1);+ }0 T7 y0 w$ |- d) t# a' y i++;) m% e$ u& N- {# x } $ R; {2 \* C6 W: m lock_sign=1; w) Z$ N0 K% H) ^% P4 w Sign=sign; : X4 b s! f$ a5 P1 P1 g4 s return TRUE; ' w, X; t- S' u0 r& d}" A8 p) L* ?/ @ //用与把 nindex 后的索引往后推 nCount ,自动调整好buffer 4 t; t5 p, O! C' Y8 u5 x" rtemplate<class TYPE, class ARG_TYPE>" e# v( ]) L6 O* n7 |. I, D! Q BOOL YCArray<TYPE, ARG_TYPE>::SetIndexSize(int nNewSize) $ I- a+ O8 O4 w+ Z1 i. I- P{ 2 F( g* C& k& n% [7 U# ` if(strIndexNum < nNewSize){ ; G" `9 _' c( N6 ? CString* tp = new CString[nNewSize];//新的buffer# A" E/ i2 Z+ a for(int i=0;i<strIndexNum;i++){//把老索引复制过来! Q1 Y% r$ f/ v2 j7 E! e tp = strIndex;1 \3 i/ C9 q" U% F }, M0 m# K: G0 X9 s( C for(i=strIndexNum;i<nNewSize;i++){ r/ A; C0 C, B! j$ I+ g tp = "" ;2 [2 F: ^4 D; Z0 t" P+ T } , f0 n2 ?% ~ F delete[] strIndex ;% l- m S6 S' i& l5 C( t6 Q9 G strIndex = tp ; 1 n3 G1 D7 V: @- ?, m$ ] strIndexNum = nNewSize ; , H U5 F/ U6 V; y }else if(strIndexNum < nNewSize){2 q& a" c7 e% u0 g1 U$ J for(int i=nNewSize;i<strIndexNum;i++){ . R! d+ u) E( Q2 Y z; E, L. w strIndex = "" ;* H) O$ q- e' E) P } ( F! c E* ]. I4 G# ], E }7 ]; G4 t# u, Z4 h) S4 K return TRUE; / Y) ~9 M R. O7 k}* p2 a7 R+ Y% R# m template<class TYPE, class ARG_TYPE>* T3 N( h5 Z( Y BOOL YCArray<TYPE, ARG_TYPE>::INSERTIndex(int nIndex,int nCount /*=1*/)5 U+ Q4 w5 G0 r { 7 F: O. T! P: `5 o# l CString* tp = new CString[m_nSize+nCount];//新的buffer ! T+ Q# T: U9 ?+ g( J, V" J for(int i=0;i<nIndex;i++){//把老索引复制过来 & L; A- N, d0 J2 m3 V9 V tp = strIndex;& B! \3 j! d5 F# V. _: } }int j =0 ; ( r ]% u1 ]1 I% g* S for(i=nIndex+nCount;i<m_nSize;i++){//把老索引复制过来 - r2 l) D$ X$ F! `, u, _+ H tp = strIndex[nIndex+j];( O; G( f) C* D8 i0 n: X& W j++;% s1 j! g, w( y+ E7 e8 K7 J3 V6 { } 8 y [* r* {7 V; g* w: r7 Y delete[] strIndex ;; v* M2 j, H$ C8 Z4 |6 G. g strIndex = tp ; 2 t0 F8 z5 }& Y2 }0 W return TRUE;5 r1 M: G) T& T6 L3 L; ?" |5 h/ D } . ?0 d i5 E! N& Atemplate<class TYPE, class ARG_TYPE># I) ]2 a- R( J$ @9 ^/ U BOOL YCArray<TYPE, ARG_TYPE>::SETIndex(CString cIndex,int nIndex) + e3 x( d3 E6 M{//在 nIndex 后面添加一个 字符串索引 ' s, v' d' U+ z, v/ H strIndex[nIndex] = cIndex ; ?9 [. G; @0 H, {" C+ u return TRUE; - \% u+ o4 I+ B8 e}3 m0 j" {$ C2 R8 e6 T0 [2 r template<class TYPE, class ARG_TYPE>7 K4 u: J' _" |' h BOOL YCArray<TYPE, ARG_TYPE>:ELIndex(int nIndex,int nCount /*=1*/)' |# N2 x* ^, H$ T, ]: y {//需要在 m_nSize 变化之前调用!!. R! F" I* S$ e# W+ f ASSERT(nIndex >= 0);% ?3 ]4 h8 S- y; G/ u' m, x. e ASSERT(nCount >= 0);; C: u0 ~& M. O6 l) K5 Q [: r8 ~ ASSERT(nIndex + nCount <= m_nSize); + s9 I. B7 g% i9 {% f: r% t8 y9 B int j =0 ;5 p% E [# U% p for(int i=nIndex+nCount;i<m_nSize;i++){//把老索引复制过来8 I/ I% X3 K" k% A& S strIndex[nIndex+j] = strIndex;8 M. [- P. r1 F5 w( ]: K8 ^ j++; 2 [! n5 x# L: U9 J" u2 \4 y# L2 d2 K }/ O! x% I# Q7 }- h6 \8 o' w4 K! n return TRUE;( @" O9 F1 V+ `6 w7 H# U } , U: n& i8 X+ L2 n: Y: _; n; {template<class TYPE, class ARG_TYPE>; n5 B$ S! @5 M3 h: j& x" { BOOL YCArray<TYPE, ARG_TYPE>:ELIndex(CString cIndex,int nCount /*=1*/) , _* G+ ]& U1 O' M J f{ & P F5 N k* I; v0 [- W int nIndex=this->GetIndex(cIndex);; A/ w& {4 B6 E; p! S return this->DELIndex(nIndex,nCount); ) R* j6 z3 e$ t. t. s: I- h! X}; b: `4 J+ {3 A# _" C$ s template<class TYPE, class ARG_TYPE>1 T' p7 w5 I# ~4 Y1 `: Z/ u int YCArray<TYPE, ARG_TYPE>::GetIndex(CString cIndex)3 q/ Y- e2 `% Q {//得到 cIndex 的数字索引 ( Z T* U9 X; F0 p' F6 \ z7 i3 s! ~ int nPos = -1;2 T% w3 ], v5 f+ C for(int i=0;i<m_nSize;i++){ $ h2 I; ~ m( V3 O if(strIndex == cIndex){ 0 X; @: B. s" [8 ] nPos = i ;break; : n" X. C+ c% r5 [ | } 5 o6 k) K6 F2 d! p% D } . L" {: Q1 f9 H' J2 w' j return nPos;1 a% n: S( m8 x } & P& z: w+ }% i3 s& n) F/ stemplate<class TYPE, class ARG_TYPE> 2 n/ v* W. f! K! O6 ^5 D' s4 _CString YCArray<TYPE, ARG_TYPE>::GetIndex(int nIndex) * }( d, E' D! ?7 Z" g7 w' B; h+ [{//返回 nIndex 的字符串索引) f, u9 l5 I* b& V return strIndex[nIndex];. F7 n! g) C) Y0 B } ) A& K! h: r% u+ [2 u" T( g///////////////////////////////////////////////////////////////////////////// 9 x) L6 m5 ^! }2 K0 n# h) N& W3 |0 d// YCArray<TYPE, ARG_TYPE> inline functions

template<class TYPE, class ARG_TYPE> 7 f+ }1 b8 p. v! N& qAFX_INLINE int YCArray<TYPE, ARG_TYPE>::GetSize() const ! G& f8 q+ n% _4 G { return m_nSize; } . A( d5 X" i) E7 f Qtemplate<class TYPE, class ARG_TYPE>3 b9 n' G+ K2 o* l AFX_INLINE int YCArray<TYPE, ARG_TYPE>::GetUpperBound() const : z, H! {- Z) w4 M4 v& `$ e { return m_nSize-1; }8 V. {6 [. }3 m9 p7 b. L template<class TYPE, class ARG_TYPE>4 C1 g' l# e* { W" l AFX_INLINE void YCArray<TYPE, ARG_TYPE>::RemoveAll()) Y$ e+ |$ C0 W' ^/ U" t* ~. Q { SetSize(0, -1); }1 ^3 |( d7 ?6 A: k( \" \5 L template<class TYPE, class ARG_TYPE> 0 |2 T7 K3 V& I( v4 PAFX_INLINE TYPE YCArray<TYPE, ARG_TYPE>::GetAt(int nIndex) const : X* }# {: q2 U+ D2 p0 V { ASSERT(nIndex >= 0 && nIndex < m_nSize); 0 Z6 w3 L0 H5 R1 i- O0 ^ return *Ym_pData[nIndex]; } , e: L$ g! h- a3 |template<class TYPE, class ARG_TYPE> / h/ g1 N6 G; FAFX_INLINE void YCArray<TYPE, ARG_TYPE>::SetAt(int nIndex, ARG_TYPE newElement)0 L ~) R5 q2 u# U! C { ASSERT(nIndex >= 0 && nIndex < m_nSize); 9 s2 v$ a( K6 j* { *(Ym_pData[nIndex]) = newElement; }

template<class TYPE, class ARG_TYPE> 3 V" B! {0 V4 B- a6 q0 n7 F; lAFX_INLINE TYPE& YCArray<TYPE, ARG_TYPE>::ElementAt(int nIndex) 8 ^; |! u, O: ?) A9 \$ C { ASSERT(nIndex >= 0 && nIndex < m_nSize); * g' ?) Y* I) t8 o8 a( c return *Ym_pData[nIndex]; }

template<class TYPE, class ARG_TYPE>% k1 C9 v( j+ I TYPE YCArray<TYPE, ARG_TYPE>::GetAt(CString cIndex) const: X) B0 g3 v, r. V: L y, F) L" Z {. j. d V: f( @) {. J" @% h4 n int nIndex=GetIndex(cIndex);! C* i, K9 g( \+ B: b$ G3 | return GetAt(nIndex); 7 R5 N5 _7 i) R; h! v} # P, s- `& r3 k; v3 Ltemplate<class TYPE, class ARG_TYPE>% J5 j' p1 Z! ]# f8 r H5 g/ f; M. ^ void YCArray<TYPE, ARG_TYPE>::SetAt(CString cIndex, ARG_TYPE newElement) * y* }* I5 r# z{6 S) E0 T$ Q+ r# G! p+ V! T8 A7 ], O% Q int nIndex=GetIndex(cIndex); - a: b ?' _$ ~3 M; a1 ?3 r$ e9 J return SetAt(nIndex, newElement); 2 z. N0 U t' g; U3 a; f; O/ ]) `/ x}; M" E$ n' w9 X template<class TYPE, class ARG_TYPE> ) D, q7 u, x1 k2 ~1 j3 o3 XTYPE& YCArray<TYPE, ARG_TYPE>::ElementAt(CString cIndex)/ o% l6 N5 x7 c' ]: C+ _$ r+ _5 [ {& I* B' I$ k4 t* t6 b# w int nIndex=GetIndex(cIndex);! K0 \3 r2 T1 [( p return ElementAt(nIndex);8 T# L3 [+ M9 m8 }. K } 8 F3 B `% a3 itemplate<class TYPE, class ARG_TYPE> 7 h8 R, R$ u6 \5 Z2 j, q8 ]AFX_INLINE const TYPE** YCArray<TYPE, ARG_TYPE>::GetData() const 9 w2 i+ A+ O& a; l { return (const TYPE**)Ym_pData; }' {6 `2 N- Y5 P" X Q, o5 q- Y3 N5 y template<class TYPE, class ARG_TYPE> 9 m! x2 P! |- \AFX_INLINE TYPE** YCArray<TYPE, ARG_TYPE>::GetData() 0 ~+ Z( G& C: f1 s2 b8 v" f' F9 \ { return (TYPE**)Ym_pData; }2 [0 h3 | O' p7 W* U! L H template<class TYPE, class ARG_TYPE>) S8 ~4 ?! A" r) h' Q. H8 n4 P AFX_INLINE int YCArray<TYPE, ARG_TYPE>::Add(ARG_TYPE newElement,CString cIndex /* ="" */) 8 `" `9 m" s& z, T8 `1 [9 m" h% R { int nIndex = m_nSize;- p' ]. ]1 w2 N$ R! v3 Y4 E P' v SetAtGrow(nIndex, newElement); # H; t, Y8 y' e) c( q5 w SETIndex(cIndex,nIndex);: n- O2 y% Y( x- Q' u2 t return nIndex; }6 }0 i4 ~" w7 c* D( N7 J2 r( ^ template<class TYPE, class ARG_TYPE> - D8 Y# ~* @/ [AFX_INLINE TYPE YCArray<TYPE, ARG_TYPE>:perator[](int nIndex) const + A, v" g( c+ i5 B$ S { return GetAt(nIndex); } , J( L9 n/ X1 Q' S Ftemplate<class TYPE, class ARG_TYPE> + N. A& Z( _, h+ ?0 @" e, [6 q( E6 uAFX_INLINE TYPE& YCArray<TYPE, ARG_TYPE>:perator[](int nIndex) - W+ ~& |; c: |: ^* R6 H { return ElementAt(nIndex); }8 N( h( C8 t- s+ i0 @$ d template<class TYPE, class ARG_TYPE>, K1 O# G* b0 S6 Q/ { AFX_INLINE TYPE YCArray<TYPE, ARG_TYPE>:perator[](CString cIndex) const 8 W9 P( z& v, e" m6 B( {/ h0 E{+ ]) V- A* @" m4 O int nIndex=GetIndex(cIndex);

return operator[](nIndex);7 {$ s5 Z& f$ v7 S: `1 F }3 f: C& ]0 p. i* q4 a3 S5 r# a5 B template<class TYPE, class ARG_TYPE> 1 n0 N+ N f( ] A3 ~AFX_INLINE TYPE& YCArray<TYPE, ARG_TYPE>:perator[](CString cIndex)* v- ^' ~6 [$ O0 d8 o r5 R3 \9 W# i { $ F, v2 F1 K* {/ g int nIndex=GetIndex(cIndex);0 v( \% \+ R, F N5 T8 F return operator[](nIndex);2 q7 C7 g9 V4 j }% U( R3 Y, d p2 e, q% Y x$ Z /* 8 Y: a- I! [8 f5 D* N1 ]7 }template<class TYPE, class ARG_TYPE> 9 X3 t' f7 {9 R u" e; BAFX_INLINE YCArray<TYPE,ARG_TYPE> YCArray<TYPE, ARG_TYPE>:perator=(YCArray<TYPE,ARG_TYPE>& tparr) const' X0 |) y1 `, ~5 _; d2 c { & `- k9 n+ {5 C q, ~( k" k$ E int i,j; " A( b/ j6 l. `0 ^( }* O+ F for(i=0;i<tparr.GetSize();i++){" h4 \9 m, G7 D$ B j = GetIndex(tparr.GetIndex(i));: a5 t$ g9 Q: C, D& K6 L4 B9 a8 T if(j>-1){) o+ [ L5 }$ G2 @ operator[](tparr.GetIndex(i)) = tparr;) u$ B, x' H7 ]) I8 @. q }else{ 3 ]7 F! z4 F1 i! s# q, M1 g4 M Add(tparr,tparr.GetIndex(i));5 {& `+ P, T+ B. d/ w5 F% R" [9 e/ |. U }# x! b' F1 a8 ~ } 1 {& q) E0 g1 k0 u7 ? return this;: t" a, Q9 r; X1 O# b) i } ) w- k0 M! i. }; n( t# F: {*/ + ^: b1 b' z6 D& `7 Q+ y$ ~8 ~template<class TYPE, class ARG_TYPE> & L# Q- s: C$ vAFX_INLINE YCArray<TYPE,ARG_TYPE>& YCArray<TYPE, ARG_TYPE>:perator=(YCArray<TYPE,ARG_TYPE>& src)7 D: S, B4 {+ \ { 0 ^/ y/ j7 O0 v9 j$ z% x. T9 j8 g ASSERT_VALID(this);- }( J3 O7 X# s Q7 @" C+ n ASSERT(this != &src); // cannot append to itself

SetSize(src.m_nSize); $ l5 @; O5 C4 X+ D& E% c- ~8 L W6 j for(int i=0;i<m_nSize;i++){ & W; | @" c9 t /*将此句修改为内存拷贝*///! ~2 n* n4 q2 l2 T *Ym_pData = *src.Ym_pData ;) s: ?2 z8 K! ?1 e4 ?9 p$ U9 [ //memcpy(Ym_pData,src.Ym_pData,sizeof(TYPE)); + ^% \. b+ G* [, W" k SETIndex(src.GetIndex(i),i); : c! G: u" I; T8 n# I+ i4 | }- s7 d1 j% E3 g, `* F2 T return *this;, F2 l. Z8 g, ]9 m } # Z# L: u' ^7 d, ]- g$ S5 y {6 ^///////////////////////////////////////////////////////////////////////////// 4 E, y2 W4 m) f* I8 h# c$ i// YCArray<TYPE, ARG_TYPE> out-of-line functions

template<class TYPE, class ARG_TYPE>' k0 n- {; U! C. r/ a; F1 D4 B; X YCArray<TYPE, ARG_TYPE>::YCArray()+ H# B9 J9 G4 ] {* u5 a% h8 m* D- z7 h Ym_pData = NULL;, i/ b( _% P6 I8 _& D; x0 m( I strIndexNum = m_nSize = m_nMaxSize = m_nGrowBy = 0;$ F0 o: f" I4 u% U& I( O strIndex=NULL;MAXSIGNTIME=10;" b# p0 z0 @1 D1 h1 N' S j/ n } + |4 a( t: D( h6 Y; s1 ntemplate<class TYPE, class ARG_TYPE> 2 A7 [* x3 q' g0 B3 _7 s0 AYCArray<TYPE, ARG_TYPE>::YCArray(YCArray<TYPE, ARG_TYPE>& tp) # t" V5 r" }7 a& R0 k( T{* s5 K4 Z( B, I+ U$ b, v) s6 F Ym_pData = NULL; # Q: P& k2 _& Y8 H2 q# \) ~6 d' | strIndexNum = m_nSize = m_nMaxSize = m_nGrowBy = 0; ( v% d& ^9 l- \' Y5 n; L( n strIndex=NULL;MAXSIGNTIME=10; {" \& W7 e# f, r! G3 @ operator=(tp);0 r o' W" l# N, n( [. Z4 p+ L }

template<class TYPE, class ARG_TYPE> 2 u7 Q( B" W. P9 C- H) Jvoid YCArray<TYPE, ARG_TYPE>::SetSize(int nNewSize, int nGrowBy)# Y& y0 v: v/ @5 k6 B6 n7 D { 1 ]& N$ |6 J5 [: i* w ASSERT_VALID(this); $ E2 U. D$ \1 s( P ^ ASSERT(nNewSize >= 0);

if (nGrowBy != -1); @( t) L! y& D m_nGrowBy = nGrowBy; // set new size

if (nNewSize == 0){! q/ x2 Q( {, m9 y // shrink to nothing 4 r3 C& t1 Q5 n! ^ if (Ym_pData != NULL){ : l# h: z' L, h/ @2 @, @ DestructAllElement(0,m_nSize);: o! S, ^/ {4 ^# }' w/ U //DestructElements<TYPE>(Ym_pData, m_nSize);3 b: t1 {( [( M& \/ C$ q delete[] (BYTE*)Ym_pData;3 U9 e7 w9 y4 e# E! o7 d, {7 \2 |- R Ym_pData = NULL; . z7 U: {6 u# ` } ) V, Y6 \& q" B: l5 y b m_nSize = m_nMaxSize = 0;# D/ y& i( {8 P9 h" |: U% f+ Q: L" F } else if (Ym_pData == NULL){1 X; W/ i; ?+ ~2 `, z // create one with exact size+ b6 q7 j) z, j+ e H/ [ #ifdef SIZE_T_MAX 2 C9 E4 X+ _7 r) r9 G' R ASSERT(nNewSize <= SIZE_T_MAX/sizeof(TYPE*)); // no overflow / g# a* y# d+ M# i- X& i8 N: v#endif% p1 o% K3 f: a i; e+ d$ K7 v# z8 a Ym_pData = (TYPE**) new BYTE[nNewSize * sizeof(TYPE*)]; ; [7 T) y5 `2 D$ N1 k0 `' M% | ConstructAllElements(0,nNewSize);//ConstructElements<TYPE>(Ym_pData, nNewSize);4 Y x7 z$ x. w7 m m_nSize = m_nMaxSize = nNewSize; 8 ~$ v4 a. l- N) z" t+ T } else if (nNewSize <= m_nMaxSize){/ ~' M8 K4 ^3 w: d2 S. \ // it fits9 N: J# T a d+ r& I if (nNewSize > m_nSize)' j. u. s9 ~2 T3 a* S {$ t, m: ~- Z$ r! k B1 |$ v // initialize the new elements# a6 U& q. Z: v! b# F2 i6 v" W0 U( z ConstructAllElements(m_nSize,nNewSize-m_nSize);//ConstructElements<TYPE>(&Ym_pData[m_nSize], nNewSize-m_nSize); - ^" f; _: v$ T! A } 3 w3 ~: G1 e) X8 C4 n else if (m_nSize > nNewSize); g' W m( k; A { ! P( o) M! {* W // destroy the old elements2 {* B' Z1 g. E9 a% T DestructAllElement(nNewSize,m_nSize-nNewSize); * \. f. X. |9 K4 n. X% ` //DestructElements<TYPE>(&Ym_pData[nNewSize], m_nSize-nNewSize);0 k# X- W1 Z0 u( y" b }, S- F% T4 P( I r: S$ T m_nSize = nNewSize;8 D; M' n( J0 D* N/ E } ) O m/ |* s/ C* i4 \% w else 4 M4 t1 _/ ^: Y0 f, T { ; T1 _& P) x( _# b5 K! w // otherwise, grow array 0 {9 q) R) O+ c7 t4 \; b int nGrowBy = m_nGrowBy; - G5 A+ w! D' `0 t if (nGrowBy == 0) : A) F7 t" O3 D& @7 W {: k1 h7 V, o) l2 b2 e2 l // heuristically determine growth when nGrowBy == 03 T$ f( P# ^2 g3 f# ~0 N( v // (this avoids heap fragmentation in many situations) / I4 Q0 Z6 w6 V2 |. I nGrowBy = m_nSize / 8; 4 v- M- y2 P u6 R" N( g* Q nGrowBy = (nGrowBy < 4) ? 4 : ((nGrowBy > 1024) ? 1024 : nGrowBy);9 v8 S3 i* p) Z, f) v9 _ }2 T8 E* V4 U% M3 Y int nNewMax; ( x+ y, V5 b. ?% ?5 t) z if (nNewSize < m_nMaxSize + nGrowBy) 3 a& o) R5 X( N' j+ `$ v nNewMax = m_nMaxSize + nGrowBy; // granularity! J2 `. H- R( b8 H else + q5 p2 v2 i# W2 }- y nNewMax = nNewSize; // no slush

ASSERT(nNewMax >= m_nMaxSize); // no wrap around/ \2 J' S# ^7 {4 X0 p- V9 g4 P _) ^ #ifdef SIZE_T_MAX# R \8 e" G% D9 N* u ASSERT(nNewMax <= SIZE_T_MAX/sizeof(TYPE)); // no overflow 3 G* d1 g4 f: K9 Z#endif 5 g+ t- W9 n: V3 ?( U _ TYPE** pNewData = (TYPE**) new BYTE[nNewMax * sizeof(TYPE*)];//TYPE* pNewData = (TYPE*) new BYTE[nNewMax * sizeof(TYPE)];

// copy new data from old 3 z5 z" e$ R3 k; g- { memcpy(pNewData, Ym_pData, m_nSize * sizeof(TYPE*));

// construct remaining elements% Y8 G! \6 q4 M4 u ASSERT(nNewSize > m_nSize); //throw("/*wait for me --- yzhlinux*/");6 Z1 Q$ t6 K; ~4 p" Q delete[] (BYTE*)Ym_pData; , V0 |) i5 q' ]5 F Ym_pData = pNewData;2 i& R1 p: W' P' V ConstructAllElements(m_nSize,nNewSize-m_nSize);//ConstructElements<TYPE>(&pNewData[m_nSize], nNewSize-m_nSize);

// get rid of old stuff (note: no destructors called) 4 a, U" i1 f4 M! H! \, a8 V m_nSize = nNewSize; % V. b* c y' ]- W Q* j m_nMaxSize = nNewMax;0 U0 m e& b3 M" ]8 ?. y* g }. S6 u5 |) }' Z* W, U SetIndexSize(nNewSize);0 M) T2 i' M% k5 I& M } 9 i% k) M) x6 ?2 B1 k


作者: yzhlinux    时间: 2005-1-10 13:58

//接上面

7 B) o6 }$ m# z. J9 p template<class TYPE, class ARG_TYPE> _' f; `9 X9 e/ g0 X. C3 t1 C# _* aint YCArray<TYPE, ARG_TYPE>::Append(const YCArray& src) 4 T3 B( L5 Z3 C% d* [. i{- h* ~" F0 b* O( F ASSERT_VALID(this); # |. C; L0 Y6 D2 c$ U ASSERT(this != &src); // cannot append to itself

int nOldSize = m_nSize; 4 D1 b1 C0 E( w. p; @8 L# ^; s3 h1 f) N SetSize(m_nSize + src.m_nSize);

ConstructAllElements(nOldSize,src.m_nSize); 4 v( d& t& M+ r' `4 p for(int i=nOldSize;i<m_nSize;i++){ C: w, N7 h$ @) h /*将此句修改为内存拷贝*/// 5 _# a0 U# r0 Q *Ym_pData = *src.Ym_pData[i-nOldSize] ; : J- Z8 [! ]: s* @/ n8 x N1 X //memcpy(Ym_pData,src.Ym_pData[i-nOldSize],sizeof(TYPE)); & _# r" t% \7 v4 R- w SETIndex(src.GetIndex(i-nOldSize),i); . I5 ~: `# R. N6 G z } 2 p$ P$ g2 I! Z+ a /*wait for me*///CopyElements<TYPE>(Ym_pData + nOldSize, src.Ym_pData, src.m_nSize); $ @3 o1 m6 A+ N" w% o0 k return nOldSize;5 u* J& ^) M0 X# I* R }

template<class TYPE, class ARG_TYPE>8 [6 d* H+ s% e: O# E$ C void YCArray<TYPE, ARG_TYPE>::Copy(const YCArray& src) e h8 h' L4 r& y- z- v7 v" @ { ( n# c. n+ m d; f2 v6 e ASSERT_VALID(this); 9 ] K' f1 b! { H! d& n ASSERT(this != &src); // cannot append to itself

SetSize(src.m_nSize); }3 I% F. o- A8 O for(int i=0;i<m_nSize;i++){ ' D+ Z; D) w, l" u& [7 m0 x: r. m /*将此句修改为内存拷贝*///*Ym_pData = *src.Ym_pData ; " M$ [: F4 @; B; E memcpy(Ym_pData,src.Ym_pData,sizeof(TYPE)); & i I2 m: n- \! b+ w7 z) N/ K SETIndex(src.GetIndex(i),i); 3 P5 g; ?2 ]+ U0 l) _! @ } & Q- |+ V& K. P6 a; l /*wait for me*///CopyElements<TYPE>(Ym_pData, src.Ym_pData, src.m_nSize); 9 A( S) r+ x- B/ Q% Q}

template<class TYPE, class ARG_TYPE> $ B* r4 z) v# I8 ], k: E' Ivoid YCArray<TYPE, ARG_TYPE>::FreeExtra() * t; t3 g! _7 \% K! w0 Z{ : N2 V# ~' {' m8 L0 ~ ASSERT_VALID(this);

if (m_nSize != m_nMaxSize) 3 e9 ]) r3 c. V, {# z { ' `0 \/ c2 t; N" i& C: j // shrink to desired size; Z* Q. y) l+ m+ b: c; Y2 l #ifdef SIZE_T_MAX - A a0 [6 Y- r7 v5 a8 N ASSERT(m_nSize <= SIZE_T_MAX/sizeof(TYPE)); // no overflow# Y4 q- r/ f0 E1 I9 T4 N #endif4 p$ A- E, Q3 Q' F- \ TYPE* pNewData = NULL; # T1 E! v# }6 L if (m_nSize != 0)7 w; P- a+ g" k+ l5 v1 j$ m { 6 J7 s' C- l1 t7 @; s; b+ |( | pNewData = (TYPE**) new BYTE[m_nSize * sizeof(TYPE*)]; 1 _4 Z+ c5 {* ]; {" Z3 |- F+ v // copy new data from old : o. W9 E8 v& R memcpy(pNewData, Ym_pData, m_nSize * sizeof(TYPE*)); : u+ }1 T& e, J2 K( k$ h }

// get rid of old stuff (note: no destructors called)/ v. D; l/ ^9 g# o1 m3 I1 p3 _8 N delete[] (BYTE*)Ym_pData; 4 k% o0 w! ~" x- i! q6 j/ ^! w Ym_pData = pNewData; $ Y/ J' e1 Q; a1 ?8 L m_nMaxSize = m_nSize;6 s5 F! O# G" a& A6 W C' H/ ` } % e# M$ E0 C5 A1 v) S* o* b} ( L: w4 u F( Y2 Dtemplate<class TYPE, class ARG_TYPE>) i; T5 N7 W* I9 q1 U5 k void YCArray<TYPE, ARG_TYPE>::SetAtGrow(CString cIndex, ARG_TYPE newElement)$ D `$ h. l& N5 ^; @ {6 G) j: i$ j7 o int nIndex=GetIndex(cIndex);+ ]1 u1 ^" S. |& ]5 c+ ~2 L return SetAtGrow(nIndex,newElement);7 y8 m! A3 u: E- k, G } 9 l0 ^+ u8 s1 H4 ctemplate<class TYPE, class ARG_TYPE> * p& h$ W7 S( X1 M# W+ z# qvoid YCArray<TYPE, ARG_TYPE>::SetAtGrow(int nIndex, ARG_TYPE newElement) 7 c- A, c. w# e6 f8 r. E# R1 y6 p5 }{9 _$ \ v9 r2 d1 ?; ~$ b8 r! E ASSERT_VALID(this);- `8 G5 I1 h' Z7 w, |( p6 Y5 h3 r! W ASSERT(nIndex >= 0);

if (nIndex >= m_nSize)4 Q1 W+ f0 r7 t1 Q1 @ SetSize(nIndex+1, -1); , e, {* h. z5 Q *Ym_pData[nIndex] = newElement; $ D S. F' d1 L; v3 e5 a. o( u7 w+ V}

template<class TYPE, class ARG_TYPE>9 c! f& M6 _9 M0 B+ A: F void YCArray<TYPE, ARG_TYPE>::InsertAt(int nIndex, ARG_TYPE newElement, int nCount /*=1*/)5 V; o! b4 _$ q" A6 V { 9 x t* {( r6 k* K% R ASSERT_VALID(this);: U- ^: D, r% t3 F, p ASSERT(nIndex >= 0); // will expand to meet need " s) e1 Q, |, L; F: D) I1 v ASSERT(nCount > 0); // zero or negative size not allowed

if (nIndex >= m_nSize) ! A, k$ \' `4 K {, t( q8 R& `# R5 K0 I // adding after the end of the array$ b5 ^/ D4 @5 a& z3 p: G. k SetSize(nIndex + nCount, -1); // grow so nIndex is valid : R) |# [' u3 y7 J" p5 v } - ^4 J" ^5 I1 {$ c/ J else+ w/ o/ r: N8 _/ M T! x9 @ {5 O& J% |+ u, o3 x // inserting in the middle of the array 6 x1 V; z3 \$ g0 ?9 k2 x int nOldSize = m_nSize; ; F& z8 O. ]6 Z" d9 U( Y( U SetSize(m_nSize + nCount, -1); // grow it to new size8 @# ?5 E1 U8 h3 Y- J, b& f // destroy intial data before copying over it 1 E1 u& S$ Y y: v" O7 d% o z6 c /*不需要销毁了,因为 SetSize 的是指针*///DestructAllElement(nOldSize,nCount);4 L2 y3 i$ K; y //DestructElements<TYPE>(&Ym_pData[nOldSize], nCount);$ K- |# o- u3 C9 q& d9 | // shift old data up to fill gap; v( b0 R* w1 ?! I# {; _ memmove(&Ym_pData[nIndex+nCount], &Ym_pData[nIndex], ' S) h# r- ]+ x5 I2 w% J; [" D (nOldSize-nIndex) * sizeof(TYPE*));

// re-init slots we copied from8 [: r# v# m5 G /*不需要销毁了,因为 SetSize 的是指针*///ConstructAllElements(nIndex,nCount);//ConstructElements<TYPE>(&Ym_pData[nIndex], nCount);$ ^/ e$ ~7 h. G( I7 R% V! V }

// insert new value in the gap" {' J6 f! i' h2 v0 v: q; f ASSERT(nIndex + nCount <= m_nSize); 0 }, U8 v' U6 U) b( ~ while (nCount--){7 P5 Y2 M, e$ ^# m* P h- d *Ym_pData[nIndex++] = newElement; ; R) O6 t5 J* @: v }4 P% g! ?, `1 d }6 B0 j" x) Q, ^ J; H template<class TYPE, class ARG_TYPE> $ b8 N; v! {0 l8 y) F7 v$ vvoid YCArray<TYPE, ARG_TYPE>::RemoveAt(CString cIndex,int nCount /*=1*/)8 b3 ~' K" F1 k, z8 g( ? { 2 L7 {: {; E& `" V% Y int nIndex = GetIndex(cIndex); , B/ u# R9 u6 i+ ?& \" q$ \ RemoveAt(nIndex,nCount); & [8 p7 _" j' g: E5 N% T4 B}- x% S$ z @" L/ a% _; i: Z template<class TYPE, class ARG_TYPE> . z+ }% E h" |. E Avoid YCArray<TYPE, ARG_TYPE>::RemoveAt(int nIndex, int nCount /*=1*//*=1*/) : I3 W3 H) Y5 M0 W% a' |) h, E' A{ + P, j. {0 j- c4 W" j ASSERT_VALID(this);& c L" `, J( K, s ASSERT(nIndex >= 0);% g; r1 p" C7 H5 n& {3 a4 u# D ASSERT(nCount >= 0); + k( b' H o0 f7 V ASSERT(nIndex + nCount <= m_nSize);# {/ _. E( o* S" [! H //yzh) \% x+ c' ~5 m; q* c DELIndex(nIndex);* c3 Y0 D! W; H //yzh : j t& `7 r2 B* W // just remove a range8 z7 x, p6 Q( ]. h; k$ V int nMoveCount = m_nSize - (nIndex + nCount); //需要移动的数目 ' \! g2 K1 P0 Z2 q* K. Z9 Z, o& N DestructAllElement(nIndex,nCount);$ o8 H: w3 b% P4 r //DestructElements<TYPE>(&Ym_pData[nIndex], nCount); 7 ]5 a5 `% \, x2 O8 V if (nMoveCount) 6 Q( G1 A1 M$ h2 M( p* [- l- e memmove(&Ym_pData[nIndex], &Ym_pData[nIndex + nCount],7 d8 x) J( t9 ]& p# R3 o nMoveCount * sizeof(TYPE*)); + {* Y( i' Q2 O/ q/ W# X( ] m_nSize -= nCount;2 u5 R+ D/ T! W }

template<class TYPE, class ARG_TYPE>5 d% K* s( n8 x' F. m void YCArray<TYPE, ARG_TYPE>::InsertAt(int nStartIndex, YCArray* pNewArray) ( N# F: h" K% _1 H4 Z; _$ K{ 3 s/ W% X+ u2 W8 L u- } ASSERT_VALID(this); 5 E- n. n5 | ~0 g$ k ASSERT(pNewArray != NULL);6 i6 u3 }9 _# k' M0 u/ m ASSERT_VALID(pNewArray); o* o8 L1 \9 S ASSERT(nStartIndex >= 0);

if (pNewArray->GetSize() > 0). W( r1 z3 w+ h( L# N) i9 f {8 U/ U5 i3 B" N, Z InsertAt(nStartIndex, pNewArray->GetAt(0), pNewArray->GetSize());$ ?- H% w! a% [3 N. q9 O for (int i = 0; i < pNewArray->GetSize(); i++) + v3 }2 }; A" ?. Z. \# s* d4 u! j SetAt(nStartIndex + i, pNewArray->GetAt(i)); u* _' W ^0 s } 3 @: B9 I/ R8 P1 T}0 U4 V- E7 \$ B$ a template<class TYPE, class ARG_TYPE>; V- s$ W6 B+ ], c( q9 l void YCArray<TYPE, ARG_TYPE>::InsertAt(ARG_TYPE newElement, int nIndex,CString cIndex)+ l7 Z6 Q( k: }4 H4 \" P/ X {9 \! a7 ?. h8 x; Z# J) R ADDIndex(cIndex,nIndex+1); ( c0 Y3 A& Z6 o3 p7 O) I InsertAt(newElement,nIndex); 5 r" V1 o& ~) I} 1 |; g1 w* D5 ktemplate<class TYPE, class ARG_TYPE>+ L; H( k0 ^1 I: A void YCArray<TYPE, ARG_TYPE>::Serialize(CArchive& ar) ; w* D0 }$ M/ e, t2 f9 h{ $ v5 D" M5 f, Q; O% z* B ASSERT_VALID(this); v; y3 t4 |# S' B4 O throw("/*wait for me --- yzhlinux*/"); 8 I4 W8 y& ?7 G) ?: ?8 u CObject::Serialize(ar);, S* K2 n, _! w2 ~- Y if (ar.IsStoring()) G4 m4 h0 Y. W% A { : r0 ?2 [2 |) B7 F* ` ar.WriteCount(m_nSize); & g/ t! W/ A2 ~# d4 E _ } , n: G; X- V: t: a2 b( U8 i8 D else3 X0 V/ `# D2 t- h& y7 w { f7 Z& {0 D4 g1 Y# p% i+ ? DWORD nOldSize = ar.ReadCount();- w0 H& G/ x- l8 q% R SetSize(nOldSize, -1); 4 p) R |* g+ ]8 N) A } " v' V8 x$ u. Z3 c& R" y //SerializeElements<TYPE>(ar, Ym_pData, m_nSize);- y$ S$ z5 ?+ c- ^! j! l. l }

#ifdef _DEBUG7 H$ `! x( S7 x0 \0 q R( ^7 F template<class TYPE, class ARG_TYPE> . M) }" M' V& H. yvoid YCArray<TYPE, ARG_TYPE>::AssertValid() const + s S$ ?0 e& N ?9 f{) M( x8 Q; T' N CObject::AssertValid();

if (Ym_pData == NULL) + z' ^% ]5 s3 ^1 g' T {- k" ~, x! Z; N4 Q' ] ASSERT(m_nSize == 0);0 |! f$ o! r5 r- G+ q& W/ B- @ ASSERT(m_nMaxSize == 0);5 t0 O! N3 O; l: C5 E } 9 d8 ^: G: {/ g else1 C+ V6 [# ^& K' g5 Q) I3 o { - N: w0 j8 z! F/ U ASSERT(m_nSize >= 0); 1 a* R o! j" }8 f3 J ASSERT(m_nMaxSize >= 0); 7 H% N6 `2 {. N5 ? ASSERT(m_nSize <= m_nMaxSize);5 t4 J8 K" q- J- G1 N ASSERT(AfxIsValidAddress(Ym_pData, m_nMaxSize * sizeof(TYPE*))); 3 J( n! k. @3 H& T' O }. G: S2 ?4 P; ^( Q9 L3 b0 V L }, h+ S# \, T3 ^% B( l6 K; i$ {2 Q% h template<class TYPE, class ARG_TYPE>5 h4 U8 T: `$ q# Y void YCArray<TYPE, ARG_TYPE>:ump(CDumpContext& dc) const r2 }, p( }: U' y" ~4 V{ 5 R% S/ k/ E1 k' ]& P CObject:ump(dc);0 w8 q/ k2 a5 V9 q0 d throw("/*wait for me --- yzhlinux*/");: F, K" ^" K4 K% @ dc << "with " << m_nSize << " elements";1 ^+ a% Z6 M" i' }$ X7 V' Y$ I if (dc.GetDepth() > 0) 2 o$ V" A5 I% N4 d {! D9 o% d3 S; D# ?. X dc << "\n";: Q- s% Z+ E+ }+ n# Q1 F3 b. V /*wait for me --- yzhlinux*/// DumpElements<TYPE>(dc, Ym_pData, m_nSize); : U. c) A) F9 [2 K }

dc << "\n";! K3 X7 B- E) z( ] }: V- w3 Q2 s7 m- H" J #endif

#endif

//完


作者: Eagle    时间: 2005-1-10 14:44
太长了点,叶师兄应该打包上传的。
作者: yzhlinux    时间: 2005-1-10 15:47
以下是引用Eagle在2005-1-10 14:44:00的发言: ' M( {$ ]7 _# Z& ^太长了点,叶师兄应该打包上传的。
1 o+ s- ]) G1 p. T' ~4 F4 j; f' W9 {0 @ [0 R. A0 S 8 Q; v0 C9 C5 V( k4 e就是因为发不了文件啊,奇怪,现在不能上传文件了,不然不用那么辛苦# e( w8 W |3 s# V! r9 f
作者: Eagle    时间: 2005-1-13 23:02
先传到帖图区……
作者: 管理员    时间: 2005-4-11 21:15
赚帖子不好吗
作者: yzhlinux    时间: 2005-6-3 14:56
现在可以传文件了,附上,好用的摸板数组类,支持 ATL 可以在任何地方使用。* g* C& d3 t, q, D3 c

1 q  n5 v" x$ M' O2 Q7 m E2mkAVvk.rar (4.61 KB, 下载次数: 19)
作者: 游侠无极限    时间: 2005-6-3 17:55
std::map 模板的作用好像就是这样的……




欢迎光临 下沙论坛 (http://bbs.xiasha.cn/) Powered by Discuz! X3.3