File tree 1 file changed +57
-0
lines changed
1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } length
3
+ */
4
+ const SnapshotArray = function ( length ) {
5
+ this . arr = new Array ( length ) . fill ( 0 ) ;
6
+ this . snaps = new Array ( length ) ;
7
+ this . count = 0 ;
8
+ } ;
9
+
10
+ /**
11
+ * @param {number } index
12
+ * @param {number } val
13
+ * @return {void }
14
+ */
15
+ SnapshotArray . prototype . set = function ( index , val ) {
16
+ if ( this . snaps [ index ] == undefined ) {
17
+ this . snaps [ index ] = { } ;
18
+ }
19
+
20
+ this . snaps [ index ] [ this . count ] = val ;
21
+ } ;
22
+
23
+ /**
24
+ * @return {number }
25
+ */
26
+ SnapshotArray . prototype . snap = function ( ) {
27
+ return this . count ++ ;
28
+ } ;
29
+
30
+ /**
31
+ * @param {number } index
32
+ * @param {number } snap_id
33
+ * @return {number }
34
+ */
35
+ SnapshotArray . prototype . get = function ( index , snap_id ) {
36
+ if ( this . snaps [ index ] == undefined ) return 0 ;
37
+
38
+ let res = 0 ;
39
+ while ( snap_id >= 0 ) {
40
+ if ( this . snaps [ index ] [ snap_id ] == undefined ) {
41
+ snap_id -- ;
42
+ } else {
43
+ res = this . snaps [ index ] [ snap_id ] ;
44
+ snap_id = - 1 ;
45
+ }
46
+ }
47
+
48
+ return res ;
49
+ } ;
50
+
51
+ /**
52
+ * Your SnapshotArray object will be instantiated and called as such:
53
+ * var obj = new SnapshotArray(length)
54
+ * obj.set(index,val)
55
+ * var param_2 = obj.snap()
56
+ * var param_3 = obj.get(index,snap_id)
57
+ */
You can’t perform that action at this time.
0 commit comments