Changeset 79b8dc3 for doc/generic_types/evaluation
- Timestamp:
- Apr 14, 2017, 6:24:35 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 17f27d40, 1c38f5b, 6eb4398
- Parents:
- 4570131
- Location:
- doc/generic_types/evaluation
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/generic_types/evaluation/cfa-bench.c
r4570131 r79b8dc3 1 1 #include <stdlib> 2 #include <stdlib.h>3 2 #include <stdio.h> 4 3 #include "pair" … … 7 6 #include "cfa-print.h" 8 7 9 int main(int argc, char** argv) { 10 FILE* out = fopen("cfa-out.txt", "w"); 11 srand(20171025); 8 int main( int argc, char *argv[] ) { 9 FILE * out = fopen( "cfa-out.txt", "w" ); 10 int max = 0; 11 stack(int) s, t; 12 REPEAT_TIMED( "push_int", push( &s, _i ); ) 13 TIMED( "copy_int", t = s; ) 14 TIMED( "clear_int", clear( &s ); ) 15 REPEAT_TIMED( "pop_int", max = max( max, pop( &t ) ); ) 16 REPEAT_TIMED( "print_int", print( out, _i, ":", _i, "\n" ); ) 12 17 13 stack(int) s; 14 REPEAT_TIMED( "push_int", 15 push( &s, rand() ); 16 ) 17 18 stack(int) t; 19 TIMED( "copy_int", 20 t = s; 21 ) 22 23 TIMED( "clear_int", 24 clear( &s ); 25 ) 26 27 int max = 0; 28 REPEAT_TIMED( "pop_int", 29 max = max( max, pop( &t ) ); 30 ) 31 print( out, max, "\n" ); 32 33 REPEAT_N_TIMED( "print_int", N/2, 34 print( out, rand(), ":", rand(), "\n" ); 35 ) 36 37 stack(pair(_Bool, char)) s2; 38 REPEAT_TIMED( "push_bool_char", 39 push( &s2, (pair(_Bool, char)){ rand() & 0x1, rand() & 0x7F } ); 40 ) 41 42 stack(pair(_Bool, char)) t2; 43 TIMED( "copy_bool_char", 44 t2 = s2; 45 ) 46 47 TIMED( "clear_bool_char", 48 clear( &s2 ); 49 ) 50 51 pair(_Bool, char) max2 = { (_Bool)0, '\0' }; 52 REPEAT_TIMED( "pop_bool_char", 53 max2 = max( max2, pop( &t2 ) ); 54 ) 55 print( out, max2, "\n" ); 56 57 REPEAT_N_TIMED( "print_pair", N/2, 58 print( out, (pair(_Bool, char)){ rand() & 0x1, rand() & 0x7F }, ":", 59 (pair(_Bool, char)){ rand() & 0x1, rand() & 0x7F }, "\n" ); 60 ) 18 stack(pair(_Bool, char)) s1, t1; 19 pair(_Bool, char) max = { (_Bool)0, '\0' }; 20 REPEAT_TIMED( "push_pair", push( &s1, (pair(_Bool, char)){ _i & 1, _i &0x7F } ); ) 21 TIMED( "copy_pair", t1 = s1; ) 22 TIMED( "clear_pair", clear( &s1 ); ) 23 REPEAT_TIMED( "pop_pair", max = max( max, pop( &t1 ) ); ) 24 REPEAT_TIMED( "print_pair", 25 print( out, (pair(_Bool, char)){ _i & 1, _i &0x7F }, ":", (pair(_Bool, char)){ _i & 1, _i &0x7F }, "\n" ); ) 61 26 fclose(out); 62 27 } -
doc/generic_types/evaluation/cpp-bench.cpp
r4570131 r79b8dc3 1 1 #include <algorithm> 2 2 #include <fstream> 3 #include <stdlib.h>4 3 #include <utility> 5 4 #include "bench.hpp" … … 9 8 int main(int argc, char** argv) { 10 9 std::ofstream out{"cpp-out.txt"}; 11 srand(20171025); 10 stack<int> s, t; 11 int max = 0; 12 REPEAT_TIMED( "push_int", s.push( _i ); ) 13 TIMED( "copy_int", t = s; ) 14 TIMED( "clear_int", s.clear(); ) 15 REPEAT_TIMED( "pop_int", max = std::max( max, t.pop() ); ) 16 print( out, max, "\n" ); 17 REPEAT_N_TIMED( "print_int", N/2, print( out, _i, ":", _i, "\n" ); ) 12 18 13 stack<int> s; 14 REPEAT_TIMED( "push_int", 15 s.push( rand() ); 16 ) 17 18 stack<int> t; 19 TIMED( "copy_int", 20 t = s; 21 ) 22 23 TIMED( "clear_int", 24 s.clear(); 25 ) 26 27 int max = 0; 28 REPEAT_TIMED( "pop_int", 29 max = std::max( max, t.pop() ); 30 ) 31 print( out, max, "\n" ); 32 33 REPEAT_N_TIMED( "print_int", N/2, 34 print( out, rand(), ":", rand(), "\n" ); 35 ) 36 37 stack<std::pair<bool, char>> s2; 38 REPEAT_TIMED( "push_bool_char", 39 s2.push( std::pair<bool, char>{ rand() & 0x1, rand() & 0x7F } ); 40 ) 41 42 stack<std::pair<bool,char>> t2; 43 TIMED( "copy_bool_char", 44 t2 = s2; 45 ) 46 47 TIMED( "clear_bool_char", 48 s2.clear(); 49 ) 50 51 std::pair<bool, char> max2 = { false, '\0' }; 52 REPEAT_TIMED( "pop_bool_char", 53 max2 = std::max( max2, t2.pop() ); 54 ) 55 print( out, max2, "\n" ); 56 19 stack<std::pair<bool, char>> s1, t1; 20 std::pair<bool, char> max1 = { false, '\0' }; 21 REPEAT_TIMED( "push_bool_char", s1.push( std::pair<bool, char>{ _i & 0x1, _i & 0x7F } ); ) 22 TIMED( "copy_bool_char", t1 = s1; ) 23 TIMED( "clear_bool_char", s1.clear(); ) 24 REPEAT_TIMED( "pop_bool_char", max1 = std::max( max1, t1.pop() ); ) 25 print( out, max1, "\n" ); 57 26 REPEAT_N_TIMED( "print_pair", N/2, 58 print( out, std::pair<bool, char>{ rand() & 0x1, rand() & 0x7F }, ":", 59 std::pair<bool, char>{ rand() & 0x1, rand() & 0x7F }, "\n" ); 60 ) 27 print( out, std::pair<bool, char>{ _i & 0x1, _i & 0x7F }, ":", 28 std::pair<bool, char>{ _i & 0x1, _i & 0x7F }, "\n" ); ) 61 29 } -
doc/generic_types/evaluation/cpp-vbench.cpp
r4570131 r79b8dc3 1 1 #include <algorithm> 2 2 #include <fstream> 3 #include <stdlib.h>4 3 #include "bench.hpp" 5 4 #include "cpp-vstack.hpp" … … 9 8 int main(int argc, char** argv) { 10 9 std::ofstream out{"cpp-vout.txt"}; 11 srand(20171025); 10 stack s, t; 11 integer max{ 0 }; 12 REPEAT_TIMED( "push_int", s.push( make<integer>( _i ) ); ) 13 TIMED( "copy_int", t = s; ) 14 TIMED( "clear_int", s.clear(); ) 15 REPEAT_TIMED( "pop_int", max = std::max( max, t.pop()->as<integer>() ); /***/ ) 16 print( out, max, c_string{"\n"} ); 17 REPEAT_N_TIMED( "print_int", N/2, 18 print( out, integer{_i}, c_string{":"}, integer{_i}, c_string{"\n"} ); ) 12 19 13 stack s; 14 REPEAT_TIMED( "push_int", 15 s.push( std::make_unique<integer>( rand() ) ); 16 ) 17 18 stack t; 19 TIMED( "copy_int", 20 t = s; 21 ) 22 23 TIMED( "clear_int", 24 s.clear(); 25 ) 26 27 integer max; 28 REPEAT_TIMED( "pop_int", 29 max = std::max( max, t.pop()->as<integer>() ); /***/ 30 ) 31 print( out, max, c_string{"\n"} ); 32 33 REPEAT_N_TIMED( "print_int", N/2, 34 print( out, integer{rand()}, c_string{":"}, integer{rand()}, c_string{"\n"} ); 35 ) 36 37 stack s2; 38 REPEAT_TIMED( "push_bool_char", 39 s2.push( std::make_unique<pair>( std::make_unique<boolean>( rand() & 0x1 ), 40 std::make_unique<character>( rand() & 0x7F ) ) ); 41 ) 42 43 stack t2; 44 TIMED( "copy_bool_char", 45 t2 = s2; 46 ) 47 48 TIMED( "clear_bool_char", 49 s2.clear(); 50 ) 51 52 auto max2 = std::make_unique<pair>( std::make_unique<boolean>(false), 53 std::make_unique<character>('\0') ); 20 stack s1, t1; 21 ptr<pair> max1 = make<pair>( make<boolean>(false), make<character>('\0') ); 22 REPEAT_TIMED( "push_bool_char", 23 s1.push( make<pair>( make<boolean>(_i & 1), make<character>(_i & 0x7F) ) ); ) 24 TIMED( "copy_bool_char", t1 = s1; ) 25 TIMED( "clear_bool_char", s1.clear(); ) 54 26 REPEAT_TIMED( "pop_bool_char", 55 std::unique_ptr<pair> x = as_ptr<pair>( t2.pop() ); /***/ 56 if ( *x > *max2 ) { max2 = std::move(x); } 57 ) 58 print( out, *max2, c_string{"\n"} ); 59 27 ptr<pair> x = as_ptr<pair>( t1.pop() ); /***/ 28 if ( *x > *max1 ) { max1 = std::move(x); } ) 29 print( out, *max1, c_string{"\n"} ); 60 30 REPEAT_N_TIMED( "print_pair", N/2, 61 print( out, pair{ std::make_unique<boolean>( rand() & 0x1 ), 62 std::make_unique<character>( rand() & 0x7F ) }, c_string{":"}, 63 pair{ std::make_unique<boolean>( rand() & 0x1 ), 64 std::make_unique<character>( rand() & 0x7F ) }, c_string{"\n"} ); 65 ) 31 print( out, pair{ make<boolean>(_i & 1), make<character>(_i & 0x7F) }, c_string{":"}, 32 pair{ make<boolean>(_i & 1), make<character>(_i & 0x7F) }, c_string{"\n"} ); ) 66 33 } -
doc/generic_types/evaluation/cpp-vstack.cpp
r4570131 r79b8dc3 5 5 stack::node::node( const object& v ) : value( v.new_copy() ), next( nullptr ) {} 6 6 7 stack::node::node( std::unique_ptr<object>&& v, node* n ) : value( std::move(v) ), next( n ) {}7 stack::node::node( ptr<object>&& v, node* n ) : value( std::move(v) ), next( n ) {} 8 8 9 9 void stack::copy(const stack& o) { … … 50 50 } 51 51 52 bool stack::empty() const { 53 return head == nullptr; 54 } 52 bool stack::empty() const { return head == nullptr; } 55 53 56 void stack::push(std::unique_ptr<object>&& value) { 57 head = new node{ std::move(value), head }; /***/ 58 } 54 void stack::push(ptr<object>&& value) { head = new node{ std::move(value), head }; /***/ } 59 55 60 std::unique_ptr<object> stack::pop() {56 ptr<object> stack::pop() { 61 57 node* n = head; 62 58 head = n->next; 63 std::unique_ptr<object> x = std::move(n->value);59 ptr<object> x = std::move(n->value); 64 60 delete n; 65 61 return x; -
doc/generic_types/evaluation/cpp-vstack.hpp
r4570131 r79b8dc3 5 5 class stack { 6 6 struct node { 7 std::unique_ptr<object> value;7 ptr<object> value; 8 8 node* next; 9 9 10 10 node( const object& v ); 11 11 12 node( std::unique_ptr<object>&& v, node* n );12 node( ptr<object>&& v, node* n ); 13 13 }; 14 14 … … 34 34 bool empty() const; 35 35 36 void push( std::unique_ptr<object>&& value);36 void push(ptr<object>&& value); 37 37 38 std::unique_ptr<object> pop();38 ptr<object> pop(); 39 39 }; -
doc/generic_types/evaluation/object.hpp
r4570131 r79b8dc3 25 25 std::type_index class_of() { return { typeid(T) }; } 26 26 27 template<typename T> using ptr = std::unique_ptr<T>; 28 27 29 class object { 28 30 public: … … 43 45 } 44 46 45 virtual std::unique_ptr<object> new_inst() const = 0;46 47 virtual std::unique_ptr<object> new_copy() const = 0;47 virtual ptr<object> new_inst() const = 0; 48 49 virtual ptr<object> new_copy() const = 0; 48 50 49 51 virtual object& operator= (const object&) = 0; … … 52 54 }; 53 55 56 template<typename T, typename... Args> 57 static inline ptr<T> make(Args&&... args) { return std::make_unique<T>(std::forward<Args>(args)...); } 58 54 59 template<typename To, typename From> 55 std::unique_ptr<To> as_ptr( std::unique_ptr<From>&& p ) { 56 return std::unique_ptr<To>{ &p.release()->template as<To>() }; 57 } 60 ptr<To> as_ptr( ptr<From>&& p ) { return ptr<To>{ &p.release()->template as<To>() }; } 58 61 59 62 class ordered : public virtual object { … … 87 90 boolean(bool x) : x(x) {} 88 91 89 std::unique_ptr<object> new_inst() const override { return std::make_unique<boolean>(); }90 91 std::unique_ptr<object> new_copy() const override { return std::make_unique<boolean>(*this); }92 ptr<object> new_inst() const override { return make<boolean>(); } 93 94 ptr<object> new_copy() const override { return make<boolean>(*this); } 92 95 93 96 boolean& operator= (const boolean& that) { … … 115 118 character(char x) : x(x) {} 116 119 117 std::unique_ptr<object> new_inst() const override { return std::make_unique<character>(); }118 119 std::unique_ptr<object> new_copy() const override { return std::make_unique<character>(*this); }120 ptr<object> new_inst() const override { return make<character>(); } 121 122 ptr<object> new_copy() const override { return make<character>(*this); } 120 123 121 124 character& operator= (const character& that) { … … 146 149 integer(int x) : x(x) {} 147 150 148 std::unique_ptr<object> new_inst() const override { return std::make_unique<integer>(); }149 150 std::unique_ptr<object> new_copy() const override { return std::make_unique<integer>(*this); }151 ptr<object> new_inst() const override { return make<integer>(); } 152 153 ptr<object> new_copy() const override { return make<integer>(*this); } 151 154 152 155 integer& operator= (const integer& that) { … … 174 177 c_string(const char* s) : s(s) {} 175 178 176 std::unique_ptr<object> new_inst() const override { return std::make_unique<c_string>(); }177 178 std::unique_ptr<object> new_copy() const override { return std::make_unique<c_string>(s); }179 ptr<object> new_inst() const override { return make<c_string>(); } 180 181 ptr<object> new_copy() const override { return make<c_string>(s); } 179 182 180 183 c_string& operator= (const c_string& that) { … … 191 194 192 195 class pair : public ordered, public printable { 193 std::unique_ptr<object> x;194 std::unique_ptr<object> y;196 ptr<object> x; 197 ptr<object> y; 195 198 196 199 public: 197 200 pair() = default; 198 201 199 pair(std::unique_ptr<object>&& x, std::unique_ptr<object>&& y) 200 : x(std::move(x)), y(std::move(y)) {} 201 202 std::unique_ptr<object> new_inst() const override { return std::make_unique<pair>(); } 203 204 std::unique_ptr<object> new_copy() const override { 205 return std::make_unique<pair>(x->new_copy(), y->new_copy()); 202 pair(ptr<object>&& x, ptr<object>&& y) : x(std::move(x)), y(std::move(y)) {} 203 204 ptr<object> new_inst() const override { return make<pair>(); } 205 206 ptr<object> new_copy() const override { 207 return make<pair>(x->new_copy(), y->new_copy()); 206 208 } 207 209
Note: See TracChangeset
for help on using the changeset viewer.