Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added range query. #158

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed query io.
AlfredChester committed Dec 15, 2024
commit 6441f51ca6a12a79a6e0110860996f0dfa8aa209
50 changes: 38 additions & 12 deletions cyaron/query.py
Original file line number Diff line number Diff line change
@@ -25,32 +25,58 @@ class RangeQueryRandomMode(IntEnum):

class RangeQuery:
"""A class for generating random queries."""
result: List[Tuple[List[int], List[int]]]

def __init__(self):
self.result = []

def __len__(self):
return len(self.result)

def __getitem__(self, item):
return self.result[item]

def __str__(self):
"""__str__(self) -> str
Return a string to output the queries.
The string contains all the queries with l and r in a row, splits with "\\n".
"""
return self.to_str()

def to_str(self):
res = ''
for l, r, in self.result:
l_to_str = [str(x) for x in l]
r_to_str = [str(x) for x in r]
res += ' '.join(l_to_str) + ' ' + ' '.join(r_to_str) + '\n'
return res

@staticmethod
def random(
num: int = 1,
position_range: Optional[List[Union[int, Tuple[int, int]]]] = None,
mode: RangeQueryRandomMode = RangeQueryRandomMode.allow_equal,
) -> List[Tuple[List[int], List[int]]]:
"""
Generate `num` random queries with dimension limit.
Args:
num: the number of queries
position_range: a list of limits for each dimension
single number x represents range [1, x]
list [x, y] or tuple (x, y) represents range [x, y]
mode: the mode queries generate, see Enum Class RangeQueryRandomMode
):
"""random(num, position_range, mode) -> RangeQuery
Generate `num` random queries with dimension limit.
Args:
num: the number of queries
position_range: a list of limits for each dimension
single number x represents range [1, x]
list [x, y] or tuple (x, y) represents range [x, y]
mode: the mode queries generate, see Enum Class RangeQueryRandomMode
"""
if position_range is None:
position_range = [10]

ret = RangeQuery()

if not list_like(position_range):
raise TypeError("the 2nd param must be a list or tuple")

result: List[Tuple[List[int], List[int]]] = []
for _ in range(num):
result.append(RangeQuery.get_one_query(position_range, mode))
return result
ret.result.append(RangeQuery.get_one_query(position_range, mode))
return ret

@staticmethod
def get_one_query(