Skip to content

Commit 2c4510d

Browse files
committed
Made table private in SparseTable
1 parent b484cca commit 2c4510d

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Algorithms
2+
==========
3+
4+
.. autoclass:: pydatastructs.RangeQueryStatic

docs/source/pydatastructs/miscellaneous_data_structures/miscellaneous_data_structures.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ Miscellaneous Data Structures
77
stack.rst
88
queue.rst
99
binomial_trees.rst
10-
disjoint_set.rst
10+
disjoint_set.rst
11+
sparse_table.rst
12+
algorithms.rst
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SparseTable
2+
===========
3+
4+
.. autoclass:: pydatastructs.SparseTable

pydatastructs/miscellaneous_data_structures/sparse_table.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class SparseTable(object):
4848
.. [1] https://cp-algorithms.com/data_structures/sparse-table.html
4949
"""
5050

51-
__slots__ = ['table', 'func']
51+
__slots__ = ['_table', 'func']
5252

5353
def __new__(cls, array, func):
5454

@@ -58,16 +58,16 @@ def __new__(cls, array, func):
5858
obj = object.__new__(cls)
5959
size = len(array)
6060
log_size = int(math.log2(size)) + 1
61-
obj.table = [OneDimensionalArray(int, log_size) for _ in range(size)]
61+
obj._table = [OneDimensionalArray(int, log_size) for _ in range(size)]
6262
obj.func = func
6363

6464
for i in range(size):
65-
obj.table[i][0] = func((array[i],))
65+
obj._table[i][0] = func((array[i],))
6666

6767
for j in range(1, log_size + 1):
6868
for i in range(size - (1 << j) + 1):
69-
obj.table[i][j] = func((obj.table[i][j - 1],
70-
obj.table[i + (1 << (j - 1))][j - 1]))
69+
obj._table[i][j] = func((obj._table[i][j - 1],
70+
obj._table[i + (1 << (j - 1))][j - 1]))
7171

7272
return obj
7373

@@ -76,15 +76,29 @@ def methods(cls):
7676
return ['query', '__str__']
7777

7878
def query(self, start, end):
79+
"""
80+
Method to perform a query on sparse table in [start, end)
81+
range.
82+
83+
Parameters
84+
==========
85+
86+
start: int
87+
The starting index of the range.
88+
end: int
89+
The index just before which the range ends.
90+
This means that this index will be excluded
91+
from the range for generating results.
92+
"""
7993
end -= 1
8094
j = int(math.log2(end - start + 1)) + 1
8195
answer = None
8296
while j >= 0:
8397
if start + (1 << j) - 1 <= end:
84-
answer = self.func((answer, self.table[start][j]))
98+
answer = self.func((answer, self._table[start][j]))
8599
start += 1 << j
86100
j -= 1
87101
return answer
88102

89103
def __str__(self):
90-
return str([str(array) for array in self.table])
104+
return str([str(array) for array in self._table])

0 commit comments

Comments
 (0)