[swift-users] array.first vs. array[0]
Jens Alfke
jens at mooseyard.com
Wed Jan 20 16:46:56 CST 2016
> On Jan 20, 2016, at 12:28 PM, Dave Abrahams via swift-users <swift-users at swift.org> wrote:
>
>> "But what about dictionaries?" Ah, but a dictionary key is not an
>> Index (note the capital). The relation between a Collection and an
>> Index is that there must be a constant-time operation to get to a
>> value from an Index.
>
> Well, but that also applies to dictionary keys.
Not necessarily. It does if the dictionary is based on a [well-implemented] hash table. But if it’s implemented as a search tree (as C++’s std::map is), it’s not — lookup time is O(log n).
Quibbling aside, there are significant differences between looking up array items and lookup up dictionary keys. It’s extremely common to look up a key that might validly not exist in the dictionary, but it’s pretty rare to do the same with an array — usually if the array index is out of bounds it’s a programmer error.
—Jens
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160120/a6527a6c/attachment.html>
More information about the swift-users
mailing list