示例#1
0
文件: list.c 项目: haifenghuang/ooc-1
/**
 * Быстрая сортировка элементов списка по возрастанию значения
 * @param _self Список
 * @param low Индекс сортировки
 * @param hight Индекс сортировки
 */
static void _QSort( void* _self, uint32_t low, uint32_t high )
{
	struct List* self = (struct List*)_self;
	uint32_t i = low;
	uint32_t j = high;
	uint32_t x = self->buf[ ( low + high ) / 2 ].id;

	do
	{
		while( self->buf[ i ].id < x ) i++;
		while( self->buf[ j ].id > x ) j--;
		if( i <= j )
		{
			struct Node tmp = self->buf[ i ];
			self->buf[ i ] = self->buf[ j ];
			self->buf[ j ] = tmp;
			i++;
			j--;
		}
	} while( i <= j );

	if( low < j )
	{
		_QSort( self, low, j );
	}
	if( i < high )
	{
		_QSort( self, i, high );
	}
}
示例#2
0
文件: list.c 项目: haifenghuang/ooc-1
/**
 * Сортировка по значению (по возрастанию)
 * @param _self Список
 */
static void _Sort( void* _self )
{
	struct List* self = (struct List*)_self;

	if( self->count )
	{
		_QSort( self, 0, self->count - 1 );
	}
}
	void _QSort( std::vector<SLinkListNode *>& v, int left, int right,
		bool (*CompFunc)( CElementType*, CElementType* ) )
	{
		// qsort
		int i, last;
		if( left >= right )
			return;

//		swap( v, left, (left + right) / 2 );
		swap( v[left], v[(left + right) / 2] );
		last = left;
		for( i = left+1; i <= right; i++ )
		{
			if( (*CompFunc)( &(v[i]->element), &(v[left]->element) ) )
//				swap( v, ++last, i );
				swap( v[++last], v[i] );
		}
//		swap( v, left, last );
		swap( v[left], v[last] );
		_QSort( v, left, last-1, CompFunc );
		_QSort( v, last+1, right, CompFunc );
	}
	void Sort( bool (*CompFunc)( CElementType*, CElementType* ) )
	{
		m_vecpTempList.resize( 0 );
        
/*		LinkListIterator itr;
		itr = Begin();

		// move all the active elements to temp list
		while( itr != End() )
		{
			m_vecpTempList.push_back( &(*itr) );
			itr++;
		}*/

		SLinkListNode *p;
		for( p = m_pActiveList; p != NULL; p = p->m_pNext )
		{
			m_vecpTempList.push_back( p );
		}

		_QSort( m_vecpTempList, 0, m_vecpTempList.size() - 1, CompFunc);

//		sort( m_vecpTempList.begin(), m_vecpTempList.end(), CompFunc );


		/// re-link objects to active list
		m_pActiveList = m_vecpTempList[0];
		m_pActiveList->m_pPrev = NULL;

		SLinkListNode *pPrev = m_pActiveList;
		int i, num = m_vecpTempList.size();
		for( i=1; i<num; i++ )
		{
			m_vecpTempList[i]->m_pPrev = pPrev;
			pPrev->m_pNext = m_vecpTempList[i];
			pPrev = pPrev->m_pNext;
		}
		pPrev->m_pNext = NULL;


	}