Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

cs9tmf

nth_element

This topic is 5540 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Has anyone implemented the C++ function nth_element in standard C? If so can i have a copy please Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
STL implementations come with source. Look there and convert it to C as you see best.

For example, here''s some of the functions STLport uses:

  
template <class _RandomAccessIter>
void nth_element(_RandomAccessIter __first, _RandomAccessIter __nth,
_RandomAccessIter __last) {
_STLP_DEBUG_CHECK(__check_range(__first, __nth))
_STLP_DEBUG_CHECK(__check_range(__nth, __last))
__nth_element(__first, __nth, __last, _STLP_VALUE_TYPE(__first, _RandomAccessIter),
__less(_STLP_VALUE_TYPE(__first, _RandomAccessIter)));
}


template <class _RandomAccessIter, class _Tp, class _Compare>
void __nth_element(_RandomAccessIter __first, _RandomAccessIter __nth,
_RandomAccessIter __last, _Tp*, _Compare __comp) {
while (__last - __first > 3) {
_RandomAccessIter __cut =
__unguarded_partition(__first, __last,
_Tp(__median(*__first,
*(__first + (__last - __first)/2),
*(__last - 1),
__comp)),
__comp);
if (__cut <= __nth)
__first = __cut;
else
__last = __cut;
}
__insertion_sort(__first, __last, __comp);
}


template <class _RandomAccessIter, class _Tp, class _Compare>
_RandomAccessIter __unguarded_partition(_RandomAccessIter __first,
_RandomAccessIter __last,
_Tp __pivot, _Compare __comp)
{
while (true) {
while (__comp(*__first, __pivot))
++__first;
--__last;
while (__comp(__pivot, *__last))
--__last;
if (!(__first < __last))
return __first;
iter_swap(__first, __last);
++__first;
}
}


template <class _RandomAccessIter, class _Compare>
void __insertion_sort(_RandomAccessIter __first,
_RandomAccessIter __last, _Compare __comp) {
if (__first == __last) return;
for (_RandomAccessIter __i = __first + 1; __i != __last; ++__i)
__linear_insert(__first, __i, *__i, __comp); //*TY 12/26/1998 - supply *__i as __val

}


template <class _RandomAccessIter, class _Tp, class _Compare>
inline void __linear_insert(_RandomAccessIter __first,
_RandomAccessIter __last, _Tp __val, _Compare __comp) {
//*TY 12/26/1998 - added __val as a paramter

// _Tp __val = *__last; //*TY 12/26/1998 - __val supplied by caller

if (__comp(__val, *__first)) {
copy_backward(__first, __last, __last + 1);
*__first = __val;
}
else
__unguarded_linear_insert(__last, __val, __comp);
}

template <class _RandomAccessIter, class _Tp, class _Compare>
void __unguarded_linear_insert(_RandomAccessIter __last, _Tp __val,
_Compare __comp) {
_RandomAccessIter __next = __last;
--__next;
while (__comp(__val, *__next)) {
*__last = *__next;
__last = __next;
--__next;
}
*__last = __val;
}


inline _Tp* copy_backward(const _Tp* __first, const _Tp* __last, _Tp* __result)
{ return (_Tp*)__copy_trivial_backward(__first, __last, __result); }

inline void*
__copy_trivial_backward(const void* __first, const void* __last, void* __result) {
const ptrdiff_t _Num = (const char*)__last - (const char*)__first;
return (_Num > 0) ? memmove((char*)__result - _Num, __first, _Num) : __result ;
}

Share this post


Link to post
Share on other sites
Whooeeee I would hate to pick up that programmers job

-UltimaX-

"You wished for a white christmas... Now go shovel your wishes!"

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Just remove all of the template declarations, insert some typedef''s, and fill in the utility functions as needed. Also drop the STLport macros and replace them with your own. Some additional checks will probably be needed to ensure that the _Compare function pointer is non-zero. Voila, C code.

Share this post


Link to post
Share on other sites
You make it sound so easy!

I''m not a competant C programmer yet so would anyone like to take up the challenge?

Would be a massive help

Cheers

Share this post


Link to post
Share on other sites
why don''t you just sort the elements?

"No lies of sugar can sweeten the sournes of reality"

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!