github.com/XiaoMi/Gaea@v1.2.5/proxy/plan/util.go (about) 1 // Copyright 2016 The kingshard Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 // Copyright 2019 The Gaea Authors. All Rights Reserved. 16 // 17 // Licensed under the Apache License, Version 2.0 (the "License"); 18 // you may not use this file except in compliance with the License. 19 // You may obtain a copy of the License at 20 // 21 // http://www.apache.org/licenses/LICENSE-2.0 22 // 23 // Unless required by applicable law or agreed to in writing, software 24 // distributed under the License is distributed on an "AS IS" BASIS, 25 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 26 // See the License for the specific language governing permissions and 27 // limitations under the License. 28 29 package plan 30 31 /*2,5 ==> [2,3,4]*/ 32 func makeList(start, end int) []int { 33 if start >= end { 34 return []int{} 35 } 36 list := make([]int, end-start) 37 for i := start; i < end; i++ { 38 list[i-start] = i 39 } 40 return list 41 } 42 43 //if value is 2016, and indexs is [2015,2016,2017] 44 //the result is [2015,2016] 45 // the indexs must be sorted 46 func makeLeList(value int, indexs []int) []int { 47 for k, v := range indexs { 48 if v == value { 49 return indexs[:k+1] 50 } 51 } 52 return nil 53 } 54 55 //if value is 2016, and indexs is [2015,2016,2017,2018] 56 //the result is [2016,2017,2018] 57 // the indexs must be sorted 58 func makeGeList(value int, indexs []int) []int { 59 for k, v := range indexs { 60 if v == value { 61 return indexs[k:] 62 } 63 } 64 return nil 65 } 66 67 //if value is 2016, and indexs is [2015,2016,2017,2018] 68 //the result is [2015] 69 // the indexs must be sorted 70 func makeLtList(value int, indexs []int) []int { 71 for k, v := range indexs { 72 if v == value { 73 return indexs[:k] 74 } 75 } 76 return nil 77 } 78 79 //if value is 2016, and indexs is [2015,2016,2017,2018] 80 //the result is [2017,2018] 81 // the indexs must be sorted 82 func makeGtList(value int, indexs []int) []int { 83 for k, v := range indexs { 84 if v == value { 85 return indexs[k+1:] 86 } 87 } 88 return nil 89 } 90 91 //if start is 2016, end is 2017. indexs is [2015,2016,2017,2018] 92 //the result is [2016,2017] 93 // the indexs must be sorted 94 func makeBetweenList(start, end int, indexs []int) []int { 95 var startIndex, endIndex int 96 var SetStart bool 97 if end < start { 98 start, end = end, start 99 } 100 for k, v := range indexs { 101 if v == start { 102 startIndex = k 103 SetStart = true 104 } 105 if v == end { 106 endIndex = k 107 if SetStart { 108 return indexs[startIndex : endIndex+1] 109 } 110 } 111 } 112 return nil 113 } 114 115 // l1 & l2 116 func interList(l1 []int, l2 []int) []int { 117 if len(l1) == 0 || len(l2) == 0 { 118 return []int{} 119 } 120 121 l3 := make([]int, 0, len(l1)+len(l2)) 122 var i = 0 123 var j = 0 124 for i < len(l1) && j < len(l2) { 125 if l1[i] == l2[j] { 126 l3 = append(l3, l1[i]) 127 i++ 128 j++ 129 } else if l1[i] < l2[j] { 130 i++ 131 } else { 132 j++ 133 } 134 } 135 136 return l3 137 } 138 139 // l1 | l2 140 func unionList(l1 []int, l2 []int) []int { 141 if len(l1) == 0 { 142 return l2 143 } else if len(l2) == 0 { 144 return l1 145 } 146 147 l3 := make([]int, 0, len(l1)+len(l2)) 148 149 var i = 0 150 var j = 0 151 for i < len(l1) && j < len(l2) { 152 if l1[i] < l2[j] { 153 l3 = append(l3, l1[i]) 154 i++ 155 } else if l1[i] > l2[j] { 156 l3 = append(l3, l2[j]) 157 j++ 158 } else { 159 l3 = append(l3, l1[i]) 160 i++ 161 j++ 162 } 163 } 164 165 if i != len(l1) { 166 l3 = append(l3, l1[i:]...) 167 } else if j != len(l2) { 168 l3 = append(l3, l2[j:]...) 169 } 170 171 return l3 172 } 173 174 // l1 - l2 175 func differentList(l1 []int, l2 []int) []int { 176 if len(l1) == 0 { 177 return []int{} 178 } else if len(l2) == 0 { 179 return l1 180 } 181 182 l3 := make([]int, 0, len(l1)) 183 184 var i = 0 185 var j = 0 186 for i < len(l1) && j < len(l2) { 187 if l1[i] < l2[j] { 188 l3 = append(l3, l1[i]) 189 i++ 190 } else if l1[i] > l2[j] { 191 j++ 192 } else { 193 i++ 194 j++ 195 } 196 } 197 198 if i != len(l1) { 199 l3 = append(l3, l1[i:]...) 200 } 201 202 return l3 203 } 204 205 func cleanList(l []int) []int { 206 s := make(map[int]struct{}) 207 listLen := len(l) 208 l2 := make([]int, 0, listLen) 209 210 for i := 0; i < listLen; i++ { 211 k := l[i] 212 s[k] = struct{}{} 213 } 214 for k := range s { 215 l2 = append(l2, k) 216 } 217 return l2 218 } 219 220 // list l need to be sorted 221 func distinctList(l []int) []int { 222 if len(l) < 2 { 223 return l 224 } 225 var ret []int 226 current := l[0] 227 ret = append(ret, current) 228 for i := 1; i < len(l); i++ { 229 if l[i] != current { 230 current = l[i] 231 ret = append(ret, current) 232 } 233 } 234 return ret 235 }