00001
00009 #ifndef _UTILS_H
00010 #define _UTILS_H
00011
00012 #include <bitset>
00013 #include <map>
00014 #include <string>
00015 #include <utility>
00016 #include <vector>
00017
00018
00019 #include "config.h"
00020
00021 namespace ace {
00022
00026 template<class T>
00027 struct select1st: public std::unary_function<T, typename T::first_type> {
00028 typename T::first_type operator()(const T& pair_arg) const {
00029 return pair_arg.first;
00030 }
00031 };
00032
00036 template<class T>
00037 struct select2nd: public std::unary_function<T, typename T::second_type> {
00038 typename T::second_type operator()(const T& pair_arg) const {
00039 return pair_arg.second;
00040 }
00041 };
00042
00047 template<typename _Type>
00048 inline bool ith_bit(_Type expr, size_t i) {
00049 return (expr >> i) & 1;
00050 }
00051
00057 template<typename _Type>
00058 inline _Type set_ith_bit(_Type expr, size_t i, bool on = true) {
00059 return expr | (static_cast<_Type>(on) << i);
00060 }
00061
00065 template<typename _Type>
00066 size_t bits_in(_Type expr) {
00067 size_t counter = 0;
00068 for ( size_t i = 0; i < sizeof(_Type)*constants::bits_per_char; ++i ) {
00069 counter += ith_bit<_Type>(expr, i);
00070 }
00071 return counter;
00072 }
00073
00076 template<typename _Type>
00077 inline _Type ff(void) {
00078 return ~ static_cast<_Type>(0);
00079 }
00080
00081
00086 std::string unsigned2str(size_t num);
00087
00093 std::string double2str(double num, unsigned precision = 0);
00094
00095 #ifdef _DEPENDENCY_MODE
00096
00100 typedef std::vector<std::vector<size_t> > sum_decompositions_t;
00101
00112 class SumDecompositions {
00115 typedef std::map<size_t, sum_decompositions_t*> overflow_sum_decompositions_mapping_t;
00118 size_t _n, _m;
00121 sum_decompositions_t **_tables;
00125 overflow_sum_decompositions_mapping_t _overflow;
00126
00129 void _init(void);
00130
00131
00132 SumDecompositions(const SumDecompositions& sum_decompositions);
00133
00134 SumDecompositions& operator=(const SumDecompositions& sum_decompositions);
00135 public:
00136
00139 SumDecompositions(size_t sum, size_t addends): _n(sum), _m(addends), _tables(NULL), _overflow() {
00140 _tables = new sum_decompositions_t*[_n*_m];
00141 _init();
00142 }
00145 ~SumDecompositions(void);
00150 const sum_decompositions_t * get_decompositions_table(size_t sum, size_t addends);
00151 };
00152
00153 #else
00154
00156 typedef std::vector<std::vector<bool> > bools_table_t;
00159 bools_table_t bools_table(size_t ones, size_t length);
00160 #endif
00161
00162 }
00163
00164 #endif