csutil/algorithms.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

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

- 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 <typename 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 <typename T, typename 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 <typename T, typename 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 <typename T, typename Fn, typename 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
    00094   template <typename T, typename Fn, typename P1, typename P2>
    00095   CS_FORCEINLINE_TEMPLATEMETHOD Fn& ForEach (T it, Fn& Func, P1& p1, P2& p2)
    00096   {
    00097     while (it.HasNext ())
    00098     {
    00099       Func (it.Next (), p1, p2);
    00100     }
    00101     return Func;
    00102   }
    00103
    00108   template <typename T, typename Fn, typename P1, typename P2, typename P3>
    00109   CS_FORCEINLINE_TEMPLATEMETHOD Fn& ForEach (T it, Fn& Func, P1& p1, P2& p2, P3& p3)
    00110   {
    00111     while (it.HasNext ())
    00112     {
    00113       Func (it.Next (), p1, p2, p3);
    00114     }
    00115     return Func;
    00116   }
    00117
    00118
    00119 }
    00120
    00121 #endif // __CSUTIL_ALGORITHMS_H__