www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - rbtree to array?

reply Pete Padil <padil gm.com> writes:
Was wondering if there is a way to get the data inside an rbtree 
as a simple array or a std.array, besides manually copying? 
Apologies if the answer is obvious and I missed it.
May 05
parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Tuesday, 6 May 2025 at 04:57:46 UTC, Pete Padil wrote:
 Was wondering if there is a way to get the data inside an 
 rbtree as a simple array or a std.array, besides manually 
 copying? Apologies if the answer is obvious and I missed it.
Rbtree has a range interface of d. Its memory layout is similar to a linked list, where the data is not contiguous. So, you have to copy the elements manually. But it is always a nice practice to use library functions, doing it for you. ```d import std.container.rbtree; import std.array; auto rbt = redBlackTree(3, 1, 4, 2, 5); auto arr = rbt[].array; writeln(arr); ```
May 05
parent Pete Padil <padil gm.com> writes:
On Tuesday, 6 May 2025 at 06:50:36 UTC, Ferhat Kurtulmuş wrote:
 Rbtree has a range interface of d. Its memory layout is similar 
 to a linked list, where the data is not contiguous. So, you 
 have to copy the elements manually. But it is always a nice 
 practice to use library functions, doing it for you.

 ```d
     import std.container.rbtree;
     import std.array;

     auto rbt = redBlackTree(3, 1, 4, 2, 5);
     auto arr = rbt[].array;

     writeln(arr);
 ```
Excellent - thank you!
May 06