@@ -51,15 +51,46 @@ static int fill_recursive_region(uint8_t *buffer,
5151}
5252
5353/**
54- * Fill a region of a multidimensional buffer with a constant value.
54+ * Helper function for recursive region filling with arange
55+ */
56+ static int fill_recursive_arange (uint8_t * buffer ,
57+ int64_t * strides ,
58+ int8_t ndim ,
59+ int64_t * start ,
60+ int64_t * stop ,
61+ int64_t value ,
62+ uint8_t typesize ,
63+ int dim ,
64+ int64_t current_offset ) {
65+ if (dim == ndim ) {
66+ // We've reached the innermost dimension, copy the value
67+ memcpy (buffer + (current_offset * typesize ), & value , typesize );
68+ // Increment value for arange
69+ return value + 1 ;
70+ }
71+
72+ // Iterate through the current dimension within the region
73+ int64_t val = value ;
74+ for (int64_t i = start [dim ]; i < stop [dim ]; i ++ ) {
75+ int64_t offset = current_offset + i * strides [dim ];
76+ val = fill_recursive_arange (buffer , strides , ndim , start , stop ,
77+ val , typesize , dim + 1 , offset );
78+ if (val < 0 ) return val ;
79+ }
80+ return val ;
81+ }
82+
83+ /**
84+ * Fill a region of a multidimensional buffer with a constant value or linspace.
5585 */
5686int fill_buffer_region (uint8_t * buffer ,
5787 int64_t * buffer_shape ,
5888 int8_t ndim ,
5989 int64_t * start ,
6090 int64_t * stop ,
6191 const void * value ,
62- uint8_t typesize ) {
92+ uint8_t typesize ,
93+ bool arange ) {
6394 // Calculate strides for the buffer
6495 int64_t strides [B2ND_MAX_DIM ];
6596 strides [ndim - 1 ] = 1 ;
@@ -68,8 +99,15 @@ int fill_buffer_region(uint8_t *buffer,
6899 }
69100
70101 // Start the recursive filling
71- return fill_recursive_region (buffer , strides , ndim , start , stop ,
72- value , typesize , 0 , 0 );
102+ if (arange ) {
103+ // If arange is true, fill with increasing values
104+ return fill_recursive_arange (buffer , strides , ndim , start , stop ,
105+ 0 , typesize , 0 , 0 );
106+ }
107+ else {
108+ return fill_recursive_region (buffer , strides , ndim , start , stop ,
109+ value , typesize , 0 , 0 );
110+ }
73111}
74112
75113
@@ -79,8 +117,8 @@ CUTEST_TEST_SETUP(concatenate) {
79117 // Add parametrization
80118 CUTEST_PARAMETRIZE (typesize , uint8_t , CUTEST_DATA (
81119 1 ,
82- // 2,
83- // 4,
120+ 2 ,
121+ 4 ,
84122 8 ,
85123 13 ,
86124 ));
@@ -100,9 +138,9 @@ CUTEST_TEST_SETUP(concatenate) {
100138 {1 , 0 , {50 }, {25 }, {5 }, {20 }, {25 }, {5 }},
101139 {1 , 0 , {2 }, {25 }, {5 }, {49 }, {25 }, {5 }},
102140 // 2-dim
103- {2 , 0 , {50 , 50 }, {25 , 13 }, {5 , 8 }, {50 , 50 }, {25 , 13 }, {5 , 8 }},
104- {2 , 1 , {50 , 50 }, {25 , 13 }, {5 , 8 }, {50 , 50 }, {25 , 13 }, {5 , 8 }},
105- {2 , 0 , {25 , 50 }, {25 , 25 }, {5 , 5 }, {2 , 50 }, {25 , 25 }, {5 , 5 }},
141+ {2 , 0 , {10 , 10 }, {2 , 2 }, {1 , 1 }, {4 , 10 }, {2 , 2 }, {1 , 1 }},
142+ {2 , 1 , {10 , 8 }, {2 , 2 }, {1 , 1 }, {10 , 8 }, {2 , 2 }, {1 , 1 }},
143+ {2 , 0 , {4 , 4 }, {4 , 4 }, {2 , 2 }, {4 , 4 }, {4 , 4 }, {2 , 2 }}, //Fails
106144 {2 , 1 , {25 , 50 }, {25 , 25 }, {5 , 5 }, {25 , 5 }, {25 , 25 }, {5 , 5 }},
107145 // 3-dim
108146 {3 , 0 , {50 , 5 , 50 }, {25 , 13 , 10 }, {5 , 8 , 5 }, {50 , 5 , 50 }, {25 , 13 , 10 }, {5 , 8 , 5 }},
@@ -111,9 +149,9 @@ CUTEST_TEST_SETUP(concatenate) {
111149 {3 , 0 , {5 , 5 , 50 }, {25 , 13 , 10 }, {5 , 8 , 5 }, {51 , 5 , 50 }, {25 , 13 , 10 }, {5 , 8 , 5 }},
112150 // Inner 0-dims are supported
113151 {3 , 1 , {50 , 1 , 50 }, {25 , 13 , 10 }, {5 , 8 , 5 }, {50 , 0 , 50 }, {25 , 13 , 10 }, {5 , 8 , 5 }},
114- // TODO: the next is not working yet
152+ // // TODO: the next is not working yet
115153 // {3, 2, {50, 50, 0}, {25, 13, 10}, {5, 8, 5}, {50, 50, 49}, {25, 13, 10}, {5, 8, 5}},
116- // 4-dim
154+ // // 4-dim
117155 {4 , 0 , {5 , 5 , 5 , 5 }, {2 , 5 , 10 , 5 }, {5 , 2 , 5 , 2 }, {5 , 5 , 5 , 5 }, {5 , 5 , 10 , 5 }, {5 , 2 , 5 , 2 }},
118156 {4 , 1 , {5 , 5 , 5 , 5 }, {2 , 5 , 10 , 5 }, {5 , 2 , 5 , 2 }, {5 , 5 , 5 , 5 }, {5 , 5 , 10 , 5 }, {5 , 2 , 5 , 2 }},
119157 {4 , 2 , {5 , 5 , 5 , 5 }, {2 , 13 , 10 , 5 }, {5 , 8 , 5 , 2 }, {5 , 5 , 5 , 5 }, {5 , 13 , 10 , 5 }, {5 , 8 , 5 , 2 }},
@@ -122,18 +160,22 @@ CUTEST_TEST_SETUP(concatenate) {
122160 {4 , 1 , {5 , 5 , 5 , 5 }, {2 , 13 , 10 , 5 }, {5 , 8 , 5 , 2 }, {5 , 6 , 5 , 5 }, {15 , 13 , 10 , 5 }, {5 , 8 , 5 , 2 }},
123161 {4 , 2 , {5 , 5 , 5 , 5 }, {2 , 13 , 10 , 5 }, {5 , 8 , 5 , 2 }, {5 , 5 , 6 , 5 }, {15 , 13 , 10 , 5 }, {5 , 8 , 5 , 2 }},
124162 {4 , 3 , {5 , 5 , 5 , 5 }, {2 , 13 , 10 , 5 }, {5 , 8 , 5 , 2 }, {5 , 5 , 5 , 6 }, {15 , 13 , 10 , 5 }, {5 , 8 , 5 , 2 }},
125-
163+ //
126164 ));
127165 CUTEST_PARAMETRIZE (fill_value , int8_t , CUTEST_DATA (
128166 3 ,
129167 -5 ,
130- // 113,
131- // 33,
168+ 113 ,
169+ 33 ,
132170 ));
133171 CUTEST_PARAMETRIZE (copy , bool , CUTEST_DATA (
134172 true,
135173 false,
136174 ));
175+ CUTEST_PARAMETRIZE (arange , bool , CUTEST_DATA (
176+ true,
177+ false,
178+ ));
137179
138180}
139181
@@ -143,6 +185,7 @@ CUTEST_TEST_TEST(concatenate) {
143185 CUTEST_GET_PARAMETER (typesize , uint8_t );
144186 CUTEST_GET_PARAMETER (fill_value , int8_t );
145187 CUTEST_GET_PARAMETER (copy , bool );
188+ CUTEST_GET_PARAMETER (arange , bool );
146189
147190 int axis = shapes .axis ;
148191 char * urlpath = "test_concatenate.b2frame" ;
@@ -188,11 +231,7 @@ CUTEST_TEST_TEST(concatenate) {
188231
189232 /* Create src2 with a value */
190233 b2nd_array_t * src2 ;
191- uint8_t * value = malloc (typesize );
192- // Fill a buffer with fill_value
193- for (int i = 0 ; i < typesize ; ++ i ) {
194- value [i ] = fill_value ;
195- }
234+
196235 blosc2_storage b2_storage2 = {.cparams = & cparams };
197236 if (backend .persistent ) {
198237 b2_storage2 .urlpath = urlpath2 ;
@@ -201,8 +240,31 @@ CUTEST_TEST_TEST(concatenate) {
201240 b2nd_context_t * ctx2 = b2nd_create_ctx (& b2_storage2 , shapes .ndim , shapes .shape2 ,
202241 shapes .chunkshape2 , shapes .blockshape2 ,
203242 NULL , 0 , NULL , 0 );
204- B2ND_TEST_ASSERT (b2nd_full (ctx2 , & src2 , value ));
205243
244+ uint8_t * value = malloc (typesize );
245+ // Fill a buffer with fill_value
246+ for (int i = 0 ; i < typesize ; ++ i ) {
247+ value [i ] = fill_value ;
248+ }
249+
250+ if (arange ) {
251+ int64_t buffsize = typesize ;
252+ for (int i = 0 ; i < shapes .ndim ; ++ i ) {
253+ buffsize *= shapes .shape2 [i ]; // Multiply by each dimension
254+ }
255+ // Allocate a buffer for the src2 array
256+ uint8_t * buff = malloc (buffsize );
257+ int64_t start_ [B2ND_MAX_DIM ] = {0 };
258+
259+ fill_buffer_region (buff , shapes .shape2 , shapes .ndim ,
260+ start_ , shapes .shape2 , value , typesize , arange ); // value is not used in this case
261+ B2ND_TEST_ASSERT (b2nd_from_cbuffer (ctx2 , & src2 , buff , buffsize ));
262+
263+ }
264+
265+ else {
266+ B2ND_TEST_ASSERT (b2nd_full (ctx2 , & src2 , value ));
267+ }
206268 /* Concatenate src1 and src2 */
207269 b2nd_array_t * array = NULL ;
208270 blosc2_storage b2_storage = {.cparams = & cparams };
@@ -234,7 +296,7 @@ CUTEST_TEST_TEST(concatenate) {
234296
235297 // Fill the region with the value
236298 fill_buffer_region (helperbuffer , helpershape , shapes .ndim ,
237- start_src2 , stop_src2 , value , typesize );
299+ start_src2 , stop_src2 , value , typesize , arange ); // value is not used if arange is true
238300
239301 // Check the shape of the concatenated array
240302 for (int i = 0 ; i < ctx -> ndim ; ++ i ) {
@@ -296,6 +358,15 @@ CUTEST_TEST_TEST(concatenate) {
296358 CUTEST_ASSERT ("Data in the concatenated array does not match the helperbuffer" , is_true );
297359 }
298360
361+ // for (int i = 0; i < buffersize; i++) {
362+ // if (i%typesize == 0) {
363+ // fprintf(stderr, "\n");
364+ // if (i%(typesize*shapes.shape1[0]) == 0) {fprintf(stderr, "--------------------------------\n");}
365+ // fprintf(stderr, "%d ", buffer[i]);
366+ // }
367+ // }
368+
369+
299370 /* Free mallocs */
300371 free (buffer_fill );
301372 free (buffer );
0 commit comments