00001 #ifndef INDEX_H
00002 #define INDEX_H
00003
00004 #ifndef _GNU_SOURCE
00005 #define _GNU_SOURCE // Needed before <cstring> for ffsl().
00006 #endif
00007
00008 #ifndef USE_PCH
00009 #include <iostream>
00010 #include <cstring>
00011 #include "debug.h"
00012 #endif
00013
00014
00015
00016
00017
00018
00019
00020 template<typename INDEX_POOL_TYPE>
00021 class Index {
00022 public:
00023 static int const max_index = 8 * sizeof(long);
00024
00025 private:
00026 static long S_index_pool;
00027 int const M_index;
00028
00029 protected:
00030
00031 Index(void) : M_index(ffsl(S_index_pool) - 1) { ASSERT(M_index != -1); S_index_pool &= ~(1UL << M_index); }
00032
00033 ~Index(void) { S_index_pool |= (1 << M_index); }
00034
00035 public:
00036
00037 int index(void) const { return M_index; }
00038
00039
00040 static int size(void) { int r = 0; unsigned long i = (~static_cast<unsigned long>(S_index_pool)) << 1; while(i >>= 1) ++r; return r; }
00041
00042 private:
00043 static void sanity_check(void)
00044 {
00045
00046 (void)static_cast<Index*>((INDEX_POOL_TYPE*)0);
00047 }
00048 };
00049
00050 template<class INDEX_POOL_TYPE>
00051 long Index<INDEX_POOL_TYPE>::S_index_pool = ~0;
00052
00053 #endif // INDEX_H