www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to implement immutable ring buffer?

reply drug <drug2004 bk.ru> writes:
Could somebody share his thoughts on the subject?
Would it be efficient? Is it possible to avoid memory copying to provide 
immutability? To avoid cache missing ring buffer should be like array, 
not list, so it's possible that the whole buffer should be moved. Is it 
neccessary to be real immutable, may be some form of fake immutabitiy is 
enough?
I'd like to try immutability in my app, and ring buffer is what I don't 
know how to use in the realm of immutability.

Thanks
May 27 2015
next sibling parent reply "thedeemon" <dlang thedeemon.com> writes:
This whole idea sounds self-contradictory.
Ring buffer is a mutable-array-based implementation of something, 
for example of a queue. You can ask about immutable 
implementations of a queue, but that would be another question, 
not involving a ring buffer.

What do you want to do with this buffer, what do you need it for?
May 27 2015
parent drug <drug2004 bk.ru> writes:
On 27.05.2015 11:04, thedeemon wrote:
 This whole idea sounds self-contradictory.
 Ring buffer is a mutable-array-based implementation of something, for
 example of a queue. You can ask about immutable implementations of a
 queue, but that would be another question, not involving a ring buffer.

 What do you want to do with this buffer, what do you need it for?
In general I need to have the last n instances of some objects queue. Now it implemented using ring buffer. Probably, you're right about immutable queue, it is more suitable definition here.
May 27 2015
prev sibling parent reply Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
On Wed, 27 May 2015 09:20:52 +0000
drug via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 Could somebody share his thoughts on the subject?
 Would it be efficient? Is it possible to avoid memory copying to
 provide immutability? To avoid cache missing ring buffer should be
 like array, not list, so it's possible that the whole buffer should
 be moved. Is it neccessary to be real immutable, may be some form of
 fake immutabitiy is enough?
 I'd like to try immutability in my app, and ring buffer is what I
 don't know how to use in the realm of immutability.
 
 Thanks
Maybe http://dlang.org/phobos/std_typecons.html#.Rebindable could help you.
May 27 2015
parent drug <drug2004 bk.ru> writes:
On 27.05.2015 11:10, Daniel Kozák via Digitalmars-d-learn wrote:
 On Wed, 27 May 2015 09:20:52 +0000
 drug via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 Could somebody share his thoughts on the subject?
 Would it be efficient? Is it possible to avoid memory copying to
 provide immutability? To avoid cache missing ring buffer should be
 like array, not list, so it's possible that the whole buffer should
 be moved. Is it neccessary to be real immutable, may be some form of
 fake immutabitiy is enough?
 I'd like to try immutability in my app, and ring buffer is what I
 don't know how to use in the realm of immutability.

 Thanks
Maybe http://dlang.org/phobos/std_typecons.html#.Rebindable could help you.
I guess it won't be efficient, because it would be ring buffer of (in some form) pointers. It's suitable if elements of the buffer are processed independently each other. In other case cache misses are provided, I think.
May 27 2015