1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 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 16class RangeEdge { 17 readonly value: number; 18 readonly inclusive: boolean; 19 constructor(value: number, inclusive: boolean) { 20 this.value = value; 21 this.inclusive = inclusive; 22 } 23} 24 25class RatioRange { 26 readonly start: RangeEdge; 27 readonly end: RangeEdge; 28 29 constructor(start: RangeEdge, end: RangeEdge) { 30 this.start = start; 31 this.end = end; 32 if (this.start.value > this.end.value) { 33 throw new Error(`RatioRange: ${this.start.value} > ${this.end.value}`); 34 } 35 } 36 37 static newEmpty(): RatioRange { 38 return new RatioRange(new RangeEdge(0, false), new RangeEdge(0, false)); 39 } 40 41 contains(point: number): boolean { 42 if (point === this.start.value) { 43 return this.start.inclusive; 44 } 45 if (point === this.end.value) { 46 return this.end.inclusive; 47 } 48 return this.start.value < point && point < this.end.value; 49 } 50 51 toString(): string { 52 return `${this.start.inclusive ? '[' : '('}${this.start.value}, ${this.end.value}${this.end.inclusive ? ']' : ')'}`; 53 } 54} 55