|
/*
* Copyright (c) 2002, Jorgen Sigvardsson <jorgen@profitab.com>
* All rights reserved.
*
* Redistribution and use in source, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions
* and the following disclaimer. The name Jorgen Sigvardsson may NOT be used to endorse or promote
* products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections;
namespace NETUtils {
public class Algorithms {
public static void ForEach(IEnumerator e, Functors.UnaryFunctor func) {
while(e.MoveNext())
func(e.Current);
}
public static void ForEachWhich(IEnumerator e, Functors.UnaryFunctor func, Functions.Predicate pred) {
while(e.MoveNext())
if(pred(e.Current))
func(e.Current);
}
public static object Accumulate(IEnumerator e, object objStartValue, Functors.BinaryFunctor func) {
while(e.MoveNext()) {
objStartValue = func(objStartValue, e.Current);
}
return objStartValue;
}
public static IEnumerator FindIf(IEnumerator e, Functions.Predicate pred) {
while(e.MoveNext()) {
if(pred(e.Current))
break;
}
return e;
}
public static int CountIf(IEnumerator e, Functions.Predicate pred) {
int nCount = 0;
while(e.MoveNext()) {
if(pred(e.Current))
++nCount;
}
return nCount;
}
public static Pair MismatchIf(IEnumerator e1, IEnumerator e2, Functions.BinaryPredicate pred) {
while(e1.MoveNext() && e2.MoveNext()) {
if(pred(e1.Current, e2.Current))
break;
}
return new Pair(e1, e2);
}
public static bool Equal(IEnumerator e1, IEnumerator e2, Functions.BinaryPredicate pred) {
bool bE1NotAtEnd = e1.MoveNext();
bool bE2NotAtEnd = e2.MoveNext();
while(bE1NotAtEnd && bE2NotAtEnd) {
if(!pred(e1, e2))
return false;
bE1NotAtEnd = e1.MoveNext();
bE2NotAtEnd = e2.MoveNext();
}
return bE1NotAtEnd != bE2NotAtEnd;
}
public static int Compare(IEnumerator e1, IEnumerator e2, Functions.Compare comp) {
bool bE1NotAtEnd = e1.MoveNext();
bool bE2NotAtEnd = e2.MoveNext();
while(bE1NotAtEnd && bE2NotAtEnd) {
int nComp = comp(e1.Current, e2.Current);
if(0 != nComp)
return nComp;
bE1NotAtEnd = e1.MoveNext();
bE2NotAtEnd = e2.MoveNext();
}
if(!bE1NotAtEnd && !bE2NotAtEnd)
return 0;
else if(bE1NotAtEnd)
return 1;
else
return -1;
}
public static object FindExtreme(IEnumerator e, Functions.BinaryPredicate pred) {
if(!e.MoveNext())
throw new ArgumentException("FindExtreme: No elements in e");
object objMostExtreme = e.Current;
while(e.MoveNext()) {
if(pred(objMostExtreme, e.Current))
objMostExtreme = e.Current;
}
return objMostExtreme;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.