Skip to content

API Change Request: add List.sortRange and List.shuffleRange #40841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jolleekin opened this issue Mar 1, 2020 · 2 comments
Open

API Change Request: add List.sortRange and List.shuffleRange #40841

jolleekin opened this issue Mar 1, 2020 · 2 comments
Labels
area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. pkg-collection-helpers type-enhancement A request for a change that isn't a bug

Comments

@jolleekin
Copy link

The List interface currently has several methods that operate on a range instead of the whole list such as fillRange, setRange, getRange, removeRange, and replaceRange. However, it doesn't have sortRange and shuffleRange. This makes it less efficient to use in some cases.

For example, in a coding competition like Google Code Jam, it's more efficient to instantiate a list once and reuse it for all test cases instead of instantiating it for every test case. This is only achievable if sortRange and shuffleRange are added.

/// Without `List.sortRange`, the list must be instantiated for every test case.

import 'dart:io';
import 'dart:typed_data';

void main() {
  final testCount = readInt();
  for (var testCase = 1; testCase <= testCount; ++testCase) {
    final n = readInt();
    final list = Int64List(n);
    for (var i = 0; i < n; ++i) {
      list[i] = readInt();
    }
    list.sort((x, y) => y - x);
  }
}
/// With `List.sortRange`, the list can be instantiated once and reused for all test cases.

import 'dart:io';
import 'dart:typed_data';

const maxN = 100000;
final list = Int64List(maxN);

void main() {
  final testCount = readInt();
  for (var testCase = 1; testCase <= testCount; ++testCase) {
    final n = readInt();
    for (var i = 0; i < n; ++i) {
      list[i] = readInt();
    }
    list.sortRange(0, n, (x, y) => y - x);
  }
}

Dart SDK version: 2.7.0

@lrhn
Copy link
Member

lrhn commented Mar 2, 2020

We do not plan to change the List API at this time.
The best solution here would be extension methods supplied by package:collection.

Another option is to add a lazy sublist view, so you can do, say, ListSlice(source, start, end)..sort(). The list created by ListSlice would be a fixed-length list which represents a slice of the source list and operate entirely on that. I'm not aware of an available implementation of this, though.
(That's effectively how the Java List and String APIs work: You can get a lazy slice and do any operation you want on that, rather than every operation having to take a start and end).

@lrhn lrhn added pkg-collection-helpers type-enhancement A request for a change that isn't a bug labels Mar 2, 2020
@jolleekin
Copy link
Author

The particular use case here is a coding competition, which makes your suggestions unfeasible

  1. Using external packages is not allowed
  2. List is much slower than typed lists such as Int64List. For some problems, using List results in time limit error while using typed lists (that are instantiated once) passes all test cases

@keertip keertip added the area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. label Mar 2, 2020
@lrhn lrhn added area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. and removed area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. pkg-collection-helpers type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants