X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=include%2Flibfirm%2Fadt%2Funionfind.h;h=b71d01651a3ed59f9f74bb3c5f934b2adc558104;hb=97bcf6f245eba55a1d3b25a7be543b367ab9c276;hp=5e1cb47d5cebc9e36ac35e0244ddd97edfcf42f3;hpb=972eca70744a2c9a0e0d472db4f62250cdeee01c;p=libfirm diff --git a/include/libfirm/adt/unionfind.h b/include/libfirm/adt/unionfind.h index 5e1cb47d5..b71d01651 100644 --- a/include/libfirm/adt/unionfind.h +++ b/include/libfirm/adt/unionfind.h @@ -21,29 +21,35 @@ * @file * @brief Union-Find datastructure * @author Matthias Braun - * @version $Id$ - * @summary - * Union-Find datastructure - * - * This implementation uses weighted sets and path compression which results - * in (nearly) O(n) complexity for n find and union operations */ #ifndef FIRM_ADT_UNIONFIND_H #define FIRM_ADT_UNIONFIND_H #include +#include "../begin.h" + +/** + * @ingroup adt + * @defgroup unionfind Union-Find + * Union-Find datastructure + * + * This implementation uses weighted sets and path compression which results + * in (nearly) O(n) complexity for n find and union operations + * @{ + */ + /** * Call this to initialize an array of @p count elements to be used by the * union find functions. * - * @param data The array (you have to allocate it yourself) - * @param from The first element that should be initialized - * @param to the index of the first element which is not initialized anymore + * @param data The array (you have to allocate it yourself) + * @param n_elems number of elements handled by the datastructure */ -static inline void uf_init(int* data, int from, int to) { - int i; - for(i = from; i < to; ++i) { +static inline void uf_init(int* data, size_t n_elems) +{ + size_t i; + for(i = 0; i < n_elems; ++i) { data[i] = -1; } } @@ -52,32 +58,34 @@ static inline void uf_init(int* data, int from, int to) { * Merge 2 sets (union operation). Note that you have to pass the * representatives of the sets and not just random elements * - * @param data The union find data - * @param set1 Representative of set1 - * @param set2 Representative of set2 - * @return 0 if the new union set is represented by set1, 1 if it is - * represented by set2 + * @param data The union find data + * @param set1 Representative of set1 + * @param set2 Representative of set2 + * @return the new representative of the set (which is set1 or set2) */ -static inline int uf_union(int* data, int set1, int set2) { - int d1 = data[set1]; - int d2 = data[set2]; +static inline int uf_union(int* data, int set1, int set2) +{ + int d1; + int d2; int newcount; if(set1 == set2) - return 0; + return set1; /* need 2 set representatives */ + d1 = data[set1]; + d2 = data[set2]; assert(d1 < 0 && d2 < 0); newcount = d1 + d2; if(d1 > d2) { data[set1] = set2; data[set2] = newcount; - return 1; + return set2; } else { data[set2] = set1; data[set1] = newcount; - return 0; + return set1; } } @@ -87,11 +95,12 @@ static inline int uf_union(int* data, int set1, int set2) { * the same/different representatives, then the elements are in the * the same/different sets. * - * @param data The union find data - * @param e The element - * @return The representative of the set that contains @p e + * @param data The union find data + * @param e The element + * @return The representative of the set that contains @p e */ -static inline int uf_find(int* data, int e) { +static inline int uf_find(int* data, int e) +{ /* go through list to find representative */ int repr = e; while(data[repr] >= 0) { @@ -108,4 +117,8 @@ static inline int uf_find(int* data, int e) { return repr; } -#endif /* FIRM_ADT_UNIONFIND_H */ +/** @} */ + +#include "../end.h" + +#endif