-
Notifications
You must be signed in to change notification settings - Fork 246
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
Include custom less-than in AVL Tree #729
base: master
Are you sure you want to change the base?
Conversation
Allows one to pass a custom isless function to the AVLTree constructor, which is necessary when simply overloading isless is not possible due to scope issues.
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 for this awesome PR. 😃 I had thought about it, while implementing this, but couldn't ultimately incorporate these changes. I've requested a few changed, mostly related to coding style(space
following every variables and punctuations/operators). Resolve those, and it looks good for merging.
Co-authored-by: Koustav Chowdhury <kc99.kol@gmail.com>
Co-authored-by: Koustav Chowdhury <kc99.kol@gmail.com>
Co-authored-by: Koustav Chowdhury <kc99.kol@gmail.com>
Co-authored-by: Koustav Chowdhury <kc99.kol@gmail.com>
Co-authored-by: Koustav Chowdhury <kc99.kol@gmail.com>
Co-authored-by: Koustav Chowdhury <kc99.kol@gmail.com>
Co-authored-by: Koustav Chowdhury <kc99.kol@gmail.com>
Define type of lt
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 for this awesome PR. I had thought about it, while implementing this, but couldn't ultimately incorporate these changes. I've requested a few changed, mostly related to coding style(
space
following every variables and punctuations/operators). Resolve those, and it looks good for merging.
Yea, I was implementing Bentley-Ottmann and this was the least hacky way of doing it. I'm a pretty big github novice (first every PR), so my apologies if I'm doing this wrong. I think(?) I have responded to your changes, please let me know if they haven't gone through though. Thanks!
src/avl_tree.jl
Outdated
@@ -19,8 +19,9 @@ AVLTreeNode_or_null{T} = Union{AVLTreeNode{T}, Nothing} | |||
mutable struct AVLTree{T} | |||
root::AVLTreeNode_or_null{T} | |||
count::Int | |||
lt::Function |
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.
The only thing I am deliberating is the member name, lt
. I suggest comp
for better readability of the code. Otherwise this looks good to me.
Also, try squashing the commits, into single one. A lot of Update src/avl_tree.jl
doesn't make sense in the long scheme of things. A meaningful commit message such as Added a custom compare to AVLTree
will be good.
The codes looks fine to be merged after these two changes.
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.
In isolation I agree, but I went with lt
for consistency with sort
and all the other sorting functions, which were the only other Julia functions could think of which take a comparison as an optional parameter. If you still prefer comp
I can switch it over though.
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.
This is an abstractly typed field.
That is going to make things slow as it will cause a dynamic dispatch.
Instead do:
mutable struct AVLTree{T, F}
root::AVLTreeNode_or_null{T}
count::Int
lt::F
end
Just noting that the sorted containers take an order argument.
From: Christopher T. Chubb ***@***.***
Sent: Sunday, March 21, 2021 5:06 PM
To: JuliaCollections/DataStructures.jl ***@***.***>
Cc: Subscribed ***@***.***>
Subject: Re: [JuliaCollections/DataStructures.jl] Include custom less-than in AVL Tree (#729)
@chubbc commented on this pull request.
________________________________
In src/avl_tree.jl<#729 (comment)>:
@@ -19,8 +19,9 @@ AVLTreeNode_or_null{T} = Union{AVLTreeNode{T}, Nothing}
mutable struct AVLTree{T}
root::AVLTreeNode_or_null{T}
count::Int
+ lt::Function
In isolation I agree, but I went with lt for consistency with sort and all the other sorting functions, which were the only other Julia functions could think of which take a comparison as an optional parameter. If you still prefer comp I can switch it over though.
-
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub<#729 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AB4EJB3F6XEGIRXVCSPAX6LTEZNSXANCNFSM4ZLFGOSA>.
|
I had thought of something like this, but I think it is strictly less powerful. I was trying to use the AVLTree in a situation in which my |
It is possible to define an order object that takes a non-global function. Suppose the nonglobal function is struct MyCompare{T} <: Ordering
comparison_function::T
end
lt(o::MyCompare{T} where T, x, y) = o.comparison_function(x,y);
function uses_sorted_dict()
my_lt = ## some complicated closure goes here
s = SortedDict{MyKeyType, MyValueType, MyCompare(my_lt)}()
end |
Sorry, I forgot that the struct denoted |
Right okay, yea that would be an alternative. I'm not sure I see the point of using Lt over this though, because it seems to just wrap a function being passed in anyway, no? I might be missing something though |
The point of using Ordering and Lt for this purpose rather than a placeholder for a function is to give the user access to all the tools in ordering.jl, not just Lt.
Since the user right now is only you, and the only tool you want is a function place-holder, I won't insist on the point too much.
However, now that I look at the PR again, I see a more pressing issue with your code, which is that lt as a member function has an undeclared type, i.e., its type is Any. This means that the compiler will not be able to deduce the type of lt at compile time, and all the AVL tree functions will need run-time dispatch for lt invocations, which is a significant performance hit.
If you do not want to implement the Ordering infrastructure but would prefer to stick to a placeholder for a function, then the solution to the performance issue is to have a second type parameter for AVLTreeNode, say C, and declare the type of lt to be C.
From: Christopher T. Chubb ***@***.***
Sent: Sunday, March 21, 2021 10:40 PM
To: JuliaCollections/DataStructures.jl ***@***.***>
Cc: Stephen Vavasis ***@***.***>; Comment ***@***.***>
Subject: Re: [JuliaCollections/DataStructures.jl] Include custom less-than in AVL Tree (#729)
Right okay, yea that would be an alternative. I'm not sure I see the point of using Lt over this though, because it seems to just wrap a function being passed in anyway, no? I might be missing something though
-
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<#729 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AB4EJB5V7ZPGALJEQVNGV23TE2UZPANCNFSM4ZLFGOSA>.
|
Cool, thanks. 👍 |
Switch the specification of the ordering from an `lt` function to a `Base.Order.Ordering`
Allows one to pass a custom isless function to the AVLTree constructor, which is necessary when simply overloading isless is not possible due to scope issues.