Imagine you have a big array and that you want to process it by little pieces. One solution would be to make a new array which is displaced to another, and that would be great because you don't use up memory space for nothing, since you donc copy the elements. However imagine, that your process can only handle simple-arrays. Then, using cmucl as I do, this small array can't be displaced-to anymore since it would transform it to a vector. Using subseq is then what I do, but it takes a lot of space.
One would say "why not give as parameter to the processing funtion the big array and an offset ?" Well, I don't really like this, it doesn't feel the write way to do it to me.
So am I wrong with this or is there another solution, ie having a kind of displaced-to array that would be a simple-array ?
On 2004-01-17, Martin Raspaud <martin.rasp...@wanadoo.fr> wrote: | | One would say "why not give as parameter to the processing funtion the | big array and an offset ?" | Well, I don't really like this, it doesn't feel the write way to do it | to me.
Why not? This is how sequence functions like find, replace, remove, etc. handle this situation, by using optional :start and :end arguments.
Martin Raspaud <martin.rasp...@wanadoo.fr> writes: > So am I wrong with this or is there another solution
I'd say you have unreasonable restrictions here. If you want to be able to efficiently process portions of large arrays, use start/end args. Otherwise, you can use displaced arrays, but then you have to remove the simple-array restriction on your utility functions. If you write them so they expect a simple array, *and* they don't take bounds arguments, they're badly written for your purpose.
> , ie having a kind > of displaced-to array that would be a simple-array ?
No, that's exactly the distinction between the types ARRAY and SIMPLE-ARRAY: a SIMPLE-ARRAY has no fill-pointer, and is not displaced. This lets the implementation do more efficient access.
-- /|_ .-----------------------. ,' .\ / | No to Imperialist war | ,--' _,' | Wage class war! | / / `-----------------------' ( -. | | ) | (`-. '--.) `. )----'
Martin Raspaud <martin.rasp...@wanadoo.fr> writes: > One would say "why not give as parameter to the processing funtion the > big array and an offset ?" > Well, I don't really like this, it doesn't feel the write way to do it > to me.
Incidentally, if you just don't like having to pass three args to your processing functions, you could wrap it all up in a class:
* Martin Raspaud | One would say "why not give as parameter to the processing funtion | the big array and an offset ?" Well, I don't really like this, it | doesn't feel the right way to do it to me. | | So am I wrong with this or is there another solution, ie having a kind | of displaced-to array that would be a simple-array ?
One common optimization when working with non-simple arrays is to dig out the underlying simple-array and the start and end positions that a displaced array makes into one convenient object.
The function ARRAY-DISPLACEMENT returns the values of the arguments :DISPLACED-TO and and :DISPLACED-INDEX-OFFSETgiven to MAKE-ARRAY or ADJUST-ARRAY, or NIL and 0 if it was not displaced.
(defun undisplace-array (array) "Return the fundamental array and the start and end positions into it of a displaced array." (let ((length (length array)) (start 0)) (loop (multiple-value-bind (to offset) (array-displacement array) (if to (setq array to start (+ start offset)) (return (values array start (+ start length))))))))
-- Erik Naggum | Oslo, Norway
Act from reason, and failure makes you rethink and study harder. Act from faith, and failure makes you blame someone and push harder.
Erik Naggum wrote: > * Martin Raspaud > | One would say "why not give as parameter to the processing funtion > | the big array and an offset ?" Well, I don't really like this, it > | doesn't feel the right way to do it to me. > | > | So am I wrong with this or is there another solution, ie having a kind > | of displaced-to array that would be a simple-array ?
> One common optimization when working with non-simple arrays is to > dig out the underlying simple-array and the start and end positions > that a displaced array makes into one convenient object.
> The function ARRAY-DISPLACEMENT returns the values of the arguments > :DISPLACED-TO and and :DISPLACED-INDEX-OFFSETgiven to MAKE-ARRAY or > ADJUST-ARRAY, or NIL and 0 if it was not displaced.
[snip]
This is, incidentally, how f2cl (Fortran-to-Lisp converter) handles Fortran arrays. For speed, you want specialized arrays. However, in Fortran, when you call a routine with a slice of an array, f2cl uses a displaced array. The called routine then uses array-displacement as many times as needed to find the original array, which is a specialized array.
This isn't as good a Fortran, but it does get as close as possible.