058475f0f98493e44aef2c5c5860f367f0d2e33e
[oota-llvm.git] / lib / Target / AMDGPU / AMDILAlgorithms.tpp
1 //===------ AMDILAlgorithms.tpp - AMDIL Template Algorithms Header --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides templates algorithms that extend the STL algorithms, but
11 // are useful for the AMDIL backend
12 //
13 //===----------------------------------------------------------------------===//
14
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,
23                        Arg &Second)
24 {
25   for ( ; First!=Last; ++First ) {
26     F(*First, Second);
27   }
28   return F;
29 }
30
31 template<class InputIterator, class Function, typename Arg>
32 Function safeBinaryForEach(InputIterator First, InputIterator Last, Function F,
33                            Arg &Second)
34 {
35   for ( ; First!=Last; ++First ) {
36     if (F(*First, Second)) {
37       --First;
38     }
39   }
40   return F;
41 }
42
43 // A template function that has two levels of looping before calling the
44 // function with the passed in argument. See binaryForEach for further
45 // explanation
46 template<class InputIterator, class Function, typename Arg>
47 Function binaryNestedForEach(InputIterator First, InputIterator Last,
48                              Function F, Arg &Second)
49 {
50   for ( ; First != Last; ++First) {
51     binaryForEach(First->begin(), First->end(), F, Second);
52   }
53   return F;
54 }
55 template<class InputIterator, class Function, typename Arg>
56 Function safeBinaryNestedForEach(InputIterator First, InputIterator Last,
57                                  Function F, Arg &Second)
58 {
59   for ( ; First != Last; ++First) {
60     safeBinaryForEach(First->begin(), First->end(), F, Second);
61   }
62   return F;
63 }
64
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)
70 {
71   for ( ; First!=Last; ++First )  F(&First)
72     ; // Do nothing.
73   return F;
74 }
75
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)
82 {
83   for ( ; First != Last; ++First) {
84     SecondIterator sf, sl;
85     for (sf = First->begin(), sl = First->end();
86          sf != sl; )  {
87       if (!F(&sf)) {
88         ++sf;
89       } 
90     }
91   }
92   return F;
93 }