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 FetchedRegistry { 17 private readonly fetchedIndicies: Set<number> = new Set(); // {I where I is in cache} ∩ `prefetchRange` 18 private rangeToPrefetch: IndexRange = new IndexRange(0, 0); 19 20 addFetched(index: number): void { 21 this.fetchedIndicies.add(index); 22 } 23 24 getFetchedInRange(range: IndexRange): void { 25 let fetched = 0; 26 range.forEachIndex((index) => { 27 fetched += this.fetchedIndicies.has(index) ? 1 : 0; 28 }); 29 return fetched; 30 } 31 32 updateRangeToPrefetch(prefetchRange: IndexRange): void { 33 this.rangeToPrefetch.subtract(prefetchRange).forEachIndex((index) => { 34 this.fetchedIndicies.delete(index); 35 }); 36 this.rangeToPrefetch = prefetchRange; 37 } 38} 39