Add getter and setter methods for wrapper

!!Need testing
This commit is contained in:
Marty Oehme 2018-08-29 22:13:13 +02:00
parent 27ef72014e
commit b0e67d9f85

View file

@ -1,4 +1,5 @@
import 'dart:core'; import 'dart:core';
import 'dart:math';
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
@ -19,4 +20,24 @@ class Grid<E> extends DelegatingList<E> {
width = w, width = w,
height = h, height = h,
super(l); super(l);
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);
} }