Skip to content

Add .elementAt override to CachingIterable #152476

@plammens

Description

@plammens

Use case

The default implementation of .elementAt is O(n) as it goes through the iterable.
CachingIterable could implement it in amortised O(1) as it is, without any extra complexity, given that it caches results in a list.

Proposal

Possible implementation:

  @override
  E elementAt(int index) {
    while (_results.length <= index) {
      if (!_fillNext()) {
        throw RangeError.index(index, this, 'index', 'Index out of range');
      }
    }
    return _results[index];
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listc: proposalA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.r: fixedIssue is closed as already fixed in a newer versionteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions