Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=60669f14a3433b32dbbd90bd54d8590fa0e5776b
commit 60669f14a3433b32dbbd90bd54d8590fa0e5776b Author: Michel Hermier <[email protected]> Date: Tue Aug 26 10:57:59 2014 +0200 libpacman: Add falgorithm, to mimic and enhance <algorithm> to our needs. diff --git a/lib/libpacman/util/falgorithm.h b/lib/libpacman/util/falgorithm.h new file mode 100644 index 0000000..f95f66b --- /dev/null +++ b/lib/libpacman/util/falgorithm.h @@ -0,0 +1,98 @@ +/* + * falgorithm.h + * + * Copyright (c) 2014 by Michel Hermier <[email protected]> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ +#ifndef F_ALGORITM_H +#define F_ALGORITM_H + +#include <algorithm> + +namespace flib +{ + template <class InputIterable, class T> + bool contains(const InputIterable &iterable, const T &val) + { + return contains(iterable.begin(), iterable.end(), val); + } + + template <class InputIterator, class T> + InputIterator contains(InputIterator first, InputIterator last, const T &val) + { + return find(first, last, val) != last; + } + + template <class InputIterable, class T> + auto find(InputIterable iterable, const T &val) + { + return find(iterable.begin(), iterable.end(), val); + } + + template <class InputIterator, class T> + InputIterator find(InputIterator first, InputIterator last, const T &val) + { + for(; first != last && !(*first == val); ++first) { + /* Nothing to do but iterate */ + } + return first; + } + + template <class T, class U> + bool match(const T &val1, const U &val2) + { + return val1 == val2; + } + + template <class InputIterable, class T> + bool match_all(const InputIterable &iterable, const T &val) + { + return match_all(iterable.begin(), iterable.end(), val); + } + + template <class InputIterator, class T> + bool match_all(InputIterator first, InputIterator last, const T &val) + { + for(; first != last; ++first) { + if(!match(*first, val)) { + return false; + } + } + return true; + } + + template <class InputIterable, class T> + bool match_any(const InputIterable &iterable, const T &val) + { + return match_any(iterable.begin(), iterable.end(), val); + } + + template <class InputIterator, class T> + bool match_any(InputIterator first, InputIterator last, const T &val) + { + for(; first != last; ++first) { + if(match(*first, val)) { + return true; + } + } + return false; + } +} + +#endif /* F_ALGORITM_H */ + +/* vim: set ts=2 sw=2 noet: */ diff --git a/lib/libpacman/util/flist.h b/lib/libpacman/util/flist.h index 6dd5274..0494860 100644 --- a/lib/libpacman/util/flist.h +++ b/lib/libpacman/util/flist.h @@ -150,38 +150,6 @@ public: public: /* extensions */ -#if 0 - bool all_match(const FMatcher &matcher) const - { - for(auto it = cbegin(), end = cend(); it != end; it = it->next()) { - if(matcher.match(it.data()) == false) { - return false; - } - } - return true; - } - - bool any_match(const FList *list, const FMatcher &matcher) const - { - for(auto it = cbegin(), end = cend(); it != end; it = it->next()) { - if(matcher.match(it->data()) == true) { - return true; - } - } - return false; - } - - ListItem<T> *find(const FComparator &comparator) - { - for(auto it = cbegin(), end = cend(); it != end; it = it->next()) { - if(comparator->compare(it->data()) == 0) { - return it; - } - } - return NULL; - } -#endif - iterator first() { return FCList::first(); diff --git a/lib/libpacman/util/fmatcher.h b/lib/libpacman/util/fmatcher.h index c87f501..729fb0d 100644 --- a/lib/libpacman/util/fmatcher.h +++ b/lib/libpacman/util/fmatcher.h @@ -46,6 +46,21 @@ protected: } }; +namespace flib +{ + template <class T, class U> + bool match(const T &val, const FMatcher<U> &matcher) + { + return flib::match(matcher, val); + } + + template <class T, class U> + bool match(const FMatcher<T> &matcher, const U &val) + { + return matcher.match(val); + } +} + // FIXME: Add FMatcherList with match_all match_any #endif /* F_MATCHER_H */ _______________________________________________ Frugalware-git mailing list [email protected] http://frugalware.org/mailman/listinfo/frugalware-git
