File size: 2,697 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import type { TreeNodeList } from "./TreeNodeList/TreeNodeList";

let version = 0;

const DO_CACHE = true;

// Create WeakMaps to store caches. The key will be the TreeNodeList instance.
const flattenedCache = new WeakMap<
    TreeNodeList,
    { list: TreeNodeList; version: number }
>();
const terminalsCache = new WeakMap<
    TreeNodeList,
    { list: TreeNodeList; version: number }
>();

/**
 * Increments the global tree version. Call this whenever the tree is mutated.
 * Note that because the caches are stored in WeakMaps, the entries that are
 * invalidated by this will be garbage collected automatically.
 */
export function incrementTreeVersion(): void {
    version++;
}

/**
 * Gets the current global tree version.
 * 
 * @returns {number} The current version.
 */
export function getTreeVersion(): number {
    return version;
}

/**
 * Gets the flattened list from the cache if the version is current.
 * 
 * @param {TreeNodeList} list The TreeNodeList instance to check.
 * @returns {TreeNodeList | null} The cached list or null if stale/missing.
 */
export function getFlattenedFromCache(
 list: TreeNodeList
): TreeNodeList | null {
    if (!DO_CACHE) {
        return null; // Caching is disabled
    }

    const cacheEntry = flattenedCache.get(list);
    if (cacheEntry && cacheEntry.version === version) {
        return cacheEntry.list;
    }
    return null;
}

/**
 * Sets the flattened list in the cache for a given TreeNodeList instance.
 * 
 * @param {TreeNodeList} list The TreeNodeList instance to cache for.
 * @param {TreeNodeList} flattened The flattened list to store.
 */
export function setFlattenedInCache(
    list: TreeNodeList,
    flattened: TreeNodeList
): void {
    flattenedCache.set(list, { list: flattened, version });
}

/**
 * Gets the terminals list from the cache if the version is current.
 * 
 * @param {TreeNodeList} list The TreeNodeList instance to check.
 * @returns {TreeNodeList | null} The cached list or null if stale/missing.
 */
export function getTerminalsFromCache(
 list: TreeNodeList
): TreeNodeList | null {
    if (!DO_CACHE) {
        return null; // Caching is disabled
    }

    const cacheEntry = terminalsCache.get(list);
    if (cacheEntry && cacheEntry.version === version) {
        return cacheEntry.list;
    }
    return null;
}

/**
 * Sets the terminals list in the cache for a given TreeNodeList instance.
 * 
 * @param {TreeNodeList} list The TreeNodeList instance to cache for.
 * @param {TreeNodeList} terminals The terminals list to store.
 */
export function setTerminalsInCache(
    list: TreeNodeList,
    terminals: TreeNodeList
): void {
    terminalsCache.set(list, { list: terminals, version });
}