-
Notifications
You must be signed in to change notification settings - Fork 17
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
Allow use with simple custom objects #13
base: master
Are you sure you want to change the base?
Conversation
Change point_search such that it does not assume the interval has the `include?` method. This allows for custom objects to be used as intervals. Add specs for the behavior of the tree with custom interval objects. Specs include: * Tree creation * Searching for custom objects * Searching with custom objects Update the readme to include examples with custom objects, and update the notes to reflect how custom objects behave.
Great addition. This is exactly what this gem was missing. Thank you! Hope it gets accepted soon. |
@danielpetri1 If you want to use objects with data right now, you can use this pattern. The CustomStruct = Struct.new(:begin, :end, :value) do
def include?(other)
self.begin <= other && other < self.end
end
def first
self.begin
end
def last
self.end
end
end |
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.
Thanks, this is a good idea.
I'm not sure if the switch away from #include?
has an impact on ranges which include vs exclude the ending. Given all the specs pass, perhaps we were missing a spec for that? Or maybe everything always just gets converted to one form, and we lose that information. I can't remember =P I'll need to look into it when I have more time.
Cheers for the contribution!
That's exactly what happens. There is a line in the README which says:
I have added a note in this PR explaining how this idea is extended to non-range objects
|
You know, after thinking this over for a few days, I think that it actually makes more sense, in the long run, to lean into the range interface. I'm thinking about the annoying |
This PR is includes the work needed to be able to use the interval tree with simple classes and structs, such as this:
I made the following changes:
Use
begin
instead offirst
, andend
instead oflast
Change point_search such that it does not assume the interval
has the
include?
method. This allows for custom objects tobe used as intervals.
Add specs for the behavior of the tree with custom
interval objects. Specs include:
Update the readme to include examples with custom objects, and
update the notes to reflect how custom objects behave.