Double ended arrays?

Steven Schveighoffer schveiguy at yahoo.com
Sun Oct 8 00:02:18 UTC 2017


On 10/7/17 3:38 AM, Chirs Forest wrote:
> I have some data that I want to store in a dynamic 2d array... I'd like 
> to be able to add elements to the front of the array and access those 
> elements with negative integers as well as add numbers to the back that 
> I'd acess normally with positive integers. Is this something I can do, 
> or do I have to build a container to handle what I want?

Dcollections has something like this, a deque. It doesn't use negative 
integers to access the prepended elements, but I suppose it could be 
made to do this.

See here:

https://github.com/schveiguy/dcollections/blob/master/dcollections/Deque.d

It's implemented by maintaining 2 dynamic arrays, one that is "reversed" 
at the front, and one that is normal at the back. When you prepend, it 
appends to the "reverse" array.

It's probably not the most efficient, but it does maintain the correct 
complexities.

Note: that code is many years old, so it may not compile with the latest 
compiler.

-Steve


More information about the Digitalmars-d-learn mailing list