/* $Id: MiscUtil.tpp 4322 2009-01-27 13:34:46Z potyra $ * vim:tabstop=8:shiftwidth=8:filetype=cpp:textwidth=72: * * Misc. utility functions, template implementations. * * Copyright (C) 2007-2009 FAUmachine Team . * This program is free software. You can redistribute it and/or modify it * under the terms of the GNU General Public License, either version 2 of * the License, or (at your option) any later version. See COPYING. */ #include #include namespace util { template bool MiscUtil::compareObject(const T* obj1, const T* obj2) { if ((obj1 == NULL) || (obj2 == NULL)) { return false; } return (*obj1) == (*obj2); } template bool MiscUtil::listMatch(const std::list& l1, const std::list& l2) { if (l1.size() != l2.size()) { return false; } return std::equal(l1.begin(), l1.end(), l2.begin(), MiscUtil::compareObject); } template bool MiscUtil::listMatch( const std::list& l1, const std::list& l2, bool (*compare)(const T*, const T*)) { if (l1.size() != l2.size()) { return false; } return std::equal(l1.begin(), l1.end(), l2.begin(), compare); } template std::string MiscUtil::toString(const T &val) { std::stringstream stream; stream << val; std::string result = stream.str(); return result; } template bool MiscUtil::listContainsObj(const T& haystack, const U needle) { for (typename T::const_iterator i = haystack.begin(); i != haystack.end(); i++) { if (MiscUtil::compareObject((*i), needle)) { return true; } } return false; } template bool MiscUtil::listStartsWith(const std::list begin, const std::list reference) { if (begin.size() > reference.size()) { return false; } return std::equal(begin.begin(), begin.end(), reference.begin(), MiscUtil::compareObject); } template void MiscUtil::ldelete(T& l) { for (typename T::const_iterator i = l.begin(); i != l.end(); i++) { delete *i; } l.clear(); } template void MiscUtil::lterminate(T*& l) { if (l == NULL) { return; } delete l; l = NULL; } template void MiscUtil::terminate(T& item) { item = NULL; } template void MiscUtil::ldelete_if(T& l, U functor) { for (typename T::iterator i = l.begin(); i != l.end();) { if (functor(*i)) { typename T::value_type cur = *i; i = l.erase(i); delete cur; continue; } i++; } } template void MiscUtil::listPut(T *l, std::ostream &stream, const char *delim) { if (l == NULL) { stream << "(null)"; return; } for (typename T::const_iterator i = l->begin(); i != l->end(); /* nothing */) { stream << **i; i++; if (i != l->end()) { stream << delim; } } } }; /* namespace util */