Skip to content
Open
Changes from all commits
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
9 changes: 9 additions & 0 deletions range_queries/sparse_table_range_queries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,19 @@ std::vector<std::vector<T> > buildTable(const std::vector<T>& A,
template <typename T>
int getMinimum(int beg, int end, const std::vector<T>& logs,
const std::vector<std::vector<T> >& table) {
if(beg<0||end<<beg||end>=(int)table[0].size()){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also why are we left shifting end by beg?
shouldnt it be

Suggested change
if(beg<0||end<<beg||end>=(int)table[0].size()){
if(beg < 0 || end < beg || end >= (int) table[0].size()){

cout<<"Error:querry range ["<<beg<<","<<end<<"] is invalid."<<endl;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cout<<"Error:querry range ["<<beg<<","<<end<<"] is invalid."<<endl;
throw std::invalid_argument("Error:query range ["<<beg<<","<<end<<"] is invalid.");

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can also force beg > 0 by using unsigned integer instead preferably uint32_t

return -1;
}
int p = logs[end - beg + 1];
int pLen = 1 << p;
if (p >= (int)table.size() || beg >= (int)table[p].size() || (end - pLen + 1) >= (int)table[p].size()) {
std::cerr << "Error: index out of bounds when accessing sparse table." << std::endl;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw an error instead

return -1;
}
return std::min(table[p][beg], table[p][end - pLen + 1]);
}

} // namespace sparse_table
} // namespace range_queries

Expand Down