Thread overview
Double ended arrays?
Oct 07, 2017
Chirs Forest
Oct 07, 2017
Ilya Yaroshenko
Oct 07, 2017
Jonathan M Davis
Oct 08, 2017
Ali Çehreli
October 07, 2017
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?
October 07, 2017
On Saturday, 7 October 2017 at 07:38:47 UTC, 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?

Mir Algorithm [1] has 2D arrays. Elements can be added to the front/back of each dimension using `concatenation` routine [2]. In the same time it does not support negative indexes.

[1] https://github.com/libmir/mir-algorithm

[2] http://docs.algorithm.dlang.io/latest/mir_ndslice_concatenation.html#.concatenation

Best regards,
Ilya Yaroshenko
October 07, 2017
On Saturday, October 07, 2017 07:38:47 Chirs Forest via Digitalmars-d-learn 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?

Dynamic arrays only support concatenating to the end, and they use size_t for indices and length, and size_t is unsigned. The standard library does the same with any containers that it has as do most 3rd party libraries.

You'll need to either implement what you want yourself or use something from somewhere like code.dlang.org. Based on Ilya's post, it sounds like you may be able to use Mir for what you want, but I'd be very surprised to see any libraries use negative indices for anything - especially since most everything uses size_t for indices.

- Jonathan M Davis

October 07, 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
October 07, 2017
On 10/07/2017 05:02 PM, Steven Schveighoffer wrote:

> 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.

I stole the idea from one of Chuck Allison's DConf talks[1] and used as the example for the Indexing Operators section here:


http://ddili.org/ders/d.en/operator_overloading.html#ix_operator_overloading.opIndex

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

Mine is supposed to compile with 2.076.

> -Steve

Ali

[1] He knows about the theft. :)