自定义的Troop泛型类( c++, java和c#)的实现代码
Troop
//formoreinformation,pleaseaccesshttp://www.one-lab.net
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceonelab
{
publicinterfaceISortable
{
voidsort(booldecending);
}
publicinterfaceIStringable
{
intfromString(Stringinput);
StringtoString();
}
publicclassTroop:ICloneable
{
publicoverrideintGetHashCode()
{
returnbase.GetHashCode();
}
publicoverrideboolEquals(objectobj){returnthis==(Troop)obj;}
publicobjectClone()
{
TroopoValue=newTroop();
oValue.cloneFrom(this);
returnoValue;
}
protectedboolequalsTo(TroopaValue)
{
intlen=aValue.length();
if(len==this.length())
{
for(intn=0;naValue)
{
data.Clear();
data.AddRange(aValue.data);
}
publicstaticbooloperator==(Troopa0,Troopa1){objecto1=(object)a0;objecto2=(object)a1;if(o1==null&&o2==null)returntrue;return(o1==null||o2==null)?false:a0.equalsTo(a1);}
publicstaticbooloperator!=(Troopa0,Troopa1){objecto1=(object)a0;objecto2=(object)a1;if(o1==null&&o2==null)returnfalse;return(o1==null||o2==null)?true:!a0.equalsTo(a1);}
privateboolmHasError=false;
publicboolhasError(){returnmHasError;}
publicListdata=newList();
publicTget(intindex)
{
mHasError=false;
if(index>=0&&index=0&&index=0&&indexrange)
{
data.AddRange(range);
}
publicboolinsertRange(intindex,refListrange)
{
if(index>=0&&index=0&&index0&&index>=0&&((index+count)<=data.Count))
{
data.RemoveRange(index,count);
returntrue;
}
returnfalse;
}
publicintlength()
{
returndata.Count;
}
publicvoidenqueue(Tvalue)
{
data.Insert(0,value);
}
publicTdequeue()
{
mHasError=false;
intlength=data.Count;
if(length>0)
{
Tb=data[length-1];
data.RemoveAt(length-1);
returnb;
}
mHasError=true;
returndefault(T);
}
publicvoidpush(Tvalue)
{
data.Add(value);
}
publicTpop()
{
returndequeue();
}
publicTroopgetRange(intindex,intcount)
{
Troopoutput=newTroop();
mHasError=true;
if(count>0&&index>=0&&((index+count)<=data.Count))
{
output.data=data.GetRange(index,count);
mHasError=false;
}
returnoutput;
}
publicvoidreverse()
{
data.Reverse();
}
publicboolswap(intindex0,intindex1)
{
intlength=data.Count;
if(index0>=0&&index0=0&&index1=0&&index=data.Count)return;
Listleft=data.GetRange(offset-1,data.Count-offset+1);
Listright=data.GetRange(0,offset-1);
data.Clear();
data.AddRange(left);
data.AddRange(right);
}
publicvoidrollBackward(intoffset)
{
if(offset>=data.Count)return;
Listleft=data.GetRange(offset,data.Count-offset);
Listright=data.GetRange(0,offset);
data.Clear();
data.AddRange(left);
data.AddRange(right);
}
}
}
//formoreinformation,pleaseaccesshttp://www.one-lab.net #ifndefVALUES_H #defineVALUES_H #include#include namespaceonelab { classISortable { public: virtualvoidsort(booldecending)=0; }; template classTroop { private: boolmHasError; staticboolequals(constTroop &from,constTroop &to); public: QList data; Troop(); Tget(intindex); boolset(intindex,Tvalue); voidappend(Tvalue); boolinsert(intindex,Tvalue); voidappendRange(constQList &range); boolinsertRange(intindex,constQList &range); voidclear(); boolremove(intindex); boolremoveRange(intindex,intcount); intlength()const; voidenqueue(Tvalue); Tdequeue(); voidpush(Tvalue); Tpop(); boolgetRange(intindex,intcount,Troop &output); voidreverse(); boolswap(intindex0,intindex1); Ttake(intindex); voidrollForward(intoffset); voidrollBackward(intoffset); booloperator==(constTroop&input)const; booloperator!=(constTroop&input)const; boolhasError()const; virtualQStringtoString(); virtualintfromString(constQString&input); }; } #endif//VALUES_H
//formoreinformation,pleaseaccesshttp://www.one-lab.net #include"values.h" #include#include namespaceonelab { template boolTroop ::equals(constTroop &from,constTroop &to) { intlen=from.length(); if(len==to.length()) { for(intn=0;n Troop ::Troop() { mHasError=false; } template TTroop ::get(intindex) { mHasError=false; if(index>=0&&index boolTroop ::set(intindex,Tvalue) { if(index>=0&&index voidTroop ::append(Tvalue) { data.append(value); } template boolTroop ::insert(intindex,Tvalue) { if(index>=0&&index voidTroop ::appendRange(constQList &range) { data.append(range); } template boolTroop ::insertRange(intindex,constQList &range) { intlength=data.length(); if(index>=0&&index left=data.mid(0,index); QList right=data.mid(index,-1); left.append(range); data.clear(); data.append(left); data.append(right); returntrue; } returnfalse; } template voidTroop ::clear() { data.clear(); } template boolTroop ::remove(intindex) { if(index>=0&&index boolTroop ::removeRange(intindex,intcount) { if(count>0&&index>=0&&((index+count)<=data.length())) { for(intn=0;n intTroop ::length()const { returndata.length(); } template voidTroop ::enqueue(Tvalue) { data.insert(0,value); } template TTroop ::dequeue() { mHasError=false; intlength=data.length(); if(length>0) { Tb=data[length-1]; data.removeAt(length-1); returnb; } mHasError=true; return(T)0; } template voidTroop ::push(Tvalue) { data.append(value); } template TTroop ::pop() { returndequeue(); } template boolTroop ::getRange(intindex,intcount,Troop &output) { mHasError=true; if(count>0&&index>=0&&((index+count)<=data.length())) { output.data=data.mid(index,count); mHasError=false; } returnmHasError; } template voidTroop ::reverse() { intlength=data.length(); QList newData; for(intn=length-1;n>=0;n--) newData.append(data[n]); data.clear(); data=newData; } template boolTroop ::swap(intindex0,intindex1) { intlength=data.length(); if(index0>=0&&index0 =0&&index1 TTroop ::take(intindex) { mHasError=false; if(index>=0&&index voidTroop ::rollForward(intoffset) { if(offset>=data.length())return; QList left=data.mid(offset-1,-1); QList right=data.mid(0,offset-1); data.clear(); data.append(left); data.append(right); } template voidTroop ::rollBackward(intoffset) { if(offset>=data.length())return; QList left=data.mid(offset,-1); QList right=data.mid(0,offset); data.clear(); data.append(left); data.append(right); } template boolTroop ::operator!=(constTroop&input)const { return!equals(input,*this); } template boolTroop ::hasError()const { returnmHasError; } template QStringTroop ::toString() { returnQString(); } template intTroop ::fromString(constQString&) { return0; } template boolTroop ::operator==(constTroop&input)const { returnequals(input,*this); } } //formoreinformation,pleaseaccesshttp://www.one-lab.net packagenet.onelab; importjava.util.ArrayList; publicclassTroop implementsCloneable{ publicObjectclone(){ Troop obj=newTroop (); obj.data.addAll(data); returnobj; } privatebooleanmHasError=false; publicbooleanhasError(){returnmHasError;} publicArrayList data=newArrayList (); publicTget(intindex){ mHasError=false; if(index>=0&&index =0&&index =0&&index range){ data.addAll(range); } publicbooleaninsertRange(intindex,ArrayList range){ if(index>=0&&index =0&&index 0&&index>=0&&((index+count)<=data.size())) { for(intn=0;n 0) { Tb=data.get(length-1); data.remove(length-1); returnb; } mHasError=true; returnnull; } publicvoidpush(Tvalue){ data.add(value); } publicTpop(){ returndequeue(); } publicTroop getRange(intindex,intcount){ mHasError=true; Troop output=newTroop (); if(count>0&&index>=0&&((index+count)<=data.size())) { output.data.addAll(data.subList(index,index+count)); mHasError=false; } returnoutput; } publicvoidreverse(){ intlength=data.size(); ArrayList newData=newArrayList (); for(intn=length-1;n>=0;n--) newData.add(data.get(n)); data.clear(); data=newData; } booleanswap(intindex0,intindex1){ intlength=data.size(); if(index0>=0&&index0 =0&&index1 =0&&index =data.size())return; ArrayList left=newArrayList (); left.addAll(data.subList(offset-1,data.size())); ArrayList right=newArrayList (); right.addAll(data.subList(0,offset-1)); data.clear(); data.addAll(left); data.addAll(right); } voidrollBackward(intoffset) { if(offset>=data.size())return; ArrayList left=newArrayList (); left.addAll(data.subList(offset,data.size())); ArrayList right=newArrayList (); right.addAll(data.subList(0,offset)); data.clear(); data.addAll(left); data.addAll(right); } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!