github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/compile/internal/abt/avlint32.go (about) 1 // Copyright 2022 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package abt 6 7 const ( 8 LEAF_HEIGHT = 1 9 ZERO_HEIGHT = 0 10 NOT_KEY32 = int32(-0x80000000) 11 ) 12 13 // T is the exported applicative balanced tree data type. 14 // A T can be used as a value; updates to one copy of the value 15 // do not change other copies. 16 type T struct { 17 root *node32 18 size int 19 } 20 21 // IsEmpty returns true iff t is empty. 22 func (t *T) IsEmpty() bool 23 24 // IsSingle returns true iff t is a singleton (leaf). 25 func (t *T) IsSingle() bool 26 27 // VisitInOrder applies f to the key and data pairs in t, 28 // with keys ordered from smallest to largest. 29 func (t *T) VisitInOrder(f func(int32, interface{})) 30 31 // Find returns the data associated with x in the tree, or 32 // nil if x is not in the tree. 33 func (t *T) Find(x int32) interface{} 34 35 // Insert either adds x to the tree if x was not previously 36 // a key in the tree, or updates the data for x in the tree if 37 // x was already a key in the tree. The previous data associated 38 // with x is returned, and is nil if x was not previously a 39 // key in the tree. 40 func (t *T) Insert(x int32, data interface{}) interface{} 41 42 func (t *T) Copy() *T 43 44 func (t *T) Delete(x int32) interface{} 45 46 func (t *T) DeleteMin() (int32, interface{}) 47 48 func (t *T) DeleteMax() (int32, interface{}) 49 50 func (t *T) Size() int 51 52 // Intersection returns the intersection of t and u, where the result 53 // data for any common keys is given by f(t's data, u's data) -- f need 54 // not be symmetric. If f returns nil, then the key and data are not 55 // added to the result. If f itself is nil, then whatever value was 56 // already present in the smaller set is used. 57 func (t *T) Intersection(u *T, f func(x, y interface{}) interface{}) *T 58 59 // Union returns the union of t and u, where the result data for any common keys 60 // is given by f(t's data, u's data) -- f need not be symmetric. If f returns nil, 61 // then the key and data are not added to the result. If f itself is nil, then 62 // whatever value was already present in the larger set is used. 63 func (t *T) Union(u *T, f func(x, y interface{}) interface{}) *T 64 65 // Difference returns the difference of t and u, subject to the result 66 // of f applied to data corresponding to equal keys. If f returns nil 67 // (or if f is nil) then the key+data are excluded, as usual. If f 68 // returns not-nil, then that key+data pair is inserted. instead. 69 func (t *T) Difference(u *T, f func(x, y interface{}) interface{}) *T 70 71 func (t *T) Iterator() Iterator 72 73 func (t *T) Equals(u *T) bool 74 75 func (t *T) String() string 76 77 func (t *T) Equiv(u *T, eqv func(x, y interface{}) bool) bool 78 79 type Iterator struct { 80 it iterator 81 } 82 83 func (it *Iterator) Next() (int32, interface{}) 84 85 func (it *Iterator) Done() bool 86 87 // Min returns the minimum element of t. 88 // If t is empty, then (NOT_KEY32, nil) is returned. 89 func (t *T) Min() (k int32, d interface{}) 90 91 // Max returns the maximum element of t. 92 // If t is empty, then (NOT_KEY32, nil) is returned. 93 func (t *T) Max() (k int32, d interface{}) 94 95 // Glb returns the greatest-lower-bound-exclusive of x and the associated 96 // data. If x has no glb in the tree, then (NOT_KEY32, nil) is returned. 97 func (t *T) Glb(x int32) (k int32, d interface{}) 98 99 // GlbEq returns the greatest-lower-bound-inclusive of x and the associated 100 // data. If x has no glbEQ in the tree, then (NOT_KEY32, nil) is returned. 101 func (t *T) GlbEq(x int32) (k int32, d interface{}) 102 103 // Lub returns the least-upper-bound-exclusive of x and the associated 104 // data. If x has no lub in the tree, then (NOT_KEY32, nil) is returned. 105 func (t *T) Lub(x int32) (k int32, d interface{}) 106 107 // LubEq returns the least-upper-bound-inclusive of x and the associated 108 // data. If x has no lubEq in the tree, then (NOT_KEY32, nil) is returned. 109 func (t *T) LubEq(x int32) (k int32, d interface{})