Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Dart, does `List.unmodifiable` create an unmodifiable view, or a whole new independent list?

  1. In Dart, do List.unmodifiable(someList) and Map.unmodifiable(someMap) create an unmodifiable view, or a whole new independent list/map? I am guessing it's a new independent list, but I must be sure.
    • If it's a view, modifications to the original list will modify the now not so unmodifiable list, but sometimes you are just discarding the original anyway, and creating a view is much faster.
  2. And, in case they create independent lists/maps, is there some way to create an unmodifiable view, for performance purposes?
    • Update: Ok, I have answered my own question number 2, by finding this: UnmodifiableListView<E> Docs. But I still need to know the answer to the other questions.
  3. What's the Big O for creating a dart list from another one, including the one created by List.unmodifiable(someList)? For example, this is the info for Java collections: Big O for Java Collections.
like image 331
MarcG Avatar asked May 13 '18 01:05

MarcG


1 Answers

  1. The items are copied. It's a whole new independent list.
  2. See Dart Collection Library, more specifically:
    • UnmodifiableListView
    • UnmodifiableMapView
  3. It's O(N) – it copies all elements, so it has to visit each element.
    • If you use List.unmodifiable from another list, it knows the target length, so it won't have to resize the list it's creating as it's iterating.
like image 160
Kevin Moore Avatar answered Nov 06 '22 01:11

Kevin Moore