Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=437c8d3aee0f7b1dc2a2434c4f1447435d263fe4

commit 437c8d3aee0f7b1dc2a2434c4f1447435d263fe4
Author: Michel Hermier <[email protected]>
Date:   Wed Sep 24 18:38:19 2014 +0200

libpacman: Rework a little falgorithm and drop f_ptrlist_clear.

diff --git a/lib/libpacman/util/falgorithm.h b/lib/libpacman/util/falgorithm.h
index ac339a1..9428ad5 100644
--- a/lib/libpacman/util/falgorithm.h
+++ b/lib/libpacman/util/falgorithm.h
@@ -25,10 +25,27 @@

namespace flib
{
-       template <class InputIterable, class T>
-       bool contains(const InputIterable &iterable, const T &val)
-       {
-               return contains(iterable.begin(), iterable.end(), val);
+       namespace detail {
+               struct null_visitor
+               {
+                       template <class... Args>
+                       void operator () (Args... args)
+                       { }
+               };
+
+               struct false_function
+               {
+                       template <class... Args>
+                       bool operator () (Args... args)
+                       { return false; }
+               };
+
+               struct true_function
+               {
+                       template <class... Args>
+                       bool operator () (Args... args)
+                       { return true; }
+               };
}

template <class InputIterator, class T>
@@ -37,43 +54,25 @@ namespace flib
return find(first, last, val) != last;
}

-       template <class InputIterable, class T>
-       auto count(const InputIterable &iterable)
-       {
-               return count(iterable.begin(), iterable.end());
-       }
-
template <class InputIterator>
-       auto count(InputIterator first, InputIterator last)
+       typename InputIterator::size_type count(InputIterator first, 
InputIterator last)
{
-               typename 
flib::iterable_traits<InputIterator::iterable>::size_type size;
+               typename InputIterator::size_type size = 0;

-               for(size = 0; first != last; ++first, ++size) {
+               for(; first != last; ++first, ++size) {
/* Nothing to do but iterate */
}
return size;
}

-       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)
{
return find(first, last,
-                               [&] (const typename InputIterator::reference 
&o) -> bool
+                               [&] (const typename InputIterator::reference o) 
-> bool
{ return o == val; });
}

-       template <class InputIterable, class UnaryPredicate>
-       auto find_if(InputIterable iterable, UnaryPredicate pred)
-       {
-               return find(iterable.begin(), iterable.end(), pred);
-       }
-
template <class InputIterator, class UnaryPredicate>
InputIterator find_if(InputIterator first, InputIterator last, UnaryPredicate 
pred)
{
@@ -89,54 +88,89 @@ namespace flib
return val1 == val2;
}

-       template <class InputIterable, class T>
-       bool match_all(const InputIterable &iterable, const T &val)
+       template <class InputIterator, class T>
+       bool all_match(InputIterator first, InputIterator last, const T &val)
{
-               return match_all(iterable.begin(), iterable.end(), val);
+               return all_match_if(
+                               [&] (const typename InputIterator::reference o) 
-> bool
+                               { return match(o, val); });
}

-       template <class InputIterator, class T>
-       bool match_all(InputIterator first, InputIterator last, const T &val)
+       template <class InputIterator, class UnaryPredicate>
+       bool all_match_if(InputIterator first, InputIterator last, 
UnaryPredicate pred)
{
for(; first != last; ++first) {
-                       if(!match(*first, val)) {
+                       if(!pred(*first)) {
return false;
}
}
return true;
}

-       template <class InputIterable, class T>
-       bool match_any(const InputIterable &iterable, const T &val)
+       template <class InputIterator, class T>
+       bool any_match(InputIterator first, InputIterator last, const T &val)
{
-               return match_any(iterable.begin(), iterable.end(), val);
+               return any_match_if(
+                               [&] (const typename InputIterator::reference o) 
-> bool
+                               { return match(o, val); });
}

-       template <class InputIterator, class T>
-       bool match_any(InputIterator first, InputIterator last, const T &val)
+       template <class InputIterator, class UnaryPredicate>
+       bool any_match_if(InputIterator first, InputIterator last, 
UnaryPredicate pred)
{
for(; first != last; ++first) {
-                       if(match(*first, val)) {
+                       if(pred(*first)) {
return true;
}
}
return false;
}

-       template <class InputOutputIterable, class T>
-       void replace_all(InputOutputIterable &iterable, const T &val, const T 
&new_val)
+       template <class InputOutputIterator, class T, class Function = 
detail::null_visitor>
+       typename InputOutputIterator::size_type
+                       remove(InputOutputIterator first, InputOutputIterator 
last, const T &val, Function fn = Function())
{
-               replace_all<typename InputOutputIterable::iterator, typename 
InputOutputIterable::value_type>(iterable.begin(), iterable.end(), val, 
new_val);
+               return remove_if(first, last,
+                               [&] (const typename 
InputOutputIterator::reference o) -> typename InputOutputIterator::size_type
+                               { return match(o, val); }, fn);
}

-       template <class InputOutputIterator, class T>
-       void replace_all(InputOutputIterator first, InputOutputIterator last, 
const T &val, const T &new_val)
+       template <class InputOutputIterator, class UnaryPredicate, class 
Function = detail::null_visitor>
+       typename InputOutputIterator::size_type
+                       remove_if(InputOutputIterator first, 
InputOutputIterator last, UnaryPredicate pred, Function fn = Function())
{
-               for(; first != last; ++first) {
-                       if(*first == val) {
-                               *first = new_val;
-                       }
+               typename InputOutputIterator::size_type size = 0;
+
+               while((first = std::find_if(first, last, pred)) != last) {
+                       fn(*first);
+                       first = first.remove();
+                       ++size;
+               }
+               return size;
+       }
+
+       template <class InputOutputIterator, class T, class Function>
+       typename InputOutputIterator::size_type
+                       replace(InputOutputIterator first, InputOutputIterator 
last, const T &val, const T &new_val, Function fn = detail::null_visitor())
+       {
+               return replace_if(first, last,
+                               [&] (const typename 
InputOutputIterator::reference o) -> typename InputOutputIterator::size_type
+                               { return match(o, val); }, fn);
+       }
+
+       template <class InputOutputIterator, class UnaryPredicate, class T, 
class Function>
+       typename InputOutputIterator::size_type
+                       replace_if(InputOutputIterator first, 
InputOutputIterator last, UnaryPredicate pred, const T &new_val, Function fn = 
detail::null_visitor())
+       {
+               typename InputOutputIterator::size_type size = 0;
+
+               while((first = std::find_if(first, last, pred)) != last) {
+                       fn(*first);
+                       *first = new_val;
+                       ++first;
+                       ++size;
}
+               return size;
}
}

diff --git a/lib/libpacman/util/flist.h b/lib/libpacman/util/flist.h
index 25d9ce8..163f295 100644
--- a/lib/libpacman/util/flist.h
+++ b/lib/libpacman/util/flist.h
@@ -32,7 +32,7 @@ typedef int (*_pacman_fn_cmp)(const void *, const void *);

#ifdef __cplusplus

-#include <algorithm>
+#include "util/falgorithm.h"

namespace flib
{
@@ -115,6 +115,7 @@ namespace flib
//              typedef typename iterable_traits<Iterable>::difference_type 
difference_type;
typedef typename iterable_traits<Iterable>::iterable iterable;
typedef typename iterable_traits<Iterable>::pointer pointer;
+               typedef typename iterable_traits<Iterable>::size_type size_type;
typedef typename iterable_traits<Iterable>::value_type value_type;

explicit const_iterator(iterable i = iterable())
@@ -224,6 +225,7 @@ namespace flib
typedef typename iterable_traits<Iterable>::iterable iterable;
typedef typename iterable_traits<Iterable>::pointer pointer;
typedef typename iterable_traits<Iterable>::reference reference;
+               typedef typename iterable_traits<Iterable>::size_type size_type;
typedef typename iterable_traits<Iterable>::value_type value_type;

explicit iterator(iterable i = iterable())
@@ -706,12 +708,14 @@ public:
/* Element access */

/* Modifiers */
-       void clear()
+       template <class Function = ::flib::detail::null_visitor>
+       void clear(Function fn = Function())
{
-               erase(begin(), end());
+               erase(begin(), end(), fn);
}

-       iterator erase(iterator position)
+       template <class Function = ::flib::detail::null_visitor>
+       iterator erase(iterator position, Function fn = Function())
{
ASSERT(position != c_end(), RET_ERR(PM_ERR_WRONG_ARGS, position));

@@ -722,7 +726,8 @@ public:
return iterator(next);
}

-       iterator erase(iterator first, iterator last)
+       template <class Function = ::flib::detail::null_visitor>
+       iterator erase(iterator first, iterator last, Function fn = Function())
{
auto it = first;
while(it != last) {
diff --git a/lib/libpacman/util/fmatcher.h b/lib/libpacman/util/fmatcher.h
index 729fb0d..ebb8c34 100644
--- a/lib/libpacman/util/fmatcher.h
+++ b/lib/libpacman/util/fmatcher.h
@@ -61,8 +61,6 @@ namespace flib
}
}

-// FIXME: Add FMatcherList with match_all match_any
-
#endif /* F_MATCHER_H */

/* vim: set ts=2 sw=2 noet: */
diff --git a/lib/libpacman/util/fptrlist.cpp b/lib/libpacman/util/fptrlist.cpp
index fb80249..b4f2606 100644
--- a/lib/libpacman/util/fptrlist.cpp
+++ b/lib/libpacman/util/fptrlist.cpp
@@ -94,13 +94,11 @@ FPtrList *f_ptrlist_new(void)

int f_ptrlist_delete(FPtrList *self, FVisitor *visitor)
{
-#ifndef F_NOCOMPAT
-       return f_ptrlist_clear(self, visitor);
-#else
-       self->clear(visitor);
+       ASSERT(self != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1));
+
+       self->clear(/* visitor */);
delete self;
return 0;
-#endif
}

FPtrList *f_ptrlist_add(FPtrList *list, void *data)
@@ -112,14 +110,6 @@ FPtrList *f_ptrlist_add(FPtrList *list, void *data)
return list;
}

-int f_ptrlist_clear(FPtrList *list, FVisitor *visitor)
-{
-       ASSERT(list != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1));
-
-       list->clear(/* visitor */);
-       return 0;
-}
-
size_t f_ptrlist_count(const FPtrList *self)
{
if(self != NULL) {
diff --git a/lib/libpacman/util/fptrlist.h b/lib/libpacman/util/fptrlist.h
index 66df8ef..f3b456f 100644
--- a/lib/libpacman/util/fptrlist.h
+++ b/lib/libpacman/util/fptrlist.h
@@ -57,7 +57,6 @@ FPtrList *f_ptrlist_new(void);
int f_ptrlist_delete(FPtrList *list, FVisitor *visitor);

FPtrList *f_ptrlist_add(FPtrList *list, void *data);
-int f_ptrlist_clear(FPtrList *list, FVisitor *visitor);
size_t f_ptrlist_count(const FPtrList *self);
FPtrListIterator *f_ptrlist_end(FPtrList *self);
const FPtrListIterator *f_ptrlist_end_const(const FPtrList *self);
_______________________________________________
Frugalware-git mailing list
[email protected]
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to