Path: blob/devel/elmergrid/src/metis-5.1.0/GKlib/gk_mksort.h
3206 views
/*!1\file gk_mksort.h2\brief Templates for the qsort routine34\date Started 3/28/075\author George6\version\verbatim $Id: gk_mksort.h 10711 2011-08-31 22:23:04Z karypis $ \endverbatim7*/8910#ifndef _GK_MKSORT_H_11#define _GK_MKSORT_H_1213/* $Id: gk_mksort.h 10711 2011-08-31 22:23:04Z karypis $14* Adopted from GNU glibc by Mjt.15* See stdlib/qsort.c in glibc */1617/* Copyright (C) 1991, 1992, 1996, 1997, 1999 Free Software Foundation, Inc.18This file is part of the GNU C Library.19Written by Douglas C. Schmidt ([email protected]).2021The GNU C Library is free software; you can redistribute it and/or22modify it under the terms of the GNU Lesser General Public23License as published by the Free Software Foundation; either24version 2.1 of the License, or (at your option) any later version.2526The GNU C Library is distributed in the hope that it will be useful,27but WITHOUT ANY WARRANTY; without even the implied warranty of28MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU29Lesser General Public License for more details.3031You should have received a copy of the GNU Lesser General Public32License along with the GNU C Library; if not, write to the Free33Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA3402111-1307 USA. */3536/* in-line qsort implementation. Differs from traditional qsort() routine37* in that it is a macro, not a function, and instead of passing an address38* of a comparision routine to the function, it is possible to inline39* comparision routine, thus speed up sorting alot.40*41* Usage:42* #include "iqsort.h"43* #define islt(a,b) (strcmp((*a),(*b))<0)44* char *arr[];45* int n;46* GKQSORT(char*, arr, n, islt);47*48* The "prototype" and 4 arguments are:49* GKQSORT(TYPE,BASE,NELT,ISLT)50* 1) type of each element, TYPE,51* 2) address of the beginning of the array, of type TYPE*,52* 3) number of elements in the array, and53* 4) comparision routine.54* Array pointer and number of elements are referenced only once.55* This is similar to a call56* qsort(BASE,NELT,sizeof(TYPE),ISLT)57* with the difference in last parameter.58* Note the islt macro/routine (it receives pointers to two elements):59* the only condition of interest is whenever one element is less than60* another, no other conditions (greather than, equal to etc) are tested.61* So, for example, to define integer sort, use:62* #define islt(a,b) ((*a)<(*b))63* GKQSORT(int, arr, n, islt)64*65* The macro could be used to implement a sorting function (see examples66* below), or to implement the sorting algorithm inline. That is, either67* create a sorting function and use it whenever you want to sort something,68* or use GKQSORT() macro directly instead a call to such routine. Note that69* the macro expands to quite some code (compiled size of int qsort on x8670* is about 700..800 bytes).71*72* Using this macro directly it isn't possible to implement traditional73* qsort() routine, because the macro assumes sizeof(element) == sizeof(TYPE),74* while qsort() allows element size to be different.75*76* Several ready-to-use examples:77*78* Sorting array of integers:79* void int_qsort(int *arr, unsigned n) {80* #define int_lt(a,b) ((*a)<(*b))81* GKQSORT(int, arr, n, int_lt);82* }83*84* Sorting array of string pointers:85* void str_qsort(char *arr[], unsigned n) {86* #define str_lt(a,b) (strcmp((*a),(*b)) < 0)87* GKQSORT(char*, arr, n, str_lt);88* }89*90* Sorting array of structures:91*92* struct elt {93* int key;94* ...95* };96* void elt_qsort(struct elt *arr, unsigned n) {97* #define elt_lt(a,b) ((a)->key < (b)->key)98* GKQSORT(struct elt, arr, n, elt_lt);99* }100*101* And so on.102*/103104/* Swap two items pointed to by A and B using temporary buffer t. */105#define _GKQSORT_SWAP(a, b, t) ((void)((t = *a), (*a = *b), (*b = t)))106107/* Discontinue quicksort algorithm when partition gets below this size.108This particular magic number was chosen to work best on a Sun 4/260. */109#define _GKQSORT_MAX_THRESH 4110111/* The next 4 #defines implement a very fast in-line stack abstraction. */112#define _GKQSORT_STACK_SIZE (8 * sizeof(size_t))113#define _GKQSORT_PUSH(top, low, high) (((top->_lo = (low)), (top->_hi = (high)), ++top))114#define _GKQSORT_POP(low, high, top) ((--top, (low = top->_lo), (high = top->_hi)))115#define _GKQSORT_STACK_NOT_EMPTY (_stack < _top)116117118/* The main code starts here... */119#define GK_MKQSORT(GKQSORT_TYPE,GKQSORT_BASE,GKQSORT_NELT,GKQSORT_LT) \120{ \121GKQSORT_TYPE *const _base = (GKQSORT_BASE); \122const size_t _elems = (GKQSORT_NELT); \123GKQSORT_TYPE _hold; \124\125if (_elems == 0) \126return; \127\128/* Don't declare two variables of type GKQSORT_TYPE in a single \129* statement: eg `TYPE a, b;', in case if TYPE is a pointer, \130* expands to `type* a, b;' wich isn't what we want. \131*/ \132\133if (_elems > _GKQSORT_MAX_THRESH) { \134GKQSORT_TYPE *_lo = _base; \135GKQSORT_TYPE *_hi = _lo + _elems - 1; \136struct { \137GKQSORT_TYPE *_hi; GKQSORT_TYPE *_lo; \138} _stack[_GKQSORT_STACK_SIZE], *_top = _stack + 1; \139\140while (_GKQSORT_STACK_NOT_EMPTY) { \141GKQSORT_TYPE *_left_ptr; GKQSORT_TYPE *_right_ptr; \142\143/* Select median value from among LO, MID, and HI. Rearrange \144LO and HI so the three values are sorted. This lowers the \145probability of picking a pathological pivot value and \146skips a comparison for both the LEFT_PTR and RIGHT_PTR in \147the while loops. */ \148\149GKQSORT_TYPE *_mid = _lo + ((_hi - _lo) >> 1); \150\151if (GKQSORT_LT (_mid, _lo)) \152_GKQSORT_SWAP (_mid, _lo, _hold); \153if (GKQSORT_LT (_hi, _mid)) \154_GKQSORT_SWAP (_mid, _hi, _hold); \155else \156goto _jump_over; \157if (GKQSORT_LT (_mid, _lo)) \158_GKQSORT_SWAP (_mid, _lo, _hold); \159_jump_over:; \160\161_left_ptr = _lo + 1; \162_right_ptr = _hi - 1; \163\164/* Here's the famous ``collapse the walls'' section of quicksort. \165Gotta like those tight inner loops! They are the main reason \166that this algorithm runs much faster than others. */ \167do { \168while (GKQSORT_LT (_left_ptr, _mid)) \169++_left_ptr; \170\171while (GKQSORT_LT (_mid, _right_ptr)) \172--_right_ptr; \173\174if (_left_ptr < _right_ptr) { \175_GKQSORT_SWAP (_left_ptr, _right_ptr, _hold); \176if (_mid == _left_ptr) \177_mid = _right_ptr; \178else if (_mid == _right_ptr) \179_mid = _left_ptr; \180++_left_ptr; \181--_right_ptr; \182} \183else if (_left_ptr == _right_ptr) { \184++_left_ptr; \185--_right_ptr; \186break; \187} \188} while (_left_ptr <= _right_ptr); \189\190/* Set up pointers for next iteration. First determine whether \191left and right partitions are below the threshold size. If so, \192ignore one or both. Otherwise, push the larger partition's \193bounds on the stack and continue sorting the smaller one. */ \194\195if (_right_ptr - _lo <= _GKQSORT_MAX_THRESH) { \196if (_hi - _left_ptr <= _GKQSORT_MAX_THRESH) \197/* Ignore both small partitions. */ \198_GKQSORT_POP (_lo, _hi, _top); \199else \200/* Ignore small left partition. */ \201_lo = _left_ptr; \202} \203else if (_hi - _left_ptr <= _GKQSORT_MAX_THRESH) \204/* Ignore small right partition. */ \205_hi = _right_ptr; \206else if (_right_ptr - _lo > _hi - _left_ptr) { \207/* Push larger left partition indices. */ \208_GKQSORT_PUSH (_top, _lo, _right_ptr); \209_lo = _left_ptr; \210} \211else { \212/* Push larger right partition indices. */ \213_GKQSORT_PUSH (_top, _left_ptr, _hi); \214_hi = _right_ptr; \215} \216} \217} \218\219/* Once the BASE array is partially sorted by quicksort the rest \220is completely sorted using insertion sort, since this is efficient \221for partitions below MAX_THRESH size. BASE points to the \222beginning of the array to sort, and END_PTR points at the very \223last element in the array (*not* one beyond it!). */ \224\225{ \226GKQSORT_TYPE *const _end_ptr = _base + _elems - 1; \227GKQSORT_TYPE *_tmp_ptr = _base; \228register GKQSORT_TYPE *_run_ptr; \229GKQSORT_TYPE *_thresh; \230\231_thresh = _base + _GKQSORT_MAX_THRESH; \232if (_thresh > _end_ptr) \233_thresh = _end_ptr; \234\235/* Find smallest element in first threshold and place it at the \236array's beginning. This is the smallest array element, \237and the operation speeds up insertion sort's inner loop. */ \238\239for (_run_ptr = _tmp_ptr + 1; _run_ptr <= _thresh; ++_run_ptr) \240if (GKQSORT_LT (_run_ptr, _tmp_ptr)) \241_tmp_ptr = _run_ptr; \242\243if (_tmp_ptr != _base) \244_GKQSORT_SWAP (_tmp_ptr, _base, _hold); \245\246/* Insertion sort, running from left-hand-side \247* up to right-hand-side. */ \248\249_run_ptr = _base + 1; \250while (++_run_ptr <= _end_ptr) { \251_tmp_ptr = _run_ptr - 1; \252while (GKQSORT_LT (_run_ptr, _tmp_ptr)) \253--_tmp_ptr; \254\255++_tmp_ptr; \256if (_tmp_ptr != _run_ptr) { \257GKQSORT_TYPE *_trav = _run_ptr + 1; \258while (--_trav >= _run_ptr) { \259GKQSORT_TYPE *_hi; GKQSORT_TYPE *_lo; \260_hold = *_trav; \261\262for (_hi = _lo = _trav; --_lo >= _tmp_ptr; _hi = _lo) \263*_hi = *_lo; \264*_hi = _hold; \265} \266} \267} \268} \269\270}271272#endif273274275