JAVA ʵ Դ ë
Import java.util .*;
Public class XYObjArray
(
Int m_nSize;
Int m_nGrowBy;
Int m_nSort;
Object [] m_pObj;
Public static int nSearchThreshold = 8;
Public static int nSortThreshold = 8;
Static Random randomGen = new Random (new Date (). GetTime ());
Static public final int GetRandomInt (int nMax)
(
Synchronized (randomGen)
(
Return randomGen.nextInt (nMax);
)
)
Public XYObjArray ()
(
M_nSize = 0;
M_nGrowBy = 64;
M_nSort = 0;
M_pObj = null;
)
Public XYObjArray (pObj Object [], int nGrowBy)
(
M_nSize = 0;
M_nGrowBy = 64;
M_nSort = 0;
M_pObj = null;
SetSize (pObj.length, nGrowBy);
For (int i = 0; i <m_nSize; i + +) m_pObj = pObj [i] [i];
)
Public final synchronized void SetSort (int nSort)
(
If (nSort! = M_nSort) Sort (nSort);
)
Public final synchronized int GetSize ()
(
Return m_nSize;
)
Public final synchronized void SetSize (int nSize, int nGrowBy)
(
If (nSize <0 | | nGrowBy <= 0)
(
Return;
)
If (nSize> m_nSize) m_nSort = 0;
M_nGrowBy = nGrowBy;
Int nStoreSize m_pObj = == null? 0: m_pObj.length;
If (nSize> = (nStoreSize-m_nGrowBy) & & nSize <= nStoreSize)
(
M_nSize nSize =;
Return;
)
Int nMin nSize => m_nSize? M_nSize: nSize;
M_nSize nSize =;
PObj Object [] = new Object [(m_nSize / m_nGrowBy +1) * m_nGrowBy];
If (nMin> 0)
(
System.arraycopy (m_pObj, 0, pObj, 0, nMin);
)
M_pObj = pObj;
)
Public final synchronized Object [] GetDataPtr ()
(
Return m_pObj;
)
Public final synchronized int Add (Object obj)
(
Int nSort = m_nSort;
Int nIndex = Locate (obj);
Insert (nIndex, obj);
M_nSort = nSort;
Return nIndex;
)
Public final synchronized int Locate (Object obj)
(
Int nIndex = -1;
If (m_nSort == 0 | | m_nSize == 0) = m_nSize nIndex;
Else
(
Comparable ref = (Comparable) obj;
Int nMin, nMax, nMid;
NMin = 0;
NMax = m_nSize-1;
While (nMax-nMin> 1)
(
NMid = (nMin nMax +) / 2;
If (m_nSort> 0)
(
If (ref.compareTo (m_pObj [nMid]) <0) = nMid nMax;
Else nMin = nMid;
)
Else
(
If (ref.compareTo (m_pObj [nMid])> 0) = nMid nMax;
Else nMin = nMid;
)
)
If (m_nSort> 0)
(
If (ref.compareTo (m_pObj [nMin]) <0) = nMin nIndex;
Else if (ref.compareTo (m_pObj [nMax]) <0) = nMax nIndex;
NMax else nIndex = +1;
)
Else
(
If (ref.compareTo (m_pObj [nMin])> 0) = nMin nIndex;
Else if (ref.compareTo (m_pObj [nMax])> 0) = nMax nIndex;
NMax else nIndex = +1;
)
)
Return nIndex;
)
Public final synchronized int Find (Object obj)
(
If (m_pObj == null) return -1;
Comparable ref = (Comparable) obj;
If (m_nSort == 0 | | m_nSize <nSearchThreshold)
(
For (int i = 0; i <m_nSize; i + +)
(
If (ref.compareTo (m_pObj [i]) == 0) return i;
)
Return -1;
)
If (ref.compareTo (m_pObj [0]) == 0) return 0;
Else if (ref.compareTo (m_pObj [m_nSize-1]) == 0) return m_nSize-1;
Int nMid, nMin = 0, = m_nSize nMax-1;
While (nMax-nMin> 1)
(
NMid = (nMin nMax +) / 2;
If (ref.compareTo (m_pObj [nMid]) == 0) return nMid;
If (m_nSort> 0)
(
If (ref.compareTo (m_pObj [nMid])> 0) = nMid nMin;
Else nMax = nMid;
)
Else
(
If (ref.compareTo (m_pObj [nMid]) <0) = nMid nMin;
Else nMax = nMid;
)
)
Return -1;
)
Public final synchronized int Insert (int nIndex, Object obj)
(
If (nIndex <0 | | nIndex> m_nSize)
(
Return -1;
)
M_nSort = 0;
If (nIndex == m_nSize & m_pObj! = Null & nIndex <m_pObj.length)
(
M_pObj [nIndex] = obj;
)
Else
(
Int nStoreSize m_pObj = == null? M_nGrowBy: (m_nSize <m_pObj.length? M_pObj.length: (m_nGrowBy + m_pObj.length));
PObj Object [] = new Object [nStoreSize];
PObj [nIndex] = obj;
If (m_pObj! = Null)
(
If (nIndex> 0) System.arraycopy (m_pObj, 0, pObj, 0, nIndex);
If (nIndex <m_nSize) System.arraycopy (m_pObj, nIndex, pObj, nIndex +1, m_nSize-nIndex);
)
M_pObj = pObj;
)
M_nSize + +;
Return nIndex;
)
Public final synchronized int Remove (int nIndex)
(
If (nIndex <0 | | nIndex> = m_nSize)
(
Return -1;
)
Int nSegmentSize = m_nSize-nIndex-1;
If (nSegmentSize> 0)
(
PObj Object [] = new Object [nSegmentSize];
System.arraycopy (m_pObj, nIndex +1, pObj, 0, nSegmentSize);
System.arraycopy (pObj, 0, m_pObj, nIndex, nSegmentSize);
)
M_nSize -;
Return m_nSize;
)
Public final synchronized Object GetAt (int nIndex)
(
If (nIndex <0 | | nIndex> = m_nSize)
(
Return null;
)
Return m_pObj [nIndex];
)
Public final synchronized int SetAt (int nIndex, Object obj)
(
If (nIndex <0 | | nIndex> = m_nSize)
(
Return -1;
)
M_nSort = 0;
M_pObj [nIndex] = obj;
Return nIndex;
)
Public final synchronized void Push (Object obj)
(
Insert (m_nSize, obj);
)
Public final synchronized Object Pop ()
(
If (m_nSize == 0) return null;
M_nSize -;
Return m_pObj [m_nSize];
)
Public final synchronized void Sort (int nSort)
(
If (m_nSize == 0)
(
M_nSort = nSort;
Return;
)
Else if (nSort> 0 & & m_nSort> 0 | | nSort <0 & & m_nSort <0 | | nSort == 0)
(
Return;
)
PObj Object [] = new Object [m_pObj.length];
PIndex int [] = new int [m_nSize];
For (int i = 0; i <m_nSize; i + +) pIndex [i] = i;
SortIndex (pIndex, nSort);
For (int j = 0; j <m_nSize; j + +) pObj m_pObj [j] = [pIndex [j]];
M_pObj = pObj;
M_nSort = nSort;
)
Public final synchronized void SortIndex (pIndex int [], int nSort)
(
If (m_nSize == 0 | | nSort == 0)
(
Return;
)
Int i, j, k;
Int nMin, nMax;
Int nTemp1, nTemp2, nSwap;
XYObjArray theStack = new XYObjArray ();
PRefIndex int [] = new int [m_nSize];
For (i = 0; i <m_nSize; i + +) pRefIndex [i] = i;
TheStack.Push (new Integer (0));
TheStack.Push (new Integer (m_nSize-1));
While (theStack.GetSize ()> 0)
(
NMax = ((Integer) theStack.Pop ()). IntValue ();
NMin = ((Integer) theStack.Pop ()). IntValue ();
If (nMax-nMin <nSortThreshold)
(
For (i = nMin +1; i <= nMax; i + +)
(
PIndex nTemp1 = [i];
PRefIndex nTemp2 = [i];
J = i-1;
Comparable ref = (Comparable) m_pObj [nTemp1];
If (nSort> 0)
(
While (j> = nMin & & (ref.compareTo (m_pObj [pIndex [j]]) <0 | | pRefIndex [j]> nTemp2 & ref.compareTo (m_pObj [pIndex [j ]])== 0)) j -;
)
Else
(
While (j> = nMin & & (ref.compareTo (m_pObj [pIndex [j]])> 0 | | pRefIndex [j]> nTemp2 & ref.compareTo (m_pObj [pIndex [j ]])== 0)) j -;
)
If (j +1 <i)
(
For (k = i; k> j +1; k -)
(
PIndex pIndex [k] = [k-1];
PRefIndex pRefIndex [k] = [k-1];
)
PIndex [j +1] = nTemp1;
PRefIndex [j +1] = nTemp2;
)
)
)
Else
(
I = nMin + (GetRandomInt (0×7FFFFFFF)% (nMax-nMin)) +1;
PIndex nSwap = [nMin]; pIndex pIndex [nMin] = [i] [i] = pIndex nSwap;
PRefIndex nSwap = [nMin]; pRefIndex pRefIndex [nMin] = [i] [i] = pRefIndex nSwap;
Comparable ref = (Comparable) m_pObj [pIndex [nMin]];
If (nSort> 0)
(
If (ref.compareTo (m_pObj [pIndex [nMin +1]]) <0 | | pRefIndex [nMin +1]> pRefIndex [nMin] & & ref.compareTo (m_pObj [pIndex [nMin +1 ]])== 0)
(
PIndex nSwap = [nMin]; pIndex pIndex [nMin] = [nMin +1]; pIndex [nMin +1] = nSwap;
PRefIndex nSwap = [nMin]; pRefIndex pRefIndex [nMin] = [nMin +1]; pRefIndex [nMin +1] = nSwap;
Ref = (Comparable) m_pObj [pIndex [nMin]];
)
If (ref.compareTo (m_pObj [pIndex [nMax]])> 0 | | pRefIndex [nMin]> pRefIndex [nMax] & & ref.compareTo (m_pObj [pIndex [nMax ]])== 0)
(
PIndex nSwap = [nMin]; pIndex pIndex [nMin] = [nMax]; pIndex [nMax] = nSwap;
PRefIndex nSwap = [nMin]; pRefIndex pRefIndex [nMin] = [nMax]; pRefIndex [nMax] = nSwap;
Ref = (Comparable) m_pObj [pIndex [nMin]];
)
If (ref.compareTo (m_pObj [pIndex [nMin +1]]) <0 | | pRefIndex [nMin +1]> pRefIndex [nMin] & & ref.compareTo (m_pObj [pIndex [nMin +1 ]])== 0)
(
PIndex nSwap = [nMin]; pIndex pIndex [nMin] = [nMin +1]; pIndex [nMin +1] = nSwap;
PRefIndex nSwap = [nMin]; pRefIndex pRefIndex [nMin] = [nMin +1]; pRefIndex [nMin +1] = nSwap;
Ref = (Comparable) m_pObj [pIndex [nMin]];
)
)
Else
(
If (ref.compareTo (m_pObj [pIndex [nMin +1])> 0 | | pRefIndex [nMin +1]> pRefIndex [nMin] & & ref.compareTo (m_pObj [pIndex [nMin +1 ]])== 0)
(
PIndex nSwap = [nMin]; pIndex pIndex [nMin] = [nMin +1]; pIndex [nMin +1] = nSwap;
PRefIndex nSwap = [nMin]; pRefIndex pRefIndex [nMin] = [nMin +1]; pRefIndex [nMin +1] = nSwap;
Ref = (Comparable) m_pObj [pIndex [nMin]];
)
If (ref.compareTo (m_pObj [pIndex [nMax]]) <0 | | pRefIndex [nMin]> pRefIndex [nMax] & & ref.compareTo (m_pObj [pIndex [nMax ]])== 0)
(
PIndex nSwap = [nMin]; pIndex pIndex [nMin] = [nMax]; pIndex [nMax] = nSwap;
PRefIndex nSwap = [nMin]; pRefIndex pRefIndex [nMin] = [nMax]; pRefIndex [nMax] = nSwap;
Ref = (Comparable) m_pObj [pIndex [nMin]];
)
If (ref.compareTo (m_pObj [pIndex [nMin +1])> 0 | | pRefIndex [nMin +1]> pRefIndex [nMin] & & ref.compareTo (m_pObj [pIndex [nMin +1 ]])== 0)
(
PIndex nSwap = [nMin]; pIndex pIndex [nMin] = [nMin +1]; pIndex [nMin +1] = nSwap;
PRefIndex nSwap = [nMin]; pRefIndex pRefIndex [nMin] = [nMin +1]; pRefIndex [nMin +1] = nSwap;
Ref = (Comparable) m_pObj [pIndex [nMin]];
)
)
NMin = i +1;
J = nMax;
While (true)
(
If (nSort> 0)
(
While
(
Ref.compareTo (m_pObj [pIndex [i])> 0 | |
PRefIndex [i] <pRefIndex [nMin] & & ref.compareTo (m_pObj [pIndex [i ]])== 0
) I + +;
While
(
Ref.compareTo (m_pObj [pIndex [j]]) <0 | |
PRefIndex [nMin] <pRefIndex [j] & & ref.compareTo (m_pObj [pIndex [j ]])== 0
) J -;
)
Else
(
While
(
Ref.compareTo (m_pObj [pIndex [i]]) <0 | |
PRefIndex [i] <pRefIndex [nMin] & & ref.compareTo (m_pObj [pIndex [i ]])== 0
) I + +;
While
(
Ref.compareTo (m_pObj [pIndex [j]])> 0 | |
PRefIndex [nMin] <pRefIndex [j] & & ref.compareTo (m_pObj [pIndex [j ]])== 0
) J -;
)
If (i> j)
(
PIndex nSwap = [nMin]; pIndex pIndex [nMin] = [j]; pIndex [j] = nSwap;
PRefIndex nSwap = [nMin]; pRefIndex pRefIndex [nMin] = [j]; pRefIndex [j] = nSwap;
Break;
)
PIndex nSwap = [i]; pIndex [i] [j] = pIndex; pIndex [j] = nSwap;
PRefIndex nSwap = [i]; pRefIndex [i] [j] = pRefIndex; pRefIndex [j] = nSwap;
)
If (nMin <j-1)
(
TheStack.Add (new Integer (nMin));
TheStack.Add (new Integer (j-1));
)
If (j +1 <nMax)
(
TheStack.Add (new Integer (j +1));
TheStack.Add (new Integer (nMax));
)
)
)
)
)






