Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

unionfind.h

00001 /*
00002   File: unionfind.h
00003 
00004   Copyright(C) C. Kotterink, Computed Graphics
00005 */
00006 #ifndef UNION_FIND_H
00007 #define UNION_FIND_H
00008 
00012 template<typename T>
00013 class unionfind
00014 {
00015 public:
00017     unionfind(unsigned int s) : size(s) {
00018         array = new T[size];
00019         for (int i=0;i<size;++i) array[i] = i;
00020     }
00021 
00023     ~unionfind() {
00024         delete[] array;
00025     }
00026 
00028     T find(T a) {
00029         T c = array[a];
00030         while(c != a) c = array[a];
00031         return c;
00032     }
00033 
00035     void join(T a, T b) {
00036         link(find(a), find(b));
00037     }
00038 
00039 protected:
00041     void link(T a, T b) {
00042         if (a < b) {
00043             array[b] = a;
00044         } else if (a > b) {
00045             array[a] = b;
00046         }
00047     }
00048 
00050     unsigned int size;
00051 
00053     T *array;
00054 };
00055 #endif

This documentation was generated using doxygen. If you have any comments or additions please mail me.