[ea7d2b0] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 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 | // |
---|
[73abe95] | 7 | // bits/containers.hfa -- Intrusive generic containers.hfa |
---|
[ea7d2b0] | 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
| 10 | // Created On : Tue Oct 31 16:38:50 2017 |
---|
[2233ad4] | 11 | // Last Modified By : Peter A. Buhr |
---|
[768bd556] | 12 | // Last Modified On : Wed Jan 15 07:42:35 2020 |
---|
| 13 | // Update Count : 28 |
---|
[ea7d2b0] | 14 | |
---|
| 15 | #pragma once |
---|
| 16 | |
---|
[73abe95] | 17 | #include "bits/align.hfa" |
---|
| 18 | #include "bits/defs.hfa" |
---|
[ea7d2b0] | 19 | |
---|
[0cf5b79] | 20 | //----------------------------------------------------------------------------- |
---|
| 21 | // Array |
---|
| 22 | //----------------------------------------------------------------------------- |
---|
| 23 | |
---|
| 24 | #ifdef __cforall |
---|
| 25 | forall(dtype T) |
---|
| 26 | #else |
---|
| 27 | #define T void |
---|
| 28 | #endif |
---|
| 29 | struct __small_array { |
---|
| 30 | T * data; |
---|
| 31 | __lock_size_t size; |
---|
| 32 | }; |
---|
| 33 | #undef T |
---|
| 34 | |
---|
| 35 | #ifdef __cforall |
---|
| 36 | #define __small_array_t(T) __small_array(T) |
---|
| 37 | #else |
---|
| 38 | #define __small_array_t(T) struct __small_array |
---|
| 39 | #endif |
---|
| 40 | |
---|
| 41 | #ifdef __cforall |
---|
| 42 | // forall(otype T | sized(T)) |
---|
| 43 | // static inline void ?{}(__small_array(T) & this) {} |
---|
| 44 | |
---|
| 45 | forall(dtype T | sized(T)) |
---|
[768bd556] | 46 | static inline T & ?[?]( __small_array(T) & this, __lock_size_t idx ) { |
---|
[0cf5b79] | 47 | return ((typeof(this.data))this.data)[idx]; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | forall(dtype T | sized(T)) |
---|
[768bd556] | 51 | static inline T & ?[?]( const __small_array(T) & this, __lock_size_t idx ) { |
---|
[0cf5b79] | 52 | return ((typeof(this.data))this.data)[idx]; |
---|
| 53 | } |
---|
| 54 | |
---|
[768bd556] | 55 | forall(dtype T) |
---|
| 56 | static inline T * begin( const __small_array(T) & this ) { |
---|
[0cf5b79] | 57 | return ((typeof(this.data))this.data); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | forall(dtype T | sized(T)) |
---|
[768bd556] | 61 | static inline T * end( const __small_array(T) & this ) { |
---|
[0cf5b79] | 62 | return ((typeof(this.data))this.data) + this.size; |
---|
| 63 | } |
---|
| 64 | #endif |
---|
| 65 | |
---|
[ea7d2b0] | 66 | //----------------------------------------------------------------------------- |
---|
| 67 | // Node Base |
---|
| 68 | //----------------------------------------------------------------------------- |
---|
| 69 | |
---|
[0cf5b79] | 70 | #ifdef __cforall |
---|
[ea7d2b0] | 71 | trait is_node(dtype T) { |
---|
[768bd556] | 72 | T *& get_next( T & ); |
---|
[ea7d2b0] | 73 | }; |
---|
| 74 | #endif |
---|
| 75 | |
---|
| 76 | //----------------------------------------------------------------------------- |
---|
| 77 | // Stack |
---|
| 78 | //----------------------------------------------------------------------------- |
---|
[0cf5b79] | 79 | #ifdef __cforall |
---|
[3623f9d] | 80 | forall(dtype TYPE) |
---|
[ea7d2b0] | 81 | #define T TYPE |
---|
| 82 | #else |
---|
| 83 | #define T void |
---|
| 84 | #endif |
---|
| 85 | struct __stack { |
---|
| 86 | T * top; |
---|
| 87 | }; |
---|
[0cf5b79] | 88 | #undef T |
---|
[ea7d2b0] | 89 | |
---|
[0cf5b79] | 90 | #ifdef __cforall |
---|
[ea7d2b0] | 91 | #define __stack_t(T) __stack(T) |
---|
| 92 | #else |
---|
| 93 | #define __stack_t(T) struct __stack |
---|
| 94 | #endif |
---|
| 95 | |
---|
[0cf5b79] | 96 | #ifdef __cforall |
---|
[3623f9d] | 97 | forall(dtype T) |
---|
[0cf5b79] | 98 | static inline void ?{}( __stack(T) & this ) { |
---|
[768bd556] | 99 | (this.top){ 0p }; |
---|
[ea7d2b0] | 100 | } |
---|
| 101 | |
---|
[768bd556] | 102 | static inline forall( dtype T | is_node(T) ) { |
---|
| 103 | void push( __stack(T) & this, T * val ) { |
---|
| 104 | verify( !get_next( *val ) ); |
---|
| 105 | get_next( *val ) = this.top; |
---|
| 106 | this.top = val; |
---|
| 107 | } |
---|
[ea7d2b0] | 108 | |
---|
[768bd556] | 109 | T * pop( __stack(T) & this ) { |
---|
| 110 | T * top = this.top; |
---|
| 111 | if( top ) { |
---|
| 112 | this.top = get_next( *top ); |
---|
| 113 | get_next( *top ) = 0p; |
---|
| 114 | } |
---|
| 115 | return top; |
---|
[ea7d2b0] | 116 | } |
---|
[2233ad4] | 117 | |
---|
[768bd556] | 118 | int ?!=?( const __stack(T) & this, __attribute__((unused)) zero_t zero ) { |
---|
| 119 | return this.top != 0; |
---|
| 120 | } |
---|
[2233ad4] | 121 | } |
---|
[ea7d2b0] | 122 | #endif |
---|
| 123 | |
---|
| 124 | //----------------------------------------------------------------------------- |
---|
| 125 | // Queue |
---|
| 126 | //----------------------------------------------------------------------------- |
---|
[0cf5b79] | 127 | #ifdef __cforall |
---|
[3623f9d] | 128 | forall(dtype TYPE) |
---|
[ea7d2b0] | 129 | #define T TYPE |
---|
| 130 | #else |
---|
| 131 | #define T void |
---|
| 132 | #endif |
---|
| 133 | struct __queue { |
---|
| 134 | T * head; |
---|
| 135 | T ** tail; |
---|
| 136 | }; |
---|
[0cf5b79] | 137 | #undef T |
---|
| 138 | |
---|
| 139 | #ifdef __cforall |
---|
| 140 | #define __queue_t(T) __queue(T) |
---|
| 141 | #else |
---|
| 142 | #define __queue_t(T) struct __queue |
---|
| 143 | #endif |
---|
[ea7d2b0] | 144 | |
---|
[0cf5b79] | 145 | #ifdef __cforall |
---|
[768bd556] | 146 | static inline forall( dtype T | is_node(T) ) { |
---|
| 147 | void ?{}( __queue(T) & this ) with( this ) { |
---|
[3381ed7] | 148 | head{ 1p }; |
---|
[768bd556] | 149 | tail{ &head }; |
---|
[3381ed7] | 150 | verify(*tail == 1p); |
---|
[768bd556] | 151 | } |
---|
[65deb18] | 152 | |
---|
[768bd556] | 153 | void append( __queue(T) & this, T * val ) with( this ) { |
---|
| 154 | verify(tail != 0p); |
---|
[3381ed7] | 155 | verify(*tail == 1p); |
---|
[768bd556] | 156 | *tail = val; |
---|
| 157 | tail = &get_next( *val ); |
---|
[3381ed7] | 158 | *tail = 1p; |
---|
[768bd556] | 159 | } |
---|
[ea7d2b0] | 160 | |
---|
[768bd556] | 161 | T * pop_head( __queue(T) & this ) { |
---|
[3381ed7] | 162 | verify(*this.tail == 1p); |
---|
[768bd556] | 163 | T * head = this.head; |
---|
[3381ed7] | 164 | if( head != 1p ) { |
---|
[768bd556] | 165 | this.head = get_next( *head ); |
---|
[3381ed7] | 166 | if( get_next( *head ) == 1p ) { |
---|
[768bd556] | 167 | this.tail = &this.head; |
---|
| 168 | } |
---|
| 169 | get_next( *head ) = 0p; |
---|
[3381ed7] | 170 | verify(*this.tail == 1p); |
---|
[dac55004] | 171 | verify( get_next(*head) == 0p ); |
---|
[3381ed7] | 172 | return head; |
---|
[ea7d2b0] | 173 | } |
---|
[3381ed7] | 174 | verify(*this.tail == 1p); |
---|
| 175 | return 0p; |
---|
[ea7d2b0] | 176 | } |
---|
| 177 | |
---|
[768bd556] | 178 | T * remove( __queue(T) & this, T ** it ) with( this ) { |
---|
| 179 | T * val = *it; |
---|
| 180 | verify( val ); |
---|
[ea7d2b0] | 181 | |
---|
[768bd556] | 182 | (*it) = get_next( *val ); |
---|
[ea7d2b0] | 183 | |
---|
[768bd556] | 184 | if( tail == &get_next( *val ) ) { |
---|
| 185 | tail = it; |
---|
| 186 | } |
---|
[ea7d2b0] | 187 | |
---|
[768bd556] | 188 | get_next( *val ) = 0p; |
---|
[ea7d2b0] | 189 | |
---|
[3381ed7] | 190 | verify( (head == 1p) == (&head == tail) ); |
---|
| 191 | verify( *tail == 1p ); |
---|
[768bd556] | 192 | return val; |
---|
| 193 | } |
---|
[8ebbfc4] | 194 | |
---|
[768bd556] | 195 | int ?!=?( const __queue(T) & this, __attribute__((unused)) zero_t zero ) { |
---|
| 196 | return this.head != 0; |
---|
| 197 | } |
---|
[8ebbfc4] | 198 | } |
---|
[ea7d2b0] | 199 | #endif |
---|
[65deb18] | 200 | |
---|
[14a61b5] | 201 | |
---|
| 202 | //----------------------------------------------------------------------------- |
---|
| 203 | // Doubly Linked List |
---|
| 204 | //----------------------------------------------------------------------------- |
---|
| 205 | #ifdef __cforall |
---|
[ffe2fad] | 206 | forall(dtype TYPE) |
---|
[14a61b5] | 207 | #define T TYPE |
---|
[705e612] | 208 | #define __getter_t * [T * & next, T * & prev] ( T & ) |
---|
[14a61b5] | 209 | #else |
---|
[705e612] | 210 | typedef void (*__generit_c_getter_t)(); |
---|
[14a61b5] | 211 | #define T void |
---|
[705e612] | 212 | #define __getter_t __generit_c_getter_t |
---|
[14a61b5] | 213 | #endif |
---|
| 214 | struct __dllist { |
---|
| 215 | T * head; |
---|
[705e612] | 216 | __getter_t __get; |
---|
[14a61b5] | 217 | }; |
---|
| 218 | #undef T |
---|
[705e612] | 219 | #undef __getter_t |
---|
[14a61b5] | 220 | |
---|
| 221 | #ifdef __cforall |
---|
| 222 | #define __dllist_t(T) __dllist(T) |
---|
| 223 | #else |
---|
| 224 | #define __dllist_t(T) struct __dllist |
---|
| 225 | #endif |
---|
| 226 | |
---|
| 227 | #ifdef __cforall |
---|
[768bd556] | 228 | forall(dtype T ) |
---|
[de94a60] | 229 | static inline [void] ?{}( __dllist(T) & this, * [T * & next, T * & prev] ( T & ) __get ) { |
---|
[768bd556] | 230 | this.head{ 0p }; |
---|
[de94a60] | 231 | this.__get = __get; |
---|
| 232 | } |
---|
| 233 | |
---|
[639991a] | 234 | #define next 0 |
---|
| 235 | #define prev 1 |
---|
[768bd556] | 236 | static inline forall(dtype T) { |
---|
| 237 | void push_front( __dllist(T) & this, T & node ) with( this ) { |
---|
| 238 | verify(__get); |
---|
| 239 | if ( head ) { |
---|
| 240 | __get( node ).next = head; |
---|
| 241 | __get( node ).prev = __get( *head ).prev; |
---|
| 242 | // inserted node must be consistent before it is seen |
---|
| 243 | // prevent code movement across barrier |
---|
| 244 | asm( "" : : : "memory" ); |
---|
| 245 | __get( *head ).prev = &node; |
---|
| 246 | T & _prev = *__get( node ).prev; |
---|
| 247 | __get( _prev ).next = &node; |
---|
| 248 | } else { |
---|
| 249 | __get( node ).next = &node; |
---|
| 250 | __get( node ).prev = &node; |
---|
| 251 | } |
---|
| 252 | |
---|
[de94a60] | 253 | // prevent code movement across barrier |
---|
| 254 | asm( "" : : : "memory" ); |
---|
[768bd556] | 255 | head = &node; |
---|
[de94a60] | 256 | } |
---|
| 257 | |
---|
[768bd556] | 258 | void remove( __dllist(T) & this, T & node ) with( this ) { |
---|
| 259 | verify(__get); |
---|
| 260 | if ( &node == head ) { |
---|
| 261 | if ( __get( *head ).next == head ) { |
---|
| 262 | head = 0p; |
---|
| 263 | } else { |
---|
| 264 | head = __get( *head ).next; |
---|
| 265 | } |
---|
[de94a60] | 266 | } |
---|
[768bd556] | 267 | __get( *__get( node ).next ).prev = __get( node ).prev; |
---|
| 268 | __get( *__get( node ).prev ).next = __get( node ).next; |
---|
| 269 | __get( node ).next = 0p; |
---|
| 270 | __get( node ).prev = 0p; |
---|
[de94a60] | 271 | } |
---|
[0c674e8] | 272 | |
---|
[768bd556] | 273 | int ?!=?( const __dllist(T) & this, __attribute__((unused)) zero_t zero ) { |
---|
| 274 | return this.head != 0; |
---|
| 275 | } |
---|
[92e7631] | 276 | |
---|
| 277 | void move_to_front( __dllist(T) & src, __dllist(T) & dst, T & node ) { |
---|
| 278 | remove (src, node); |
---|
| 279 | push_front(dst, node); |
---|
| 280 | } |
---|
[0c674e8] | 281 | } |
---|
[639991a] | 282 | #undef next |
---|
| 283 | #undef prev |
---|
[14a61b5] | 284 | #endif |
---|
| 285 | |
---|
[65deb18] | 286 | //----------------------------------------------------------------------------- |
---|
| 287 | // Tools |
---|
| 288 | //----------------------------------------------------------------------------- |
---|
| 289 | #ifdef __cforall |
---|
| 290 | |
---|
[2233ad4] | 291 | #endif |
---|
[768bd556] | 292 | |
---|
| 293 | // Local Variables: // |
---|
| 294 | // tab-width: 4 // |
---|
| 295 | // End: // |
---|