Creating dynamic arrays of known size

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Mar 8 22:05:57 PST 2012


On Thu, Mar 08, 2012 at 09:13:13PM -0800, H. S. Teoh wrote:
> So, I'm plodding along with my AA implementation that *hopefully* will
> eventually reach the point where it's usable enough to be dropped into
> druntime. I'm writing .keys and .values right now, and wondering what's
> the best way to construct the returned array.
> 
> Obviously, using =~ repeatedly is a bad idea, since we already know
> the resulting array size. Would this be the best way to do it?
> 
> 	Key[] keys;
> 	keys.length = num_keys;
> 	for (size_t i=0; i < num_keys; i++) {
> 		keys[i] = ...;
> 	}
> 
> Looking at aaA.d, I see that _aaKeys calls gc_malloc directly and sets
> BlkAttr.NO_SCAN.  Should I just copy this code?
[...]

Another problem: if Key is a const/immutable type, then keys[i] is
immutable, so the keys can't be copied into the resulting array. I tried
inout but it doesn't seem to be good enough, because individual array
elements need to be assigned to, which violates const/immutable, even
though we're really just copying const/immutable data here.


T

-- 
He who sacrifices functionality for ease of use, loses both and deserves neither. -- Slashdotter


More information about the Digitalmars-d mailing list