-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
floor and ceiling for Sorted Arrays #804
base: master
Are you sure you want to change the base?
Conversation
…ial implementation for Object type and each primitive type. A few test cases are also included.
I have no idea what this new code does as there are ZERO Javadocs, so there is nothing to review from my POV. There is also a lot of code duplication but I am not sure if lambdas and primitives would mix in an effective manner here. |
Thank you for your feedback @garydgregory :) I will add Javadoc asap. This PR adds two methods
At the time I was writing the code, I could not find a better way to write overloads of the functions for different primitive Java types. Any suggestions is greatly appreciated. |
What happens if the array is not sorted? Is it possible for the code to cause an infinite loop? |
… code to avoid having magic numbers
Hi @garydgregory , I have added Javadoc and refined a code a little bit to make it similar to the rest of the codebase (e.g., I avoided using
This algorithm is based on binary search, so we assume the array will be sorted (at least within the specified range) prior to passing it to the method. Thanks |
Hi @kinow, @chtompki, and all: I am pondering on this PR again: Since floor and ceiling are mathematical concepts, I would think it belongs in Commons Math, which already has functions to implement these concepts: https://commons.apache.org/proper/commons-math/javadocs/api-3.4/org/apache/commons/math3/analysis/UnivariateFunction.html I do not know how easy it is to couple the above CM functions with arrays, but it's a question for the user's mailing list ;-) Thoughts? |
Thanks, @garydgregory , for reviewing my PR. Many thanks |
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.
Hi again @ali-ghanbari 👋
Code looks good, docs and tests well written too. Thanks! 👍
I am pondering on this PR again: Since floor and ceiling are mathematical concepts, I would think it belongs in Commons Math, which already has functions to implement these concepts: https://commons.apache.org/proper/commons-math/javadocs/api-3.4/org/apache/commons/math3/analysis/UnivariateFunction.html
Good point on whether it belongs to Lang or to another component. I haven't been following commons-math, but I thought it was also being split into smaller modules. Maybe commons-math or another of those math related modules would make more sense.
Or maybe commons-collections? The JDK has a NavigableSet
1 which has methods for floor
and ceiling
for a sorted set. Commons Collections has the AbstractNavigableSetDecorator
class that also implements those methods 2.
I think if I were to search for these functions/methods, I would probably look for them in the collections component, and if not found then the lang or math-related components, in this order.
Bruno
Footnotes
Hi @kinow I'd prefer to avoid feature creep in Lang in the form of a "non-java.lang" domain coming in which would open the door to request for perhaps other "mathy" requests: "Well, if floor and ceiling are in there, why not foo and bar?" Somewhat in the same vein as we are looking at Commons Text for text-based functionality instead of Lang. IOW, focus. Since we are talking about arrays here, Commons Collections does not feel like a good fit. |
Hi @kinow ! Thanks for your constructive comments and thanks for reviewing the code :) |
I understand @garydgregory 's concern about keeping I also suggest adding this class to: Commons Lang or Commons Collections for we are essentially extending As soon as we decide on the right module for this, I will raise my PR :) Thanks |
If I am not mistaking, JDK standard library and the current implementation of Apache Commons Lang do not have functions for binary searching for
floor
andceiling
in sorted arrays.This PR adds efficient implementation for
floor
andceiling
inside the classArrayUtils
(Java'sArrays
class hasbinarySearch
methods, so I thought this class would be a good place for these methods).The methods handle various Java types.
A few test cases are also included.