-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastronode.ts
executable file
·101 lines (94 loc) · 2.1 KB
/
astronode.ts
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
101
export class AstroNode {
uniqueid: string;
level: number;
vector: Float32Array | number[];
neighbors: string[][]; // neighbors[level][M]
deleted?: boolean = false;
constructor(
uniqueid: string,
vector: Float32Array | number[],
level: number,
M: number,
neighbors?: string[][],
deleted: boolean = false,
) {
this.uniqueid = uniqueid;
this.vector = vector;
this.level = level;
// if neighbors given, initialize with that
if (neighbors) {
this.neighbors = neighbors;
} else {
// otherwise create an empty array
// this.neighbors = Array.from({ length: level + 1 }, () =>
// new Array(M).fill(''),
// );
this.neighbors = [];
}
this.deleted = deleted;
}
toJSON(): Record<string, any> {
return {
uniqueid: this.uniqueid,
level: this.level,
vector: Array.from(this.vector),
neighbors: this.neighbors.map((level) => Array.from(level)),
deleted: this.deleted,
};
}
/**
* Parses a JSON object into an AstroNode
* @param obj
* @returns
*/
static parse(obj: Record<string, any>): AstroNode {
return new AstroNode(
obj.uniqueid,
obj.vector,
obj.level,
obj.M,
obj.neighbors,
obj.deleted,
);
}
}
/**
* Extends AstroNode with a score relative to the query
*/
export class AstroNodeWithScore extends AstroNode {
score: number;
constructor(
uniqueid: string,
vector: Float32Array | number[],
level: number,
M: number,
neighbors?: string[][],
deleted: boolean = false,
score: number = 0,
) {
super(uniqueid, vector, level, M, neighbors, deleted);
this.score = score;
}
toJSON(): Record<string, any> {
return {
...super.toJSON(),
score: this.score,
};
}
/**
* Parses a JSON object into an AstroNode
* @param obj
* @returns
*/
static parse(obj: Record<string, any>): AstroNodeWithScore {
return new AstroNodeWithScore(
obj.uniqueid,
obj.vector,
obj.level,
obj.M,
obj.neighbors,
obj.deleted,
obj.score,
);
}
}