

Link: http://codepad.org/vF7V6Oig raw code | output | fork ]



C , pasted on Oct 30:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 #include <stdio.h> #include <stdlib.h> #include <string.h> #define LIST_STRUCT(T) T *members; size_t length, capacity; #define LIST_INIT(l) ((l)->length = (l)->capacity = 0, (l)->members = NULL) #define LIST_DEINIT(l) ((l)->capacity ? free((l)->members) : 0) #define LIST_PUSH(l,x) (_LIST_EXPAND(l), ((l)->members[(l)->length++] = (x))) #define LIST_POP(l) ((l)->members[--(l)->length]) #define LIST_CLEAR(l) ((l)->length = 0) #define LIST_SPLICE(l,i,c)\ (memmove(\ (l)->members + (i), (l)->members + (i) + (c),\ ((l)->length - (i) - (c)) * sizeof(*(l)->members)\ ),\ (l)->length -= (c)) #define LIST_INSERT(l,i,x)\ (_LIST_EXPAND(l), memmove(\ (l)->members + (i) + 1, (l)->members + (i),\ ((l)->length - (i)) * sizeof(*(l)->members)\ ),\ (l)->length++, (l)->members[(i)] = (x)) #define LIST_SORT(l,f) qsort((l)->members, (l)->length,\ sizeof(*(l)->members), (f)) #define LIST_SWAP(l,a,b)\ (_LIST_EXPAND(l),\ ((l)->members[(l)->length] = (l)->members[(b)]),\ ((l)->members[(b)] = (l)->members[(a)]),\ ((l)->members[(a)] = (l)->members[(l)->length])) #define LIST_FOREACH(l,v,i)\ for ((i) = 0;\ (v) = (l)->members[(i)], (i) < (l)->length; ++(i)) #define _LIST_REALLOC(l)\ ((l)->members = realloc(\ (l)->members, (l)->capacity * sizeof(*(l)->members))) #define _LIST_EXPAND(l) ((l)->length + 1 > (l)->capacity\ ? (((l)->capacity && ((l)->capacity <<= 1))\ ? 0 : ((l)->capacity = 1)),\ _LIST_REALLOC(l) : 0) int compare_string_ptr ( const void * a , const void * b ) { return strcmp ( * ( char ** ) a , * ( char ** ) b ); } int compare_float_ptr ( const void * a , const void * b ) { return * ( float * ) a < * ( float * ) b ? - 1 : * ( float * ) a > * ( float * ) b ; } void test_string ( void ) { int i ; char * str ; struct { LIST_STRUCT ( char * ) } l ; LIST_INIT ( & l ); /* Add initial values to the list */ LIST_PUSH ( & l , "Hello" ); LIST_PUSH ( & l , "World" ); LIST_PUSH ( & l , "How's" ); LIST_PUSH ( & l , "It" ); LIST_PUSH ( & l , "Going" ); LIST_PUSH ( & l , "Today?" ); printf ( "Original : " ); LIST_FOREACH ( & l , str , i ) printf ( "'%s', " , str ); printf ( "

" ); /* Remove some values and add some others at different places in the list */ LIST_SPLICE ( & l , 0 , 2 ); LIST_SPLICE ( & l , l . length - 1 , 1 ); LIST_INSERT ( & l , 0 , "Hi" ); LIST_INSERT ( & l , l . length - 2 , "All" ); printf ( "Modified : " ); LIST_FOREACH ( & l , str , i ) printf ( "'%s', " , str ); printf ( "

" ); /* Sort the list, ascending alphabetical order */ LIST_SORT ( & l , compare_string_ptr ); printf ( "Sorted : " ); LIST_FOREACH ( & l , str , i ) printf ( "'%s', " , str ); printf ( "

" ); /* Reverse the list */ for ( i = 0 ; i < l . length / 2 ; i ++ ) { LIST_SWAP ( & l , i , l . length - 1 - i ); } printf ( "Reversed : " ); LIST_FOREACH ( & l , str , i ) printf ( "'%s', " , str ); printf ( "

" ); /* Print the list's ending length/capacity and deinit it */ printf ( "Capacity : %d

" , l . capacity ); printf ( "Length : %d

" , l . length ); LIST_DEINIT ( & l ); } void test_float ( void ) { int i ; struct { LIST_STRUCT ( float ) } l ; LIST_INIT ( & l ); /* Add 100,000 random numbers */ for ( i = 0 ; i < 100000 ; i ++ ) { LIST_PUSH ( & l , rand ()); } /* Sort the numbers */ LIST_SORT ( & l , compare_float_ptr ); /* Print smallest 5 values */ printf ( "Smallest : " ); for ( i = 0 ; i < 5 ; i ++ ) { printf ( "%.0f, " , l . members [ i ]); } printf ( "

" ); /* Print largest 5 values */ printf ( "Largest : " ); for ( i = l . length - 1 ; i > l . length - 6 ; i -- ) { printf ( "%.0f, " , l . members [ i ]); } printf ( "

" ); /* Print the list's ending length/capacity and deinit it */ printf ( "Capacity : %d

" , l . capacity ); printf ( "Length : %d

" , l . length ); LIST_DEINIT ( & l ); } int main ( void ) { printf ( "--- STRING TEST

" ); test_string (); printf ( "--- FLOAT TEST

" ); test_float (); return 0 ; }



Output: 1 2 3 4 5 6 7 8 9 10 11 12 --- STRING TEST Original : 'Hello', 'World', 'How's', 'It', 'Going', 'Today?', Modified : 'Hi', 'How's', 'It', 'All', 'Going', Sorted : 'All', 'Going', 'Hi', 'How's', 'It', Reversed : 'It', 'How's', 'Hi', 'Going', 'All', Capacity : 8 Length : 5 --- FLOAT TEST Smallest : 3722, 31756, 35554, 56172, 61089, Largest : 2147469824, 2147466240, 2147414144, 2147378048, 2147316992, Capacity : 131072 Length : 100000