Skip to content

Can size be made O(1)? #138

Open
@neongreen

Description

@neongreen

It is somewhat unfortunate that size is O(n) for HashMap and HashSet:

  • If a faster size is desired, I have to store it separately in my code and make sure that I update it whenever I do an insert or delete. With filtering functions, union, etc. it gets even more cumbersome.

  • Even experienced programmers often assume that size is O(1) without checking, which leads to performance bugs :(

(I have looked at the code, but I have to confess that I don't understand how HashMap/Set work and so it's hard for me to assess how much of a performance/memory penalty adding a size field to the constructors would be, or whether it is possible at all.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions