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 | //
|
---|
7 | // bits/containers.h -- Intrusive generic containers.h
|
---|
8 | //
|
---|
9 | // Author : Thierry Delisle
|
---|
10 | // Created On : Tue Oct 31 16:38:50 2017
|
---|
11 | // Last Modified By : --
|
---|
12 | // Last Modified On : --
|
---|
13 | // Update Count : 0
|
---|
14 |
|
---|
15 | #pragma once
|
---|
16 |
|
---|
17 | #include "bits/defs.h"
|
---|
18 | #include "libhdr.h"
|
---|
19 |
|
---|
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))
|
---|
46 | static inline T& ?[?]( __small_array(T) & this, __lock_size_t idx) {
|
---|
47 | return ((typeof(this.data))this.data)[idx];
|
---|
48 | }
|
---|
49 |
|
---|
50 | forall(dtype T | sized(T))
|
---|
51 | static inline T& ?[?]( const __small_array(T) & this, __lock_size_t idx) {
|
---|
52 | return ((typeof(this.data))this.data)[idx];
|
---|
53 | }
|
---|
54 |
|
---|
55 | forall(dtype T | sized(T))
|
---|
56 | static inline T* begin( const __small_array(T) & this ) {
|
---|
57 | return ((typeof(this.data))this.data);
|
---|
58 | }
|
---|
59 |
|
---|
60 | forall(dtype T | sized(T))
|
---|
61 | static inline T* end( const __small_array(T) & this ) {
|
---|
62 | return ((typeof(this.data))this.data) + this.size;
|
---|
63 | }
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | //-----------------------------------------------------------------------------
|
---|
67 | // Node Base
|
---|
68 | //-----------------------------------------------------------------------------
|
---|
69 |
|
---|
70 | #ifdef __cforall
|
---|
71 | trait is_node(dtype T) {
|
---|
72 | T*& get_next( T& );
|
---|
73 | };
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | //-----------------------------------------------------------------------------
|
---|
77 | // Stack
|
---|
78 | //-----------------------------------------------------------------------------
|
---|
79 | #ifdef __cforall
|
---|
80 | forall(dtype TYPE | is_node(TYPE))
|
---|
81 | #define T TYPE
|
---|
82 | #else
|
---|
83 | #define T void
|
---|
84 | #endif
|
---|
85 | struct __stack {
|
---|
86 | T * top;
|
---|
87 | };
|
---|
88 | #undef T
|
---|
89 |
|
---|
90 | #ifdef __cforall
|
---|
91 | #define __stack_t(T) __stack(T)
|
---|
92 | #else
|
---|
93 | #define __stack_t(T) struct __stack
|
---|
94 | #endif
|
---|
95 |
|
---|
96 | #ifdef __cforall
|
---|
97 | forall(dtype T | is_node(T))
|
---|
98 | static inline void ?{}( __stack(T) & this ) {
|
---|
99 | (this.top){ NULL };
|
---|
100 | }
|
---|
101 |
|
---|
102 | forall(dtype T | is_node(T) | sized(T))
|
---|
103 | static inline void push( __stack(T) & this, T * val ) {
|
---|
104 | verify( !get_next( *val ) );
|
---|
105 | get_next( *val ) = this.top;
|
---|
106 | this.top = val;
|
---|
107 | }
|
---|
108 |
|
---|
109 | forall(dtype T | is_node(T) | sized(T))
|
---|
110 | static inline T * pop( __stack(T) & this ) {
|
---|
111 | T * top = this.top;
|
---|
112 | if( top ) {
|
---|
113 | this.top = get_next( *top );
|
---|
114 | get_next( *top ) = NULL;
|
---|
115 | }
|
---|
116 | return top;
|
---|
117 | }
|
---|
118 | #endif
|
---|
119 |
|
---|
120 | //-----------------------------------------------------------------------------
|
---|
121 | // Queue
|
---|
122 | //-----------------------------------------------------------------------------
|
---|
123 | #ifdef __cforall
|
---|
124 | forall(dtype TYPE | is_node(TYPE))
|
---|
125 | #define T TYPE
|
---|
126 | #else
|
---|
127 | #define T void
|
---|
128 | #endif
|
---|
129 | struct __queue {
|
---|
130 | T * head;
|
---|
131 | T ** tail;
|
---|
132 | };
|
---|
133 | #undef T
|
---|
134 |
|
---|
135 | #ifdef __cforall
|
---|
136 | #define __queue_t(T) __queue(T)
|
---|
137 | #else
|
---|
138 | #define __queue_t(T) struct __queue
|
---|
139 | #endif
|
---|
140 |
|
---|
141 | #ifdef __cforall
|
---|
142 | forall(dtype T | is_node(T))
|
---|
143 | static inline void ?{}( __queue(T) & this ) {
|
---|
144 | (this.head){ NULL };
|
---|
145 | (this.tail){ &this.head };
|
---|
146 | }
|
---|
147 |
|
---|
148 | forall(dtype T | is_node(T) | sized(T))
|
---|
149 | static inline void append( __queue(T) & this, T * val ) {
|
---|
150 | verify(this.tail != NULL);
|
---|
151 | *this.tail = val;
|
---|
152 | this.tail = &get_next( *val );
|
---|
153 | }
|
---|
154 |
|
---|
155 | forall(dtype T | is_node(T) | sized(T))
|
---|
156 | static inline T * pop_head( __queue(T) & this ) {
|
---|
157 | T * head = this.head;
|
---|
158 | if( head ) {
|
---|
159 | this.head = get_next( *head );
|
---|
160 | if( !get_next( *head ) ) {
|
---|
161 | this.tail = &this.head;
|
---|
162 | }
|
---|
163 | get_next( *head ) = NULL;
|
---|
164 | }
|
---|
165 | return head;
|
---|
166 | }
|
---|
167 |
|
---|
168 | forall(dtype T | is_node(T) | sized(T))
|
---|
169 | static inline T * remove( __queue(T) & this, T ** it ) {
|
---|
170 | T * val = *it;
|
---|
171 | verify( val );
|
---|
172 |
|
---|
173 | (*it) = get_next( *val );
|
---|
174 |
|
---|
175 | if( this.tail == &get_next( *val ) ) {
|
---|
176 | this.tail = it;
|
---|
177 | }
|
---|
178 |
|
---|
179 | get_next( *val ) = NULL;
|
---|
180 |
|
---|
181 | verify( (this.head == NULL) == (&this.head == this.tail) );
|
---|
182 | verify( *this.tail == NULL );
|
---|
183 | return val;
|
---|
184 | }
|
---|
185 | #endif
|
---|