## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# csutil/algorithms.h

Go to the documentation of this file.

    00001 /*
    00002   Crystal Space General Algorithms
    00003   Copyright (C)2005 by Marten Svanfeldt
    00004
    00005   This library is free software; you can redistribute it and/or
    00006   modify it under the terms of the GNU Library General Public
    00007   License as published by the Free Software Foundation; either
    00008   version 2 of the License, or (at your option) any later version.
    00009
    00010   This library is distributed in the hope that it will be useful,
    00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013   Library General Public License for more details.
    00014
    00015   You should have received a copy of the GNU Library General Public
    00016   License along with this library; if not, write to the Free
    00017   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00018 */
    00019
    00020 #ifndef __CSUTIL_ALGORITHMS_H__
    00021 #define __CSUTIL_ALGORITHMS_H__
    00022
    00027 namespace CS
    00028 {
    00029
    00033   template <class T>
    00034   CS_FORCEINLINE_TEMPLATEMETHOD void Swap (T& a, T& b)
    00035   {
    00036     T tmp = a;
    00037     a = b;
    00038     b = tmp;
    00039   }
    00040
    00051   template <class T, class Fn>
    00052   CS_FORCEINLINE_TEMPLATEMETHOD Fn& ForEach (T it, Fn& Func)
    00053   {
    00054     while (it.HasNext ())
    00055     {
    00056       Func (it.Next ());
    00057     }
    00058     return Func;
    00059   }
    00060
    00065   template <class T, class Fn>
    00066   CS_FORCEINLINE_TEMPLATEMETHOD Fn& ForEach (T* start, T* end, Fn& Func)
    00067   {
    00068     while (start != end)
    00069     {
    00070       Func (*start);
    00071       start++;
    00072     }
    00073     return Func;
    00074   }
    00075
    00080   template <class T, class Fn, class P>
    00081   CS_FORCEINLINE_TEMPLATEMETHOD Fn& ForEach (T it, Fn& Func, P& p)
    00082   {
    00083     while (it.HasNext ())
    00084     {
    00085       Func (it.Next (), p);
    00086     }
    00087     return Func;
    00088   }
    00089
    00091   template <class T1, class T2>
    00092   struct ConditionAnd
    00093   {
    00094     ConditionAnd (T1 op1, T2 op2)
    00095     { }
    00096
    00097     template<class T>
    00098     CS_FORCEINLINE_TEMPLATEMETHOD bool operator () (T obj)
    00099     {
    00100       return (op1 (obj) && op2 (obj));
    00101     }
    00102   private:
    00103     T1 op1;
    00104     T2 op2;
    00105   };
    00106 }
    00107
    00108 #endif // __CSUTIL_ALGORITHMS_H__