69 void CSG_Index::_On_Construction(
void)
112 if( _Set_Array(nValues) && _Set_Index(&Compare) )
127 Create(nValues, pCompare);
133 if( pCompare && _Set_Array(nValues) && _Set_Index(pCompare) )
152 int *m_Values;
bool m_Ascending;
154 CSG_Index_Compare_Int(
int *Values,
bool Ascending) : m_Values(Values), m_Ascending(Ascending) {}
158 sLong a = m_Ascending ? _a : _b;
159 sLong b = m_Ascending ? _b : _a;
161 return( m_Values[a] - m_Values[b] );
170 Create(nValues, Values, bAscending);
176 CSG_Index_Compare_Int Compare(Values, bAscending);
178 return(
Create(nValues, &Compare) );
190 double *m_Values;
bool m_Ascending;
192 CSG_Index_Compare_Double(
double *Values,
bool Ascending) : m_Values(Values), m_Ascending(Ascending) {}
196 sLong a = m_Ascending ? _a : _b;
197 sLong b = m_Ascending ? _b : _a;
199 double d = m_Values[a] - m_Values[b];
201 return( d < 0. ? -1 : d > 0. ? 1 : 0 );
210 Create(nValues, Values, bAscending);
216 CSG_Index_Compare_Double Compare(Values, bAscending);
218 return(
Create(nValues, &Compare) );
232 CSG_Index_Compare_Function(
TSG_PFNC_Compare Function) : m_Function(Function) {}
236 return( m_Function(_a, _b) );
245 Create(nValues, fCompare);
251 CSG_Index_Compare_Function Compare(fCompare);
253 return(
Create(nValues, &Compare) );
264 m_bProgress = bProgress;
270 if( Position < 0 || Position >= m_nValues - 1 )
272 return( _Set_Array(m_nValues + 1) );
275 if( _Set_Array(m_nValues + 1) )
277 for(
sLong i=Position, Value=m_nValues-1; i<m_nValues; i++)
279 sLong v = m_Index[i]; m_Index[i] = Value; Value = v;
291 if( Position < 0 || Position >= m_nValues - 1 )
293 return( _Set_Array(m_nValues - 1) );
296 sLong Value = m_Index[Position];
298 for(
sLong i=Position; i<m_nValues-1; i++)
300 m_Index[i] = m_Index[i + 1];
303 m_Index[m_nValues - 1] = Value;
305 return( _Set_Array(m_nValues - 1) );
309 bool CSG_Index::_Set_Array(
sLong nValues)
316 if( nValues == m_nValues )
321 if( m_nValues > nValues )
323 for(
sLong i=0, j=nValues; i<nValues && j<m_nValues; i++)
325 if( m_Index[i] >= nValues )
327 while( m_Index[j] >= nValues )
337 sLong c = m_Index[i]; m_Index[i] = m_Index[j]; m_Index[j] = c;
351 if( m_nValues < nValues )
353 for(
sLong i=m_nValues; i<nValues; i++)
370 #define SG_INDEX_SWAP(a, b) {itemp=(a);(a)=(b);(b)=itemp;}
373 bool CSG_Index::_Set_Index(CSG_Index_Compare *pCompare)
387 sLong nProcessed = 0;
403 for(j=l+1; j<=ir; j++)
405 a = indxt = m_Index[j];
407 for(i=j-1; i>=0; i--)
409 if( pCompare->Compare(m_Index[i], a) <= 0 )
414 m_Index[i + 1] = m_Index[i];
417 m_Index[i + 1] = indxt;
425 ir = istack[jstack--];
426 l = istack[jstack--];
433 if( pCompare->Compare(m_Index[l + 1], m_Index[ir]) > 0 )
436 if( pCompare->Compare(m_Index[l ], m_Index[ir]) > 0 )
439 if( pCompare->Compare(m_Index[l + 1], m_Index[l ]) > 0 )
444 a = indxt = m_Index[l];
448 do i++;
while( pCompare->Compare(m_Index[i], a) < 0 );
449 do j--;
while( pCompare->Compare(m_Index[j], a) > 0 );
459 m_Index[l] = m_Index[j];
463 if( jstack >= nstack )
468 if( ir - i + 1 >= j - l )
470 istack[jstack ] = ir;
471 istack[jstack - 1] = i;
476 istack[jstack ] = j - 1;
477 istack[jstack - 1] = l;
501 m_pLeaf[0] = m_pLeaf[1] = NULL;
556 size_t CSG_PriorityQueue::_Insert_Position(CSG_PriorityQueueItem *pItem)
564 size_t b = m_nItems - 1;
566 if( pItem->Compare(m_Items[a]) < 0 )
571 if( pItem->Compare(m_Items[b]) > 0 )
576 for(
size_t d=(b-a)/2 ; d>0; d/=2)
580 if( pItem->Compare(m_Items[i]) > 0 )
582 a = a < i ? i : a + 1;
586 b = b > i ? i : b - 1;
590 for(
size_t i=a; i<=b; i++)
592 if( pItem->Compare(m_Items[i]) < 0 )
604 if( m_Items && m_nItems < m_maxSize )
606 size_t Position = _Insert_Position(pItem);
608 memmove(m_Items + Position + 1, m_Items + Position,
sizeof(
CSG_PriorityQueueItem *) * (m_nItems - Position));
610 m_Items[Position] = pItem;
616 size_t Divide = m_maxSize / 2;
621 m_pLeaf[0]->m_nItems = Divide;
622 m_pLeaf[1]->m_nItems = m_maxSize - Divide;
625 memcpy(m_pLeaf[1]->m_Items, m_Items + m_pLeaf[0]->m_nItems, m_pLeaf[1]->m_nItems *
sizeof(
CSG_PriorityQueueItem *));
633 m_pLeaf[1]->
Add(pItem);
637 m_pLeaf[0]->
Add(pItem);
658 return( m_Items[m_nItems] );
663 if( m_pLeaf[1]->m_nItems == 0 )
670 m_Items = pLeaf->m_Items;
671 m_pLeaf[0] = pLeaf->m_pLeaf[0];
672 m_pLeaf[1] = pLeaf->m_pLeaf[1];
674 pLeaf->m_Items = NULL;
675 pLeaf->m_pLeaf[0] = NULL;
676 pLeaf->m_pLeaf[1] = NULL;