[5b21138] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
| 3 | //
|
---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
| 7 | // function-operator.c --
|
---|
| 8 | //
|
---|
| 9 | // Author : Rob Schluntz
|
---|
| 10 | // Created On : Fri Aug 25 15:21:11 2017
|
---|
[09687aa] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[dd3576b] | 12 | // Last Modified On : Sat Feb 25 07:26:10 2023
|
---|
| 13 | // Update Count : 12
|
---|
[5b21138] | 14 | //
|
---|
| 15 |
|
---|
[73abe95] | 16 | #include <fstream.hfa>
|
---|
| 17 | #include <stdlib.hfa>
|
---|
[5b21138] | 18 |
|
---|
| 19 | #define length(array) (sizeof((array))/sizeof((array)[0]))
|
---|
| 20 | #define begin(array) (&array[0]) // there's currently a bug in passing an array to a polymorphic function, so ensure a pointer is passed instead
|
---|
| 21 | #define end(array) (&array[length(array)])
|
---|
| 22 |
|
---|
| 23 | // STL-like Algorithms
|
---|
[dd3576b] | 24 | forall(T &, U &)
|
---|
| 25 | trait Assignable { T ?=?(T &, U); };
|
---|
| 26 | forall(T &)
|
---|
| 27 | trait Copyable { void ?{}(T &, T); };
|
---|
| 28 | forall(T &)
|
---|
| 29 | trait Destructable { void ^?{}(T &); };
|
---|
[5b21138] | 30 |
|
---|
[fd54fef] | 31 | trait Iterator(iter & | sized(iter) | Copyable(iter) | Destructable(iter), T) {
|
---|
[5b21138] | 32 | T & *?(iter);
|
---|
| 33 | iter ++?(iter &);
|
---|
| 34 | int ?!=?(iter, iter);
|
---|
| 35 | };
|
---|
| 36 |
|
---|
[fd54fef] | 37 | forall(Tin, Input & | Iterator(Input, Tin), Tout, Output & | Iterator(Output, Tout) | Assignable(Tout, Tin))
|
---|
[5b21138] | 38 | Output copy(Input first, Input last, Output result) {
|
---|
[09687aa] | 39 | while (first != last) {
|
---|
| 40 | *result = *first;
|
---|
| 41 | ++result; ++first;
|
---|
| 42 | }
|
---|
| 43 | return result;
|
---|
[5b21138] | 44 | }
|
---|
| 45 |
|
---|
| 46 | // test ?()(T *, ...) -- ?() with function call-by-pointer
|
---|
[fd54fef] | 47 | forall(Tin, Input & | Iterator(Input, Tin), Tout, Output & | Iterator(Output, Tout), FuncRet, Func & | { FuncRet ?()(Func *, Tin); } | Assignable(Tout, FuncRet))
|
---|
[5b21138] | 48 | Output transform (Input first, Input last, Output result, Func * op) {
|
---|
[09687aa] | 49 | while (first != last) {
|
---|
| 50 | *result = op(*first);
|
---|
| 51 | ++result; ++first;
|
---|
| 52 | }
|
---|
| 53 | return result;
|
---|
[5b21138] | 54 | }
|
---|
| 55 |
|
---|
| 56 | // test ?()(T, ...) -- ?() with function call-by-value
|
---|
[fd54fef] | 57 | forall(Iter &, T | Iterator(Iter, T), Pred | { int ?()(Pred, T); })
|
---|
[5b21138] | 58 | Iter find_if (Iter first, Iter last, Pred pred) {
|
---|
[09687aa] | 59 | while (first != last) {
|
---|
| 60 | if (pred(*first)) return first;
|
---|
| 61 | ++first;
|
---|
| 62 | }
|
---|
| 63 | return last;
|
---|
[5b21138] | 64 | }
|
---|
| 65 |
|
---|
| 66 | // test ?()(T, ...) -- ?() with function call-by-reference
|
---|
[fd54fef] | 67 | forall(Generator, GenRet | { GenRet ?()(Generator &); }, Iter &, T | Iterator(Iter, T) | Assignable(T, GenRet))
|
---|
[5b21138] | 68 | void generate(Iter first, Iter last, Generator & gen) {
|
---|
[09687aa] | 69 | int i = 0;
|
---|
| 70 | while (first != last) {
|
---|
| 71 | *first = gen();
|
---|
| 72 | ++first;
|
---|
| 73 | }
|
---|
[5b21138] | 74 | }
|
---|
| 75 |
|
---|
| 76 | // encapsulate a counter that increments by one every time it is called
|
---|
| 77 | struct Counter { int c; };
|
---|
| 78 | void ?{}(Counter & cnt) { cnt.c = 0; }
|
---|
| 79 | int ?()(Counter & cnt) { return cnt.c++; }
|
---|
| 80 |
|
---|
| 81 | // TODO: abstract over os type with ostream trait; resolver is currently too slow for this to be reasonable, but it does work.
|
---|
| 82 | struct os_wrapper {
|
---|
[09687aa] | 83 | ofstream * out;
|
---|
[5b21138] | 84 | };
|
---|
| 85 |
|
---|
| 86 | // TODO: abstract over (os, T)
|
---|
| 87 | os_wrapper ?=?(os_wrapper & wrapper, int x) {
|
---|
[200fcb3] | 88 | *wrapper.out | x;
|
---|
[09687aa] | 89 | return wrapper;
|
---|
[5b21138] | 90 | }
|
---|
| 91 |
|
---|
[09687aa] | 92 | struct ostream_iterator {
|
---|
| 93 | os_wrapper * out;
|
---|
| 94 | };
|
---|
[5b21138] | 95 | void ?{}(ostream_iterator & iter, ofstream * out) {
|
---|
[09687aa] | 96 | iter.out = new(out);
|
---|
[b3763ca] | 97 | }
|
---|
[5b21138] | 98 | // no destructor, memory leak. This is necessary for this to work at the moment, since
|
---|
| 99 | // *? requires its parameter by value and returns a reference.
|
---|
| 100 |
|
---|
| 101 | // implement Iterator
|
---|
| 102 | os_wrapper & *?(ostream_iterator iter) {
|
---|
[09687aa] | 103 | return *iter.out;
|
---|
[5b21138] | 104 | }
|
---|
| 105 | ostream_iterator ++?(ostream_iterator & iter) {
|
---|
[09687aa] | 106 | // nothing to do
|
---|
| 107 | return iter;
|
---|
[5b21138] | 108 | }
|
---|
| 109 | int ?!=?(ostream_iterator i1, ostream_iterator i2) {
|
---|
[09687aa] | 110 | return i1.out->out != i2.out->out;
|
---|
[5b21138] | 111 | }
|
---|
| 112 |
|
---|
[fd54fef] | 113 | forall(T | { int ?==?(T, T); })
|
---|
[5b21138] | 114 | struct Equals {
|
---|
| 115 | T val;
|
---|
| 116 | };
|
---|
| 117 |
|
---|
[fd54fef] | 118 | forall(T | { int ?==?(T, T); })
|
---|
[5b21138] | 119 | int ?()(Equals(T) eq, T x) {
|
---|
| 120 | return eq.val == x;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[fd54fef] | 123 | forall(T | { T ?*?(T, T); })
|
---|
[5b21138] | 124 | struct Multiply {
|
---|
| 125 | T val;
|
---|
| 126 | };
|
---|
| 127 |
|
---|
[fd54fef] | 128 | forall(T | { T ?*?(T, T); })
|
---|
[5b21138] | 129 | T ?()(Multiply(T) * mult, T x) {
|
---|
| 130 | return mult->val * x;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[7d9ad510] | 133 | // TODO: generalize to ttype return; doesn't work yet
|
---|
| 134 | // like std::function
|
---|
[fd54fef] | 135 | forall(Return, Args...)
|
---|
[7d9ad510] | 136 | struct function {
|
---|
[09687aa] | 137 | Return (*f)(Args);
|
---|
[7d9ad510] | 138 | };
|
---|
| 139 | // TODO: missing adapter in these functions
|
---|
| 140 | // // value, reference, pointer operators
|
---|
| 141 | // forall(otype Return, ttype Args) Return ?()(function(Return, Args) func, Args args) { return func.f(args); }
|
---|
| 142 | // forall(otype Return, ttype Args) Return ?()(function(Return, Args) & func, Args args) { return func.f(args); }
|
---|
| 143 | // forall(otype Return, ttype Args) Return ?()(function(Return, Args) * func, Args args) { return func->f(args); }
|
---|
| 144 |
|
---|
[5b21138] | 145 | int main() {
|
---|
| 146 | // generate for array fill
|
---|
| 147 | Counter c;
|
---|
| 148 | int x[10], y[10];
|
---|
| 149 | generate(begin(x), end(x), c);
|
---|
| 150 | generate(begin(y), end(y), c);
|
---|
| 151 |
|
---|
| 152 | // copy for output
|
---|
[09687aa] | 153 | ostream_iterator out_iter = { &sout };
|
---|
[5b21138] | 154 | copy(begin(x), end(x), out_iter);
|
---|
| 155 | copy(begin(y), end(y), out_iter);
|
---|
| 156 |
|
---|
| 157 | // find_if for searching
|
---|
| 158 | Equals(int) is5 = { 5 };
|
---|
| 159 | if (find_if(begin(x), end(x), is5) != end(y)) {
|
---|
| 160 | printf("Found 5 in x.\n");
|
---|
| 161 | } else {
|
---|
| 162 | printf("Did not find 5 in x.\n");
|
---|
| 163 | }
|
---|
| 164 | if (find_if(begin(y), end(y), is5) != end(y)) {
|
---|
| 165 | printf("Found 5 in y.\n");
|
---|
| 166 | } else {
|
---|
| 167 | printf("Did not find 5 in y.\n");
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | Multiply(int) times2 = { 2 };
|
---|
| 171 | transform(begin(x), end(x), begin(x), ×2);
|
---|
| 172 | copy(begin(x), end(x), out_iter);
|
---|
[b3763ca] | 173 |
|
---|
| 174 | // REMOVE WHEN ?* PROBLEM FIXED.
|
---|
| 175 | delete(out_iter.out);
|
---|
[5b21138] | 176 | }
|
---|
| 177 |
|
---|
| 178 | // Local Variables: //
|
---|
| 179 | // tab-width: 4 //
|
---|
| 180 | // End: //
|
---|