1 //===------ AMDILAlgorithms.tpp - AMDIL Template Algorithms Header --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides templates algorithms that extend the STL algorithms, but
11 // are useful for the AMDIL backend
13 //===----------------------------------------------------------------------===//
15 // A template function that loops through the iterators and passes the second
16 // argument along with each iterator to the function. If the function returns
17 // true, then the current iterator is invalidated and it moves back, before
18 // moving forward to the next iterator, otherwise it moves forward without
19 // issue. This is based on the for_each STL function, but allows a reference to
20 // the second argument
21 template<class InputIterator, class Function, typename Arg>
22 Function binaryForEach(InputIterator First, InputIterator Last, Function F,
25 for ( ; First!=Last; ++First ) {
31 template<class InputIterator, class Function, typename Arg>
32 Function safeBinaryForEach(InputIterator First, InputIterator Last, Function F,
35 for ( ; First!=Last; ++First ) {
36 if (F(*First, Second)) {
43 // A template function that has two levels of looping before calling the
44 // function with the passed in argument. See binaryForEach for further
46 template<class InputIterator, class Function, typename Arg>
47 Function binaryNestedForEach(InputIterator First, InputIterator Last,
48 Function F, Arg &Second)
50 for ( ; First != Last; ++First) {
51 binaryForEach(First->begin(), First->end(), F, Second);
55 template<class InputIterator, class Function, typename Arg>
56 Function safeBinaryNestedForEach(InputIterator First, InputIterator Last,
57 Function F, Arg &Second)
59 for ( ; First != Last; ++First) {
60 safeBinaryForEach(First->begin(), First->end(), F, Second);
65 // Unlike the STL, a pointer to the iterator itself is passed in with the 'safe'
66 // versions of these functions This allows the function to handle situations
67 // such as invalidated iterators
68 template<class InputIterator, class Function>
69 Function safeForEach(InputIterator First, InputIterator Last, Function F)
71 for ( ; First!=Last; ++First ) F(&First)
76 // A template function that has two levels of looping before calling the
77 // function with a pointer to the current iterator. See binaryForEach for
78 // further explanation
79 template<class InputIterator, class SecondIterator, class Function>
80 Function safeNestedForEach(InputIterator First, InputIterator Last,
81 SecondIterator S, Function F)
83 for ( ; First != Last; ++First) {
84 SecondIterator sf, sl;
85 for (sf = First->begin(), sl = First->end();