www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Array types and index types

reply Liam McGillivray <yoshi.pit.link.mario gmail.com> writes:
In D, it appears that dynamic arrays (at least by default) use a 
ulong as their key type. They are declared like this:
```
string[] dynamicArray;
```

I imagine that using a 64-bit value as the key would be slower 
than using 32 bits or 16 bits, and 64 bits is way overkill for 
nearly everything. However, if I declare the array as 
`string[uint] myArray;` or `string[ushort] myArray`, it's treated 
as an associative array. This means that I don't get to do the 
things I want to do with a dynamic array, such as appending.

So I have some questions:

Is there a way to declare a dynamic array with a uint, ushort, or 
ubyte key?

If there was, would it really be faster?

Is an associative array with a ushort key faster than a dynamic 
array with a ulong key?
Feb 27
parent "H. S. Teoh" <hsteoh qfbox.info> writes:
On Wed, Feb 28, 2024 at 03:00:55AM +0000, Liam McGillivray via
Digitalmars-d-learn wrote:
 In D, it appears that dynamic arrays (at least by default) use a ulong
 as their key type. They are declared like this:
 ```
 string[] dynamicArray;
 ```
 
 I imagine that using a 64-bit value as the key would be slower than
 using 32 bits or 16 bits,
Wrong. The machine uses 64 bits internally anyway. Well, 48 on i386. But the point is that there is no speed difference. Also, on 32-bit architectures size_t is aliased to uint, which is 32 bits. [...]
 So I have some questions:
 
 Is there a way to declare a dynamic array with a uint, ushort, or ubyte key?
No.
 If there was, would it really be faster?
No.
 Is an associative array with a ushort key faster than a dynamic array
 with a ulong key?
No. T -- Error: Keyboard not attached. Press F1 to continue. -- Yoon Ha Lee, CONLANG
Feb 27