├── .gitignore ├── README.md ├── quadtree ├── quadtree.go ├── immutable.go ├── quadtree_test.go └── immutable_test.go └── UNLICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Datastructures for weird things :hocho: 2 | -------------------------------------------------------------------------------- /quadtree/quadtree.go: -------------------------------------------------------------------------------- 1 | package gg 2 | 3 | type QuadtreeType int 4 | 5 | const ( 6 | W QuadtreeType = 0 7 | B QuadtreeType = 1 8 | G QuadtreeType = 2 9 | ) 10 | 11 | /* 12 | Quadtree interface 13 | */ 14 | 15 | type Quadtree struct { 16 | Color QuadtreeType 17 | NW *Quadtree 18 | NE *Quadtree 19 | SE *Quadtree 20 | SW *Quadtree 21 | } 22 | 23 | func NewQuadtreeBlack() *Quadtree { 24 | return &Quadtree{B, nil, nil, nil, nil} 25 | } 26 | 27 | func NewQuadtreeWhite() *Quadtree { 28 | return &Quadtree{W, nil, nil, nil, nil} 29 | } 30 | 31 | func NewQuadtreeWithColor(color QuadtreeType) *Quadtree { 32 | return &Quadtree{color, nil, nil, nil, nil} 33 | } 34 | 35 | func NewQuadtreeLeaf(nw, ne, se, sw QuadtreeType) *Quadtree { 36 | return &Quadtree{ 37 | G, 38 | NewQuadtreeWithColor(nw), 39 | NewQuadtreeWithColor(ne), 40 | NewQuadtreeWithColor(se), 41 | NewQuadtreeWithColor(sw), 42 | } 43 | } 44 | 45 | func NewQuadtree(nw, ne, se, sw *Quadtree) *Quadtree { 46 | return &Quadtree{ 47 | G, 48 | nw, 49 | ne, 50 | se, 51 | sw, 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /quadtree/immutable.go: -------------------------------------------------------------------------------- 1 | package gg 2 | 3 | type Qtype int 4 | 5 | const ( 6 | WHITE Qtype = 0 7 | BLACK = 1 8 | GRAY = 2 9 | ) 10 | 11 | type Qregion int 12 | 13 | const ( 14 | NW Qregion = 0 15 | NE Qregion = 1 16 | SE Qregion = 2 17 | SW Qregion = 3 18 | ) 19 | 20 | type Qtree struct { 21 | /* 22 | WW -> W 23 | BW -> B 24 | BB -> B 25 | GB -> *RECURSE* 26 | GW -> *RECURSE* 27 | */ 28 | Color Qtype 29 | 30 | /* 31 | If gray, regions look l ike: 32 | +----+----+ 33 | | NW | NE | 34 | +----+----+ 35 | | SW | SE + 36 | +----+----+ 37 | */ 38 | Regions []Qtree 39 | } 40 | 41 | func QtreeBlack() Qtree { 42 | return Qtree{BLACK, nil} 43 | } 44 | 45 | func QtreeWhite() Qtree { 46 | return Qtree{WHITE, nil} 47 | } 48 | 49 | func NewQtree(nw, ne, se, sw Qtype) Qtree { 50 | if nw == ne && ne == se && se == sw && sw == nw { 51 | return Qtree{nw, nil} 52 | } 53 | 54 | return Qtree{ 55 | GRAY, 56 | []Qtree{ 57 | Qtree{nw, nil}, 58 | Qtree{ne, nil}, 59 | Qtree{se, nil}, 60 | Qtree{sw, nil}, 61 | }, 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /quadtree/quadtree_test.go: -------------------------------------------------------------------------------- 1 | package gg 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func _(t *testing.T) { 9 | fmt.Println("x_x") 10 | } 11 | 12 | func TestBasic(t *testing.T) { 13 | a := Quadtree{B, nil, nil, nil, nil} 14 | b := Quadtree{W, nil, nil, nil, nil} 15 | 16 | if a.Color != B || b.Color != W { 17 | t.Fail() 18 | } 19 | 20 | c := NewQuadtreeLeaf(B, B, W, W) 21 | 22 | if c.NW.Color != B || c.NE.Color != B || c.SE.Color != W || c.SW.Color != W { 23 | t.Fail() 24 | } 25 | 26 | d := NewQuadtreeLeaf(W, W, B, W) 27 | 28 | if d.NW.Color != W || d.NE.Color != W || d.SE.Color != B || d.SW.Color != W { 29 | t.Fail() 30 | } 31 | 32 | e := NewQuadtree( 33 | NewQuadtreeLeaf(B, B, B, W), 34 | NewQuadtreeLeaf(W, W, B, W), 35 | NewQuadtreeWhite(), 36 | NewQuadtreeBlack(), 37 | ) 38 | 39 | e_nw := e.NW 40 | e_ne := e.NE 41 | 42 | if e.Color != G || e.NW.Color != G || e.NE.Color != G || e.SE.Color != W || e.SW.Color != B { 43 | t.Fail() 44 | } 45 | 46 | if e_nw.NW.Color != B || e_nw.NE.Color != B || e_nw.SE.Color != B || e_nw.SW.Color != W { 47 | t.Fail() 48 | } 49 | 50 | if e_ne.NW.Color != W || e_ne.NE.Color != W || e_ne.SE.Color != B || e_ne.SW.Color != W { 51 | t.Fail() 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /UNLICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /quadtree/immutable_test.go: -------------------------------------------------------------------------------- 1 | package gg 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func _(t *testing.T) { 9 | fmt.Println("x_x") 10 | } 11 | 12 | func TestSimpleQuadtree(t *testing.T) { 13 | a := Qtree{BLACK, nil} 14 | b := Qtree{WHITE, nil} 15 | c := Qtree{GRAY, nil} 16 | 17 | if a.Color != BLACK || b.Color != WHITE || c.Color != GRAY { 18 | t.Fail() 19 | } 20 | 21 | d := Qtree{ 22 | GRAY, 23 | []Qtree{ 24 | Qtree{BLACK, nil}, 25 | Qtree{WHITE, nil}, 26 | Qtree{BLACK, nil}, 27 | Qtree{WHITE, nil}, 28 | }, 29 | } 30 | 31 | switch { 32 | case d.Regions[NW].Color != BLACK: 33 | t.Fail() 34 | case d.Regions[NE].Color != WHITE: 35 | t.Fail() 36 | case d.Regions[SE].Color != BLACK: 37 | t.Fail() 38 | case d.Regions[SW].Color != WHITE: 39 | t.Fail() 40 | } 41 | } 42 | 43 | func TestQtreeHelpers(t *testing.T) { 44 | a := QtreeBlack() 45 | b := QtreeWhite() 46 | 47 | if a.Color != BLACK || b.Color != WHITE { 48 | t.Fail() 49 | } 50 | 51 | c := NewQtree(BLACK, WHITE, BLACK, WHITE) 52 | 53 | if c.Regions[NW].Color != BLACK || c.Regions[NE].Color != WHITE || c.Regions[SE].Color != BLACK || c.Regions[SW].Color != WHITE { 54 | fmt.Println(c.Regions) 55 | t.Fail() 56 | } 57 | 58 | d := NewQtree(BLACK, BLACK, BLACK, BLACK) 59 | 60 | if d.Color != BLACK { 61 | t.Fail() 62 | } 63 | 64 | e := NewQtree(WHITE, WHITE, WHITE, WHITE) 65 | 66 | if e.Color != WHITE { 67 | t.Fail() 68 | } 69 | } 70 | --------------------------------------------------------------------------------