source: tests/function-operator.cfa @ 38f4953

Last change on this file since 38f4953 was dd3576b, checked in by Peter A. Buhr <pabuhr@…>, 21 months ago

update from old to new trait syntax using forall

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[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]24forall(T &, U &)
25trait Assignable { T ?=?(T &, U); };
26forall(T &)
27trait Copyable { void ?{}(T &, T); };
28forall(T &)
29trait Destructable { void ^?{}(T &); };
[5b21138]30
[fd54fef]31trait Iterator(iter & | sized(iter) | Copyable(iter) | Destructable(iter), T) {
[5b21138]32        T & *?(iter);
33        iter ++?(iter &);
34        int ?!=?(iter, iter);
35};
36
[fd54fef]37forall(Tin, Input & | Iterator(Input, Tin), Tout, Output & | Iterator(Output, Tout) | Assignable(Tout, Tin))
[5b21138]38Output 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]47forall(Tin, Input & | Iterator(Input, Tin), Tout, Output & | Iterator(Output, Tout), FuncRet, Func & | { FuncRet ?()(Func *, Tin); } | Assignable(Tout, FuncRet))
[5b21138]48Output 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]57forall(Iter &, T | Iterator(Iter, T), Pred | { int ?()(Pred, T); })
[5b21138]58Iter 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]67forall(Generator, GenRet | { GenRet ?()(Generator &); }, Iter &, T | Iterator(Iter, T) | Assignable(T, GenRet))
[5b21138]68void 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
77struct Counter { int c; };
78void ?{}(Counter & cnt) { cnt.c = 0; }
79int ?()(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.
82struct os_wrapper {
[09687aa]83        ofstream * out;
[5b21138]84};
85
86// TODO: abstract over (os, T)
87os_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]95void ?{}(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
102os_wrapper & *?(ostream_iterator iter) {
[09687aa]103        return *iter.out;
[5b21138]104}
105ostream_iterator ++?(ostream_iterator & iter) {
[09687aa]106        // nothing to do
107        return iter;
[5b21138]108}
109int ?!=?(ostream_iterator i1, ostream_iterator i2) {
[09687aa]110        return i1.out->out != i2.out->out;
[5b21138]111}
112
[fd54fef]113forall(T | { int ?==?(T, T); })
[5b21138]114struct Equals {
115        T val;
116};
117
[fd54fef]118forall(T | { int ?==?(T, T); })
[5b21138]119int ?()(Equals(T) eq, T x) {
120        return eq.val == x;
121}
122
[fd54fef]123forall(T | { T ?*?(T, T); })
[5b21138]124struct Multiply {
125        T val;
126};
127
[fd54fef]128forall(T | { T ?*?(T, T); })
[5b21138]129T ?()(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]135forall(Return, Args...)
[7d9ad510]136struct 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]145int 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), &times2);
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: //
Note: See TracBrowser for help on using the repository browser.