digitalmars.D.learn - Double ended arrays?
- Chirs Forest (6/6) Oct 07 2017 I have some data that I want to store in a dynamic 2d array...
- Ilya Yaroshenko (9/15) Oct 07 2017 Mir Algorithm [1] has 2D arrays. Elements can be added to the
- Jonathan M Davis (11/17) Oct 07 2017 Dynamic arrays only support concatenating to the end, and they use size_...
- Steven Schveighoffer (14/19) Oct 07 2017 Dcollections has something like this, a deque. It doesn't use negative
- =?UTF-8?Q?Ali_=c3=87ehreli?= (9/18) Oct 07 2017 I stole the idea from one of Chuck Allison's DConf talks[1] and used as
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?
Oct 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] Best regards, Ilya Yaroshenko
Oct 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
Oct 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
Oct 07 2017
On 10/07/2017 05:02 PM, Steven Schveighoffer wrote:https://github.com/schveiguy/dcollections/blob/master/dcollections/Deque.dIt'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.opIndexNote: that code is many years old, so it may not compile with the latest compiler.Mine is supposed to compile with 2.076.-SteveAli [1] He knows about the theft. :)
Oct 07 2017