github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/segment/test/set_functions.go (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain 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, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package segment is a test package. 16 package segment 17 18 type setFunctions struct{} 19 20 // MinKey returns the minimum key for the set. 21 func (s setFunctions) MinKey() int { 22 return -s.MaxKey() - 1 23 } 24 25 // MaxKey returns the maximum key for the set. 26 func (setFunctions) MaxKey() int { 27 return int(^uint(0) >> 1) 28 } 29 30 func (setFunctions) ClearValue(*int) {} 31 32 func (setFunctions) Merge(_ Range, val1 int, _ Range, _ int) (int, bool) { 33 return val1, true 34 } 35 36 func (setFunctions) Split(_ Range, val int, _ int) (int, int) { 37 return val, val 38 } 39 40 type gapSetFunctions struct { 41 setFunctions 42 } 43 44 // MinKey is adjusted to make sure no add overflow would happen in test cases. 45 // e.g. A gap with range {MinInt32, 2} would cause overflow in Range().Length(). 46 // 47 // Normally Keys should be unsigned to avoid these issues. 48 func (s gapSetFunctions) MinKey() int { 49 return s.setFunctions.MinKey() / 2 50 } 51 52 // MaxKey returns the maximum key for the set. 53 func (s gapSetFunctions) MaxKey() int { 54 return s.setFunctions.MaxKey() / 2 55 }