2018-08-29 20:12:19 +00:00
|
|
|
import 'dart:core';
|
2018-08-29 20:13:13 +00:00
|
|
|
import 'dart:math';
|
2018-08-29 20:12:19 +00:00
|
|
|
|
|
|
|
import 'package:collection/collection.dart';
|
|
|
|
|
|
|
|
class Grid<E> extends DelegatingList<E> {
|
|
|
|
final List<E> _internal;
|
|
|
|
final width;
|
|
|
|
final height;
|
|
|
|
|
|
|
|
Grid(int width, int height) : this._(List<E>(width * height), width, height);
|
|
|
|
|
|
|
|
Grid.from(Grid<E> l)
|
|
|
|
: this._(List<E>.from(l.getRange(0, l.length)), l.width, l.height);
|
|
|
|
|
|
|
|
Grid.fromList(List<E> l, int width) : this._(l, width, l.length ~/ width);
|
|
|
|
|
|
|
|
Grid._(l, int w, int h)
|
|
|
|
: _internal = l,
|
|
|
|
width = w,
|
|
|
|
height = h,
|
|
|
|
super(l);
|
2018-08-29 20:13:13 +00:00
|
|
|
|
|
|
|
E elementAtPos(int x, int y) {
|
|
|
|
int i = toIndex(x, y);
|
|
|
|
if (i >= length) throw RangeError.index(i, this);
|
|
|
|
_internal[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
void setElement(int x, int y, E value) {
|
|
|
|
int i = toIndex(x, y);
|
|
|
|
if (i >= length) throw RangeError.index(i, this);
|
|
|
|
_internal[i] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
E get(int x, int y) => elementAtPos(x, y);
|
|
|
|
|
|
|
|
void set(int x, int y, E value) => setElement(x, y, value);
|
|
|
|
|
|
|
|
int toIndex(int x, int y) => y * width + x;
|
|
|
|
|
|
|
|
Point<int> toCoords(int index) => Point<int>(index % width, index ~/ width);
|
2018-08-29 20:12:19 +00:00
|
|
|
}
|