fqtree
Version:
a flexible quadtree for JavaScript/TypeScript
52 lines (39 loc) • 1.02 kB
text/typescript
import { Option, is_none } from 'onsreo';
export class DataCache<T> {
cache: Map<number, T>;
constructor() {
this.cache = new Map();
}
destroy() {
this.cache.clear();
}
add(data: T, id: number): void {
this.cache.set(id, data);
}
all(): IterableIterator<T> {
return this.cache.values();
}
get(id: Option<number>): Option<T> {
return id ? this.cache.get(id) : undefined;
}
has(id: Option<number>): boolean {
return id ? this.cache.has(id) : false;
}
remove(id: Option<number>): void {
id && this.cache.delete(id);
}
}
let __qt_data_cache: Option<DataCache<any>> = null;
export function get_data_cache<T>(): DataCache<T> {
if (is_none(__qt_data_cache)) {
__qt_data_cache = new DataCache<T>();
}
return __qt_data_cache;
}
let __qt_quadrant_cache: Option<DataCache<any>> = null;
export function get_qt_cache<T>(): DataCache<T> {
if (is_none(__qt_quadrant_cache)) {
__qt_quadrant_cache = new DataCache<T>();
}
return __qt_quadrant_cache;
}