Inserting and removing elements from a sorted container
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Sun Nov 19 16:05:53 UTC 2017
On Sunday, November 19, 2017 13:41:51 Dirk via Digitalmars-d-learn wrote:
> Hi!
>
> I want to add an uint into a container, but avoid duplicate
> uints, similar to a set<> from C++ STL.
>
> To find out if an uint is already present in the container, it
> would make sense if the container is sorted.
>
> This is some pseudo-D-code that should make clear what i want to
> do:
>
> auto sortedlist = SList!uint(1, 2, 3, 5);
> uint newValue = 4;
> bool appendValue = true;
> foreach( i; sortedlist[] ) {
> if( value == i ) {
> // value already in list
> appendValue = false;
> break;
> }
> if( i > newValue ) {
> // value not there. Insert value in front of i
> sortedlist.insertBefore(i, newValue);
> appendValue = false;
> }
> }
> if( appendValue )
> sortedlist.append( newValue );
>
> Can someone help me?
>
> Thank you,
> Dirk
I'd suggest that you use std.container.rbtree..RedBlackTree. A red-black
tree exactly the sort of data structure that is typically used in a sorted
set.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list