func (p *page) Get(x, y int16) (tile Tile) {
	i := (y%3)*3 + (x % 3)
	p.Lock()
	tile = p.tiles[i]
	p.Unlock()
	return
}
Get gets a tile at a specific coordinate.
package tile
import (
	"reflect"
	"runtime"
	"sync/atomic"
	"unsafe"
)
type Iterator = func(Point, Tile)
type pageFn = func(*page)
type indexFn = func(x, y int16) int
type pointFn = func(i int) Point
type Grid struct {
	pages      []page   
	pageWidth  int16    
	pageHeight int16    
	observers  pubsub   
	Size       Point    
	indexOf    indexFn  
}
func NewGrid(width, height int16) *Grid {
	width, height = width/3, height/3
	max := int32(width) * int32(height)
	pages := make([]page, max)
	m := &Grid{
		pages:      pages,
		pageWidth:  width,
		pageHeight: height,
		observers:  pubsub{},
		Size:       At(width*3, height*3),
	}
	var pointAt func(i int) Point = func(i int) Point {
		return At(int16(i%int(width)), int16(i/int(width)))
	}
	m.indexOf = m.pointToFlat
	if width == height && (width&(width-1)) == 0 {
		pointAt = deinterleavePoint
		m.indexOf = m.pointToZ
	}
	for i := 0; i < int(max); i++ {
		pages[i].point = pointAt(i).MultiplyScalar(3)
	}
	return m
}
func (m *Grid) pointToFlat(x, y int16) int {
	return int(x) + int(m.pageWidth)*int(y)
}
func (m *Grid) pointToZ(x, y int16) int {
	return int(At(x, y).Interleave())
}
func (m *Grid) Each(fn Iterator) {
	until := int(m.pageHeight) * int(m.pageWidth)
	for i := 0; i < until; i++ {
		m.pages[i].Each(fn)
	}
}
func (m *Grid) Within(nw, se Point, fn Iterator) {
	m.pagesWithin(nw, se, func(page *page) {
		page.Each(func(p Point, tile Tile) {
			if p.Within(nw, se) {
				fn(p, tile)
			}
		})
	})
}
func (m *Grid) pagesWithin(nw, se Point, fn pageFn) {
	if !se.WithinSize(m.Size) {
		se = At(m.Size.X-1, m.Size.Y-1)
	}
	for x := nw.X / 3; x <= se.X/3; x++ {
		for y := nw.Y / 3; y <= se.Y/3; y++ {
			fn(&m.pages[m.indexOf(x, y)])
		}
	}
}
func (m *Grid) At(x, y int16) (Tile, bool) {
	if x >= 0 && y >= 0 && x < m.Size.X && y < m.Size.Y {
		return m.pages[m.indexOf(x/3, y/3)].Get(x, y), true
	}
	return Tile{}, false
}
func (m *Grid) WriteAt(x, y int16, tile Tile) {
	if x >= 0 && y >= 0 && x < m.Size.X && y < m.Size.Y {
		if m.pages[m.indexOf(x/3, y/3)].SetTile(x, y, tile) {
			m.observers.Notify(At(x/3*3, y/3*3), At(x, y), tile)
		}
	}
}
func (m *Grid) MergeAt(x, y int16, tile, mask Tile) {
	if x >= 0 && y >= 0 && x < m.Size.X && y < m.Size.Y {
		if v, ok := m.pages[m.indexOf(x/3, y/3)].SetBits(x, y, tile, mask); ok {
			m.observers.Notify(At(x/3*3, y/3*3), At(x, y), v)
		}
	}
}
func (m *Grid) Neighbors(x, y int16, fn Iterator) {
	nX, nY := x/3, (y-1)/3  
	eX, eY := (x+1)/3, y/3  
	sX, sY := x/3, (y+1)/3  
	wX, wY := (x-1)/3, y/3  
	if y > 0 {
		fn(At(x, y-1), m.pages[m.indexOf(nX, nY)].Get(x, y-1))
	}
	if eX < m.pageWidth {
		fn(At(x+1, y), m.pages[m.indexOf(eX, eY)].Get(x+1, y))
	}
	if sY < m.pageHeight {
		fn(At(x, y+1), m.pages[m.indexOf(sX, sY)].Get(x, y+1))
	}
	if x > 0 {
		fn(At(x-1, y), m.pages[m.indexOf(wX, wY)].Get(x-1, y))
	}
}
func (m *Grid) View(rect Rect, fn Iterator) *View {
	view := &View{
		Grid:  m,
		Inbox: make(chan Update, 8),
		rect:  NewRect(-1, -1, -1, -1),
	}
	view.Resize(rect, fn)
	return view
}
type Tile [6]byte
type page struct {
	lock  int32    
	flags uint16   
	point Point    
	tiles [9]Tile  
}
func (p *page) Bounds() Rect {
	return Rect{p.point, At(p.point.X+3, p.point.Y+3)}
}
func (p *page) SetTile(x, y int16, tile Tile) bool {
	i := (y%3)*3 + (x % 3)
	p.Lock()
	p.tiles[i] = tile
	notify := p.flags&1 != 0
	p.Unlock()
	return notify
}
func (p *page) SetBits(x, y int16, tile, mask Tile) (Tile, bool) {
	t := uint64(tile[0]) | uint64(tile[1])<<8 | uint64(tile[2])<<16 |
		uint64(tile[3])<<24 | uint64(tile[4])<<32 | uint64(tile[5])<<40
	m := uint64(mask[0]) | uint64(mask[1])<<8 | uint64(mask[2])<<16 |
		uint64(mask[3])<<24 | uint64(mask[4])<<32 | uint64(mask[5])<<40
	i := (y%3)*3 + (x % 3)
	p.Lock()
	b := &p.tiles[i]
	v := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 |
		uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40
	v = (v &^ m) | (t & m)
	b[0] = byte(v)
	b[1] = byte(v >> 8)
	b[2] = byte(v >> 16)
	b[3] = byte(v >> 24)
	b[4] = byte(v >> 32)
	b[5] = byte(v >> 40)
	merged, notify := *b, p.flags&1 != 0
	p.Unlock()
	return merged, notify
}