2014-09-19 16:38:37 -07:00
|
|
|
library facade.collection;
|
|
|
|
|
2014-11-09 18:24:28 -05:00
|
|
|
import 'dart:collection' show HashMap, IterableBase, Iterator;
|
2014-10-02 20:39:27 -07:00
|
|
|
export 'dart:core' show Map, List, Set;
|
2015-01-22 11:52:36 +01:00
|
|
|
import 'dart:math' show max, min;
|
2014-09-19 16:38:37 -07:00
|
|
|
|
2014-11-09 18:24:28 -05:00
|
|
|
class MapIterator extends Iterator<List> {
|
|
|
|
Iterator _iterator;
|
|
|
|
Map _map;
|
|
|
|
|
|
|
|
MapIterator(Map map) {
|
|
|
|
this._map = map;
|
|
|
|
this._iterator = map.keys.iterator;
|
|
|
|
}
|
|
|
|
bool moveNext() {
|
|
|
|
return this._iterator.moveNext();
|
|
|
|
}
|
|
|
|
List get current {
|
|
|
|
return this._iterator.current != null ?
|
|
|
|
[this._iterator.current, this._map[this._iterator.current]] :
|
|
|
|
null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class IterableMap extends IterableBase<List> {
|
|
|
|
Map _map;
|
|
|
|
|
|
|
|
IterableMap(Map map) {
|
|
|
|
this._map = map;
|
|
|
|
}
|
|
|
|
Iterator<List> get iterator => new MapIterator(this._map);
|
|
|
|
}
|
|
|
|
|
2014-09-19 16:38:37 -07:00
|
|
|
class MapWrapper {
|
|
|
|
static HashMap create() => new HashMap();
|
2014-12-09 10:31:19 -08:00
|
|
|
static HashMap clone(Map m) => new HashMap.from(m);
|
2014-11-11 17:33:47 -08:00
|
|
|
static HashMap createFromStringMap(m) => m;
|
2014-11-05 13:48:36 -08:00
|
|
|
static HashMap createFromPairs(List pairs) {
|
|
|
|
return pairs.fold({}, (m, p){
|
|
|
|
m[p[0]] = p[1];
|
|
|
|
return m;
|
|
|
|
});
|
|
|
|
}
|
2014-09-19 16:38:37 -07:00
|
|
|
static get(m, k) => m[k];
|
|
|
|
static void set(m, k, v){ m[k] = v; }
|
|
|
|
static contains(m, k) => m.containsKey(k);
|
2014-09-30 14:56:33 -04:00
|
|
|
static forEach(m, fn) {
|
2014-10-18 17:50:55 -04:00
|
|
|
m.forEach((k,v) => fn(v,k));
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
2014-10-20 15:17:06 -04:00
|
|
|
static int size(m) {return m.length;}
|
2014-10-28 18:56:15 +01:00
|
|
|
static void delete(m, k) { m.remove(k); }
|
|
|
|
static void clear(m) { m.clear(); }
|
2014-11-09 18:24:28 -05:00
|
|
|
static Iterable iterable(m) { return new IterableMap(m); }
|
2014-12-11 00:30:21 +01:00
|
|
|
static Iterable keys(m) { return m.keys; }
|
|
|
|
static Iterable values(m) { return m.values; }
|
2014-09-19 16:38:37 -07:00
|
|
|
}
|
|
|
|
|
2014-10-28 14:46:38 -07:00
|
|
|
// TODO: how to export StringMap=Map as a type?
|
|
|
|
class StringMapWrapper {
|
|
|
|
static HashMap create() => new HashMap();
|
|
|
|
static get(map, key) {
|
|
|
|
return map[key];
|
|
|
|
}
|
|
|
|
static set(map, key, value) {
|
|
|
|
map[key] = value;
|
|
|
|
}
|
2014-11-11 17:33:47 -08:00
|
|
|
static forEach(m, fn) {
|
|
|
|
m.forEach((k,v) => fn(v,k));
|
|
|
|
}
|
|
|
|
static isEmpty(m) {
|
|
|
|
return m.isEmpty;
|
|
|
|
}
|
2014-10-28 14:46:38 -07:00
|
|
|
}
|
|
|
|
|
2014-09-19 16:38:37 -07:00
|
|
|
class ListWrapper {
|
|
|
|
static List clone(List l) => new List.from(l);
|
|
|
|
static List create() => new List();
|
2014-09-30 14:56:33 -04:00
|
|
|
static List createFixedSize(int size) => new List(size);
|
2014-09-19 16:38:37 -07:00
|
|
|
static get(m, k) => m[k];
|
|
|
|
static void set(m, k, v) { m[k] = v; }
|
2014-10-27 13:02:46 -04:00
|
|
|
static contains(List m, k) => m.contains(k);
|
2014-09-30 14:56:33 -04:00
|
|
|
static map(list, fn) => list.map(fn).toList();
|
2014-10-27 11:47:13 -04:00
|
|
|
static filter(List list, fn) => list.where(fn).toList();
|
2014-10-14 16:00:35 -04:00
|
|
|
static find(List list, fn) => list.firstWhere(fn, orElse:() => null);
|
|
|
|
static any(List list, fn) => list.any(fn);
|
2014-11-24 18:42:53 +01:00
|
|
|
static forEach(list, Function fn) {
|
2014-09-30 14:56:33 -04:00
|
|
|
list.forEach(fn);
|
|
|
|
}
|
2014-10-28 12:22:38 -04:00
|
|
|
static reduce(List list, Function fn, init) {
|
|
|
|
return list.fold(init, fn);
|
|
|
|
}
|
2015-01-21 12:05:52 -08:00
|
|
|
static first(List list) => list.isEmpty ? null : list.first;
|
|
|
|
static last(List list) => list.isEmpty ? null : list.last;
|
2014-10-07 10:42:27 -04:00
|
|
|
static List reversed(List list) => list.reversed.toList();
|
2014-09-26 13:52:12 -07:00
|
|
|
static void push(List l, e) { l.add(e); }
|
2014-11-04 10:19:37 -08:00
|
|
|
static List concat(List a, List b) {a.addAll(b); return a;}
|
2014-10-28 18:56:15 +01:00
|
|
|
static bool isList(l) => l is List;
|
|
|
|
static void insert(List l, int index, value) { l.insert(index, value); }
|
|
|
|
static void removeAt(List l, int index) { l.removeAt(index); }
|
2015-01-02 14:23:59 -08:00
|
|
|
static void removeAll(List list, List items) {
|
|
|
|
for (var i = 0; i < items.length; ++i) {
|
|
|
|
list.remove(items[i]);
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 09:41:26 +01:00
|
|
|
static bool remove(List list, item) => list.remove(item);
|
2014-10-28 18:56:15 +01:00
|
|
|
static void clear(List l) { l.clear(); }
|
2014-12-05 18:30:45 -08:00
|
|
|
static String join(List l, String s) => l.join(s);
|
2015-01-02 14:23:59 -08:00
|
|
|
static bool isEmpty(list) => list.isEmpty;
|
2015-01-22 11:52:36 +01:00
|
|
|
static void fill(List l, value, [int start = 0, int end]) {
|
|
|
|
// JS semantics
|
|
|
|
// see https://github.com/google/traceur-compiler/blob/81880cd3f17bac7de90a4cd0339e9f1a9f61d24c/src/runtime/polyfills/Array.js#L94
|
|
|
|
int len = l.length;
|
|
|
|
start = start < 0 ? max(len + start, 0) : min(start, len);
|
|
|
|
if (end == null) {
|
|
|
|
end = len;
|
|
|
|
} else {
|
|
|
|
end = end < 0 ? max(len + end, 0) : min(end, len);
|
|
|
|
}
|
|
|
|
l.fillRange(start, end, value);
|
|
|
|
}
|
2015-01-25 16:59:06 -08:00
|
|
|
static bool equals(List a, List b){
|
|
|
|
if(a.length != b.length) return false;
|
|
|
|
for (var i = 0; i < a.length; ++i) {
|
|
|
|
if (a[i] != b[i]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-26 13:52:12 -07:00
|
|
|
}
|
|
|
|
|
2014-11-24 18:42:53 +01:00
|
|
|
bool isListLikeIterable(obj) => obj is Iterable;
|
|
|
|
|
|
|
|
void iterateListLike(iter, Function fn) {
|
|
|
|
assert(iter is Iterable);
|
|
|
|
for (var item in iter) {
|
|
|
|
fn (item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-26 13:52:12 -07:00
|
|
|
class SetWrapper {
|
|
|
|
static Set createFromList(List l) { return new Set.from(l); }
|
|
|
|
static bool has(Set s, key) { return s.contains(key); }
|
2014-09-19 16:38:37 -07:00
|
|
|
}
|