Changeset 1ad1c99
- Timestamp:
- Apr 27, 2016, 5:35:59 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 84bb4d9
- Parents:
- 21ae786
- Location:
- src/examples
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/examples/array.c
r21ae786 r1ad1c99 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // array.c -- 7 // array.c -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Wed Mar 2 18:13:52 201611 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Apr 27 17:21:52 2016 13 13 // Update Count : 3 14 14 // … … 26 26 // The first element is always at index 0. 27 27 forall( otype array_type, otype elt_type | bounded_array( array_type, elt_type ) ) 28 elt_type * begin( array_type array ) {28 elt_type * begin( array_type * array ) { 29 29 return &array[ 0 ]; 30 30 } … … 32 32 // The end iterator should point one past the last element. 33 33 forall( otype array_type, otype elt_type | bounded_array( array_type, elt_type ) ) 34 elt_type * end( array_type array ) {34 elt_type * end( array_type * array ) { 35 35 return &array[ last( array ) ] + 1; 36 36 } -
src/examples/array.h
r21ae786 r1ad1c99 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // array.h -- 7 // array.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Wed Mar 2 18:13:35201611 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Apr 27 17:26:04 2016 13 13 // Update Count : 5 14 14 // … … 26 26 27 27 // A bounded array is an array that carries its maximum index with it. 28 trait bounded_array( otype array_type, otype elt_type | array( array_type , elt_type ) ) {29 int last( array_type );28 trait bounded_array( otype array_type, otype elt_type | array( array_type *, elt_type ) ) { 29 int last( array_type * ); 30 30 }; 31 31 … … 41 41 // return iterators corresponding to the first element and the one-past-the-end element, STL-style. 42 42 forall( otype array_type, otype elt_type | bounded_array( array_type, elt_type ) ) 43 elt_type * begin( array_type);43 elt_type * begin( array_type * array ); 44 44 45 // The end iterator should point one past the last element. 45 46 forall( otype array_type, otype elt_type | bounded_array( array_type, elt_type ) ) 46 elt_type * end( array_type);47 elt_type * end( array_type * array ); 47 48 48 49 #endif // ARRAY_H -
src/examples/vector_int.c
r21ae786 r1ad1c99 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Apr 06 17:18:31201612 // Last Modified On : Wed Apr 27 17:27:12 2016 13 13 // Update Count : 3 14 14 // … … 30 30 vec->capacity = reserve; 31 31 vec->data = malloc( sizeof( int ) * reserve ); 32 } 33 34 void ?{}( vector_int * vec, vector_int other ) { 35 vec->last = other.last; 36 vec->capacity = other.capacity; 37 vec->data = malloc( sizeof( int ) * other.capacity ); 38 for (int i = 0; i < vec->last; i++) { 39 vec->data[i] = other.data[i]; 40 } 32 41 } 33 42 … … 54 63 // implement bounded_array 55 64 56 lvalue int ?[?]( vector_int vec, int index ) {57 return vec .data[ index ];65 lvalue int ?[?]( vector_int * vec, int index ) { 66 return vec->data[ index ]; 58 67 } 59 68 60 int last( vector_int vec ) {61 return vec .last;69 int last( vector_int * vec ) { 70 return vec->last; 62 71 } 63 72 -
src/examples/vector_int.h
r21ae786 r1ad1c99 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Apr 06 17:21:59 201612 // Last Modified On : Wed Apr 27 17:26:59 2016 13 13 // Update Count : 2 14 14 // … … 26 26 27 27 void ?{}( vector_int * ); // allocate vector with default capacity 28 void ?{}( vector_int *, int reserve ); // allocate vector with specified capacity 28 void ?{}( vector_int *, int reserve ); // allocate vector with specified capacity 29 void ?{}( vector_int * vec, vector_int other ); // copy constructor 29 30 void ^?{}( vector_int * ); // deallocate vector's storage 30 31 … … 34 35 // implement bounded_array 35 36 36 lvalue int ?[?]( vector_int vec, int index ); // access to arbitrary element (does not resize)37 int last( vector_int vec ); // return last element37 lvalue int ?[?]( vector_int * vec, int index ); // access to arbitrary element (does not resize) 38 int last( vector_int * vec ); // return last element 38 39 39 40 #endif // VECTOR_INT_H -
src/examples/vector_test.c
r21ae786 r1ad1c99 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Apr 06 17:19:07 201612 // Last Modified On : Wed Apr 27 17:31:27 2016 13 13 // Update Count : 18 14 14 // … … 34 34 35 35 sout | "Array elements:" | endl; 36 write( begin( vec ), end(vec ), sout );36 write( begin( &vec ), end( &vec ), sout ); 37 37 sout | endl; 38 38 39 39 sout | "Array elements reversed:" | endl; 40 write_reverse( begin( vec ), end(vec ), sout );40 write_reverse( begin( &vec ), end( &vec ), sout ); 41 41 sout | endl; 42 42 }
Note: See TracChangeset
for help on using the changeset viewer.