// util.h - Don Yang (uguu.org) // // 05/29/11 #ifndef UTIL_H_ #define UTIL_H_ #include #include // Check that a container contains a key template inline bool ContainsKey(const Container &x, const Key &key) { return x.find(key) != x.end(); } // Delete all pointer elements in a container template inline void DeletePtrElements(T *x) { for(typename T::iterator i = x->begin(); i != x->end(); ++i) delete *i; x->clear(); } // Clear container by doing a swap with an empty container. This reduces // memory footprint a bit more for containers that reserves extra capacity. template inline void ClearContainer(T *x) { T empty; x->swap(empty); } // Seek an iterator N positions forward in a container, returns end() if // the end is reached before then. template typename T::iterator inline SeekForward(T *x, int steps) { typename T::iterator i = x->begin(); for(; i != x->end() && steps > 0; steps--) ++i; return i; } template typename T::const_iterator inline SeekForward(const T &x, int steps) { typename T::const_iterator i = x.begin(); for(; i != x.end() && steps > 0; steps--) ++i; return i; } // Conditionally exit with nonzero status and message #define CHECK(cond, msg) \ if( !(cond) ) \ { \ printf("%s:%d: %s\n", __FILE__, __LINE__, (msg)); \ exit(EXIT_FAILURE); \ } #endif // UTIL_H_