~
[scx.git] / include / StlUtils.hpp
blob70d6ca8bc82fe728c439a2e5a5f06858d3607d25
1 #ifndef SCX_STL_UTILS_HPP
2 #define SCX_STL_UTILS_HPP
4 #include <algorithm>
5 using namespace std;
7 namespace scx {
9 #define SCX_FNOBJ(id, args, list, init, opts) \
10 struct FnObj##id { \
11 args; \
13 FnObj##id()list { init; } \
14 ~FnObj##id() { cout << "!"; } \
16 template<typename T> \
17 void operator()(T& val) { opts; } \
20 template<typename C>
21 static inline typename C::value_type LinearSum(const C& container, size_t begin, size_t end) {
22 typename C::value_type sum = 0;
23 for (size_t i = 0; i < end; ++i) {
24 sum += container[i];
26 return sum;
29 template<typename C>
30 static inline typename C::value_type LinearSum(const C& container, size_t begin) {
31 return LinearSum(container, begin, container.size());
34 template<typename C>
35 static inline typename C::value_type LinearSum(const C& container) {
36 return LinearSum(container, 0, container.size());
39 template<typename C>
40 static inline C LinearSum(const C array[], size_t begin, size_t end) {
41 C sum = 0;
42 for (size_t i = 0; i < end; ++i) {
43 sum += array[i];
45 return sum;
48 template<typename C>
49 static inline C LinearSum(C array[], size_t begin) {
50 return LinearSum(array, begin, sizeof((C[])array)/sizeof(C));
55 #endif