00001 00013 #ifndef STAT_TOOLS_H_INCLUDED 00014 #define STAT_TOOLS_H_INCLUDED 00015 00016 #include <vector> 00017 00018 namespace BPF 00019 { 00021 template<class T> 00022 T factorial( 00023 T val ) 00024 { 00025 T res(1); 00026 for (T i = 2; i <= val; ++i) 00027 res *= i; 00028 return res; 00029 } 00030 00031 namespace Internal 00032 { 00041 template<class I> 00042 void allCombin( 00043 std::vector<bool> &usedList, 00044 const I &iList, 00045 I &resultList, 00046 std::vector<I> &resultListOfList) 00047 { 00048 if (iList.size() == resultList.size()) 00049 { 00050 resultListOfList.push_back(resultList); 00051 return; 00052 } 00053 00054 int index = 0; 00055 for (typename I::const_iterator iter = iList.begin(); 00056 iter != iList.end(); 00057 iter++, index++) 00058 { 00059 if (usedList[index] == false) 00060 { 00061 usedList[index] = true; 00062 resultList.push_back(*iter); 00063 Internal::allCombin(usedList, iList, resultList, resultListOfList); 00064 resultList.pop_back(); 00065 usedList[index] = false; 00066 } 00067 } 00068 } 00069 } 00070 00074 template<class I> 00075 void allCombin( 00076 const I &iList , 00077 std::vector<I> &resultListOfList ) 00078 { 00079 resultListOfList.clear(); 00080 resultListOfList.reserve(factorial(iList.size())); 00081 if (iList.empty()) 00082 resultListOfList.push_back(I()); 00083 00084 std::vector<bool> usedList; 00085 usedList.assign(iList.size(), false); 00086 I resultList; 00087 resultList.reserve(iList.size()); 00088 int index = 0; 00089 for (typename I::const_iterator iter = iList.begin(); 00090 iter != iList.end(); 00091 ++iter, ++index) 00092 { 00093 usedList[index] = true; 00094 resultList.push_back(*iter); 00095 Internal::allCombin(usedList, iList, resultList, resultListOfList); 00096 resultList.pop_back(); 00097 usedList[index] = false; 00098 } 00099 } 00100 } 00101 00102 #endif // STAT_TOOLS_H_INCLUDED