|
From: | Daniel J Sebald |
Subject: | Re: Question about Array::index with resizing |
Date: | Wed, 22 May 2013 23:14:15 -0500 |
User-agent: | Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111108 Fedora/3.1.16-1.fc14 Thunderbird/3.1.16 |
On 05/22/2013 09:06 PM, Michael Goffioul wrote:
Could anybody enlighten me about the implementation of Array<T> Array<T>::index (const Array<idx_vector>& ia, bool resize_ok, const T& rfv) const I'm trying to use it in the classdef branch, assuming this function would resize the array is the given index is out of bound. In my case, "ia" contains only scalars, so the variable "all_scalars" ends up being true. But then the code does: if (all_scalars) return Array<T> (dim_vector (1, 1), rfv); In other words, the code always return an array with a single element being the resize_fill_value. No resizing or array indexing actually takes place. Is it intentional? Unimplemented? Or am I missing something?
My guess is that it's probably intentional in the sense it is the best that can be done without issuing some type of error. There is resizing, but only provided it can be done properly.
This hunk of code: for (int i = 0; i < ial; i++) dvx(i) = ia(i).extent (dv (i)); if (! (dvx == dv))verifies that the index arrays are all within proper extent. If the two vectors don't match, then something was outside of extent. And if they don't match and every one of the indeces is a scalar, apparently there is no way to resize, so a bogus value is being returned in lieu of issuing an error. Otherwise if there is an index vector in the list somewhere, a resize is attempted (but with no guarantee of success). If the resize fails, it is caught by the next conditional:
if (tmp.dimensions != dvx) return Array<T> ();and gives another type of bogus result slightly different from the all-scalar case. Maybe some type of error checking by returning two different bogus results under improper indexing? If the resize was successful the unchecked version of
tmp.index (ia);is used where tmp is a copy of the original Array. I think most of this is simply verifying that the index array can be applied in a valid way without attempting to access Array in a way that could cause a segmentation fault. The expectation is probably that the programmer is going to pass in a valid index vector array, using error checking with error messages ahead of doing so.
Dan
[Prev in Thread] | Current Thread | [Next in Thread] |