Changes in / [87e08e24:7ece922]
 Location:
 src
 Files:

 4 edited
Legend:
 Unmodified
 Added
 Removed

src/libcfa/stdlib
r87e08e24 r7ece922 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Aug 7 11:19:07 201713 // Update Count : 22 312 // Last Modified On : Wed Aug 23 20:29:47 2017 13 // Update Count : 224 14 14 // 15 15 … … 230 230 231 231 forall( otype T ) 232 void swap( T * t1, T *t2 );232 void swap( T & t1, T & t2 ); 233 233 234 234 // Local Variables: // 
src/libcfa/stdlib.c
r87e08e24 r7ece922 10 10 // Created On : Thu Jan 28 17:10:29 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Aug 8 17:31:13201713 // Update Count : 29 112 // Last Modified On : Wed Aug 23 20:30:44 2017 13 // Update Count : 292 14 14 // 15 15 … … 305 305 306 306 forall( otype T ) 307 void swap( T * t1, T *t2 ) {308 T temp = *t1;309 *t1 = *t2;310 *t2 = temp;307 void swap( T & t1, T & t2 ) { 308 T temp = t1; 309 t1 = t2; 310 t2 = temp; 311 311 } // swap 312 312 
src/tests/avltree/avl3.c
r87e08e24 r7ece922 3 3 #include <stdlib> 4 4 5 // from stdlib6 forall(otype T)7 void swap(T *, T *);8 9 5 // swaps the data within two tree nodes 10 6 forall(otype K  Comparable(K), otype V) 11 7 void node_swap(tree(K, V) * t, tree(K, V) * t2){ 12 swap(&t>key, &t2>key);13 swap(&t>value, &t2>value);8 swap( t>key, t2>key); 9 swap( t>value, t2>value); 14 10 } 15 11 … … 17 13 forall(otype K  Comparable(K), otype V) 18 14 tree(K, V) * find_successor(tree(K, V) * t){ 19 20 21 22 23 24 25 26 27 15 tree(K, V) * find_successor_helper(tree(K, V) * t){ 16 // go left as deep as possible, return the last node 17 if (empty(t>left)){ 18 return t; 19 } else { 20 return find_successor_helper(t>left); 21 } 22 } 23 return find_successor_helper(t>right); 28 24 } 29 25 … … 31 27 forall(otype K  Comparable(K), otype V) 32 28 void deleteSingleNode(tree(K, V) * t) { 33 34 35 29 t>left = NULL; 30 t>right = NULL; 31 delete(t); 36 32 } 37 33 … … 39 35 forall(otype K  Comparable(K), otype V) 40 36 tree(K, V) * remove_node(tree(K, V) * t){ 41 42 43 44 45 46 47 48 49 37 // is the node a leaf? 38 if (empty(t>left) && empty(t>right)){ 39 // yes, just delete this node 40 delete(t); 41 return NULL; 42 } else if (empty(t>left)){ 43 // if the left is empty, there is only one child > move right up 44 node_swap(t, t>right); 45 tree(K, V) * tmp = t>right; 50 46 51 52 53 47 // relink tree 48 t>left = tmp>left; 49 t>right = tmp>right; 54 50 55 56 57 58 59 60 61 62 51 setParent(t>left, t); 52 setParent(t>right, t); 53 deleteSingleNode(tmp); 54 return t; 55 } else if (empty(t>right)){ 56 // if the right is empty, there is only one child > move left up 57 node_swap(t, t>left); 58 tree(K, V) * tmp = t>left; 63 59 64 65 66 60 // relink tree 61 t>left = tmp>left; 62 t>right = tmp>right; 67 63 68 69 70 71 72 73 74 75 64 setParent(t>left, t); 65 setParent(t>right, t); 66 deleteSingleNode(tmp); 67 return t; 68 } else { 69 // swap with the successor 70 tree(K, V) * s = find_successor(t); 71 tree(K, V) * parent = s>parent; 76 72 77 78 79 80 81 82 83 84 85 86 87 73 if (parent>left == s){ 74 parent>left = s>right; 75 } else { 76 assert(parent>right == s); 77 parent>right = s>right; 78 } 79 setParent(s>right, parent); 80 node_swap(t, s); 81 deleteSingleNode(s); 82 return t; 83 } 88 84 } 89 85 … … 91 87 forall(otype K  Comparable(K), otype V) 92 88 tree(K, V) * remove_helper(tree(K, V) * t, K key, int * worked){ 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 89 if (empty(t)){ 90 // did not work because key was not found 91 // set the status variable and return 92 *worked = 1; 93 } else if (t>key == key) { 94 t = remove_node(t); 95 } else if (t>key < key){ 96 t>right = remove_helper(t>right, key, worked); 97 } else { 98 // t>key > key 99 t>left = remove_helper(t>left, key, worked); 100 } 101 // try to fix after deleting 102 if (! empty(t)) { 103 t = tryFix(t); 104 } 105 return t; 110 106 } 111 107 112 108 forall(otype K  Comparable(K), otype V) 113 109 int remove(tree(K, V) ** t, K key){ 114 115 116 117 110 int worked = 0; 111 tree(K, V) * newTree = remove_helper(*t, key, &worked); 112 *t = newTree; 113 return worked; 118 114 } 115 116 // Local Variables: // 117 // tabwidth: 4 // 118 // End: // 
src/tests/swap.c
r87e08e24 r7ece922 7 7 // swap.c  8 8 // 9 // Author : Richard C. Bilson9 // Author : Peter A. Buhr 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Apr 21 08:10:41 201613 // Update Count : 6912 // Last Modified On : Wed Aug 23 20:34:45 2017 13 // Update Count : 70 14 14 // 15 15 … … 20 20 char c1 = 'a', c2 = 'b'; 21 21 sout  "char\t\t\t"  c1  ' '  c2  "\t\t\tswap "; 22 swap( &c1, &c2 );22 swap( c1, c2 ); 23 23 sout  '\t'  c1  ' '  c2  endl; 24 24 25 25 signed int i1 = 1, i2 = 2; 26 26 sout  "signed int\t\t"  i1  i2  "\t\t\tswap "; 27 swap( &i1, &i2 );27 swap( i1, i2 ); 28 28 sout  '\t'  i1  i2  endl; 29 29 30 30 unsigned int ui1 = 1, ui2 = 2; 31 31 sout  "unsigned int\t\t"  ui1  ui2  "\t\t\tswap "; 32 swap( &ui1, &ui2 );32 swap( ui1, ui2 ); 33 33 sout  '\t'  ui1  ui2  endl; 34 34 35 35 signed long int li1 = 1, li2 = 2; 36 36 sout  "signed long int\t\t"  li1  li2  "\t\t\tswap "; 37 swap( &li1, &li2 );37 swap( li1, li2 ); 38 38 sout  '\t'  li1  li2  endl; 39 39 40 40 unsigned long int uli1 = 1, uli2 = 2; 41 41 sout  "unsigned long int\t"  uli1  uli2  "\t\t\tswap "; 42 swap( &uli1, &uli2 );42 swap( uli1, uli2 ); 43 43 sout  '\t'  uli1  uli2  endl; 44 44 45 45 signed long long int lli1 = 1, lli2 = 2; 46 46 sout  "signed long long int\t"  lli1  lli2  "\t\t\tswap "; 47 swap( &lli1, &lli2 );47 swap( lli1, lli2 ); 48 48 sout  '\t'  lli1  lli2  endl; 49 49 50 50 unsigned long long int ulli1 = 1, ulli2 = 2; 51 51 sout  "unsigned long long int\t"  ulli1  ulli2  "\t\t\tswap "; 52 swap( &ulli1, &ulli2 );52 swap( ulli1, ulli2 ); 53 53 sout  '\t'  ulli1  ulli2  endl; 54 54 55 55 float f1 = 1.5, f2 = 2.5; 56 56 sout  "float\t\t\t"  f1  f2  "\t\t\tswap "; 57 swap( &f1, &f2 );57 swap( f1, f2 ); 58 58 sout  '\t'  f1  f2  endl; 59 59 60 60 double d1 = 1.5, d2 = 2.5; 61 61 sout  "double\t\t\t"  d1  d2  "\t\t\tswap "; 62 swap( &d1, &d2 );62 swap( d1, d2 ); 63 63 sout  '\t'  d1  d2  endl; 64 64 65 65 long double ld1 = 1.5, ld2 = 2.5; 66 66 sout  "long double\t\t"  ld1  ld2  "\t\t\tswap "; 67 swap( &ld1, &ld2 );67 swap( ld1, ld2 ); 68 68 sout  '\t'  ld1  ld2  endl; 69 69 70 70 float _Complex fc1 = 1.5f+1.5if, fc2 = 2.5f+2.5if; 71 71 sout  "float _Complex\t\t"  fc1  fc2  "\tswap "; 72 swap( &fc1, &fc2 );72 swap( fc1, fc2 ); 73 73 sout  '\t'  fc1  fc2  endl; 74 74 75 75 double _Complex dc1 = 1.5d+1.5id, dc2 = 2.5d+2.5id; 76 76 sout  "double _Complex\t\t"  dc1  dc2  "\tswap "; 77 swap( &dc1, &dc2 );77 swap( dc1, dc2 ); 78 78 sout  '\t'  dc1  dc2  endl; 79 79 80 80 long double _Complex ldc1 = 1.5d+1.5il, ldc2 = 2.5d+2.5il; 81 81 sout  "long double _Complex\t"  ldc1  ldc2  "\tswap "; 82 swap( &ldc1, &ldc2 );82 swap( ldc1, ldc2 ); 83 83 sout  '\t'  ldc1  ldc2  endl; 84 84 … … 86 86 ofstream * ??( ofstream * os, S s ) { return os  s.i  s.j; } 87 87 sout  "struct S\t\t"  s1  ","  s2  "\t\tswap "; 88 swap( &s1, &s2 );88 swap( s1, s2 ); 89 89 sout  '\t'  s1  ","  s2  endl; 90 90 } // main
Note: See TracChangeset
for help on using the changeset viewer.