Re: Shift operation for array elements

2016-09-30 Thread Krux02
I would like to throw in my implementation that is inspired by `std::transform` from c++. proc genRangeswap[Coll](data: var Coll; s0, s1, N: Natural): void = for i in 0 ..< N: swap(data[s0+i], data[s1+i]) proc genTransform[Coll](data: var Coll; first, middle, l

Re: Shift operation for array elements

2016-09-25 Thread Krux02
I would call it transform like in `std::transform` from c++ ``. But then it should also rotate elements, meaning that elements dropping out from the left, should also reenter from the right. And to complete the functionality of std::transform, it should also be possible to pass subranges of a se

Re: Shift operation for array elements

2016-09-25 Thread stisa
@jlp765 : yeah probably, and stefan said that, but I find this interesting :) So, based on [this thread](http://forum.nim-lang.org///forum.nim-lang.org/t/1471/2) , using reset on the elements we move out of the array should handle ref etc. properly. The code becomes this: proc sh

Re: Shift operation for array elements

2016-09-25 Thread jlp765
If you are wanting to remove elements from the start of the seq and shift data elements to the front, shouldn't you be [using a queue](http://forum.nim-lang.org///nim-lang.org/docs/queues.html) rather than a sequence?

Re: Shift operation for array elements

2016-09-24 Thread OderWat
I was thinking about refs too and I kinda doubt that just moving the refs in memory works. Sequtils usually uses `shallowCopy()` for manipulation of elements in the seq. Which imho avoids (deep) copying a string or seq inside the seq and copies the "pointer" (reference). I guess OP shift() proc

Re: Shift operation for array elements

2016-09-24 Thread stisa
@OderWat: do you mean something like this? proc shift[T](a: var openarray[T]; d: int) = if d == 0: discard # do nothing elif abs(d) >= a.len: # reset all zeroMem(addr a[0], a.len*sizeof(T)) elif d > 0: # right shift moveMem(addr a[d], addr a[0], (a.le

Re: Shift operation for array elements

2016-09-24 Thread Stefan_Salewski
@OderWat Sorry, can not really understand... Somethink like memCopy or memMove? That was my first idea, but I was very unsure what would happen when array elements are not somethink like plain ints, but objects with refs. Would that confuse the GC? And of course one may ask if array shifting is

Re: Shift operation for array elements

2016-09-24 Thread OderWat
Would "shifting" a seq or even an array not just be one operation operation for all elements at once and then initialisation of the remaining fields with 0 because an array/seq has continuous memory layout and the default init is "zeroing memory" afaik.

Re: Shift operation for array elements

2016-09-24 Thread Stefan_Salewski
Yes, for some languages shift may mean returning an element. But in Nim we have shl and shr operators. scroll may be confused with rotation? I will look at your code in more detail tonigth -- you are using magic reset proc from system module. I tried also a year ago, then I asked Araq in IRC ab

Re: Shift operation for array elements

2016-09-24 Thread stisa
Nice, though I'm used to shift meaning "remove the first value of the (resizable) array and return the value", I think your proc feels more like it's "scrolling" the array? Anyway, naming aside, what do you think of this version? proc scroll[T](a: var openarray[T]; d: int) =

Shift operation for array elements

2016-09-23 Thread Stefan_Salewski
Do we have a shift operation for array elements in Nim like this one? Well, now we have. The filling with default value: I am using a compiler initialized var to get the default. Is that the suggested way? proc shift[T](a: var openarray[T]; d: int) = ## Shift a right by d