-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
137fcb9
commit 8b95bd9
Showing
7 changed files
with
173 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// Code here is taken from https://www.geeksforgeeks.org/interval-tree/ and edited | ||
|
||
#pragma once | ||
|
||
namespace til | ||
{ | ||
class IntervalTree | ||
{ | ||
public: | ||
// Structure to represent an interval | ||
struct Interval | ||
{ | ||
COORD low, high; | ||
}; | ||
|
||
// Structure to represent a node in Interval Search Tree | ||
struct ITNode | ||
{ | ||
Interval* i; | ||
COORD max; | ||
ITNode *left, *right; | ||
size_t patternId; | ||
}; | ||
|
||
// A utility function to create a new Interval Search Tree Node | ||
ITNode* newNode(Interval i, size_t id) | ||
{ | ||
ITNode* temp = new ITNode; | ||
temp->i = new Interval(i); | ||
temp->max = i.high; | ||
temp->left = temp->right = NULL; | ||
temp->patternId = id; | ||
return temp; | ||
}; | ||
|
||
// A utility function to insert a new Interval Search Tree Node | ||
// This is similar to BST Insert. Here the low value of interval | ||
// is used to maintain BST property | ||
ITNode* insert(ITNode* root, Interval i, size_t id) | ||
{ | ||
// Base case: Tree is empty, new node becomes root | ||
if (root == NULL) | ||
return newNode(i, id); | ||
|
||
// Get low value of interval at root | ||
COORD l = root->i->low; | ||
|
||
// If root's low value is smaller, then new interval goes to | ||
// left subtree | ||
if (lessThan(i.low, l)) | ||
root->left = insert(root->left, i, id); | ||
|
||
// Else, new node goes to right subtree. | ||
else | ||
root->right = insert(root->right, i, id); | ||
|
||
// Update the max value of this ancestor if needed | ||
if (lessThan(root->max, i.high)) | ||
root->max = i.high; | ||
|
||
return root; | ||
} | ||
|
||
// A utility function to check if given two intervals overlap | ||
bool doOverlap(Interval i1, Interval i2) const | ||
{ | ||
if (lessThanOrEqual(i1.low, i2.low) && lessThan(i2.high, i1.high)) | ||
return true; | ||
return false; | ||
} | ||
|
||
// The main function that searches a given interval i in a given | ||
// Interval Tree. | ||
ITNode* overlapSearch(ITNode* root, Interval i) const | ||
{ | ||
// Base Case, tree is empty | ||
if (root == NULL) | ||
return NULL; | ||
|
||
// If given interval overlaps with root | ||
if (doOverlap(*(root->i), i)) | ||
return root; | ||
|
||
// If left child of root is present and max of left child is | ||
// greater than or equal to given interval, then i may | ||
// overlap with an interval is left subtree | ||
if (root->left != NULL && greaterThanOrEqual(root->left->max, i.high)) | ||
return overlapSearch(root->left, i); | ||
|
||
// Else interval can only overlap with right subtree | ||
return overlapSearch(root->right, i); | ||
} | ||
|
||
private: | ||
bool lessThan(const COORD x, const COORD y) const | ||
{ | ||
if (x.Y < y.Y) | ||
{ | ||
return true; | ||
} | ||
else if (x.Y == y.Y) | ||
{ | ||
return x.X < y.X; | ||
} | ||
return false; | ||
} | ||
|
||
bool lessThanOrEqual(const COORD x, const COORD y) const | ||
{ | ||
if (x.Y < y.Y) | ||
{ | ||
return true; | ||
} | ||
else if (x.Y == y.Y) | ||
{ | ||
return x.X <= y.X; | ||
} | ||
return false; | ||
} | ||
|
||
bool greaterThan(const COORD x, const COORD y) const | ||
{ | ||
if (x.Y > y.Y) | ||
{ | ||
return true; | ||
} | ||
else if (x.Y == y.Y) | ||
{ | ||
return x.X > y.X; | ||
} | ||
return false; | ||
} | ||
|
||
bool greaterThanOrEqual(const COORD x, const COORD y) const | ||
{ | ||
if (x.Y > y.Y) | ||
{ | ||
return true; | ||
} | ||
else if (x.Y == y.Y) | ||
{ | ||
return x.X >= y.X; | ||
} | ||
return false; | ||
} | ||
}; | ||
} |
8b95bd9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New misspellings found, please review:
To accept these changes, run the following commands
✏️ Contributor please read this
By default the command suggestion will generate a file named based on your commit. That's generally ok as long as you add the file to your commit. Someone can reorganize it later.
perl
command excluding the outer'
marks and dropping any'"
/"'
quotation mark pairs into a file and then runperl file.pl
from the root of the repository to run the code. Alternatively, you can manually insert the items...If the listed items are:
.github/actions/spell-check/dictionary/names.txt
..github/actions/spell-check/dictionary/
..github/actions/spell-check/expect/
..github/actions/spell-check/patterns/
.See the
README.md
in each directory for more information.🔬 You can test your commits without appending to a PR by creating a new branch with that extra change and pushing it to your fork. The :check-spelling action will run in response to your push -- it doesn't require an open pull request. By using such a branch, you can limit the number of typos your peers see you make. 😉
At present, the action that triggered this message will not show its ❌ in this PR unless the branch is within this repository.
Thus, you should make sure that this comment has been addressed before encouraging the merge bot to merge this PR.