Skip to content
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

Add sort methods to Slice #7597

Merged
merged 1 commit into from
May 30, 2019

Conversation

Maroo-b
Copy link
Contributor

@Maroo-b Maroo-b commented Mar 26, 2019

This PR depends on this fix: #7591

I extracted the sort logic from Array to a module. If this approach is approved, I'll try to add some docs for the module (or if someone has more skills with writing docs :) )

@asterite
Copy link
Member

You can now rebase against master :-)

@Maroo-b Maroo-b force-pushed the 6498_add_sorting_to_slice branch from 98e769f to eb98a1d Compare March 27, 2019 00:07
@straight-shoota
Copy link
Member

What's the purpose of Sortable module? It doesn't define an actual interface, just some class methods can easily be placed in an existing namespace like Slice or Array. Only intro_sort is publicly visible at all (that might even be questionable).

@yxhuvud
Copy link
Contributor

yxhuvud commented Mar 27, 2019

Is it possible to move sort, sort!, sort_by etc into Sortable?

@straight-shoota
Copy link
Member

@yxhuvud Yes that should work. The exclamation methods need a tiny adjustment, but #to_unsafe returns the pointer in both Array and Slice.

But I'm not sure if this module would be much of a benefit. Duplicating the code for sort methods is one thing, but that's not really an issue. The question is, is having a Sortable interface useful to users?

@Maroo-b
Copy link
Contributor Author

Maroo-b commented Mar 27, 2019

@straight-shoota
I extracted the sort logic from Array. Should I just rename the module to Sort for example and just require it in Array and Slice? or do you have another suggestion?

@asterite
Copy link
Member

@Maroo-b Your changes (the Sort module) don't show up for some reason...

My thoughts:

  • there's no need for a Sort or Sortable module that can be included. In fact there's no need for it to be generic, the T isn't used anywhere
  • I agree that the sort helper methods (currently in Sortable) should exist somewhere where this logic can be shared, but let's not use global names like Sort or Sortable which can collide with user-defined types. I think just putting the helper methods in Slice or Pointer::SortUtils or Pointer::SortHelpers is good enough.

@Maroo-b
Copy link
Contributor Author

Maroo-b commented Mar 27, 2019

@asterite thank you for the feedback!

Sure I agree, I'll move those methods to Pointer::SortUtils then :)

@Maroo-b Maroo-b force-pushed the 6498_add_sorting_to_slice branch 3 times, most recently from 081681b to 151ffea Compare March 27, 2019 15:40
@straight-shoota
Copy link
Member

IMHO the name Pointer::SortUtils doesn't make much sense. The sort algorithm is implemented on pointers, but conceptually, it operates on collections.

I'm still not convinced that we need a separate namespace for these methods anyway, but if we do, I'd call it Slice::Sort or Array::Sort. Or maybe Crystal::IntroSort?

@asterite
Copy link
Member

The way I imagine it, the sort methods belong to Slice. Then Array simply does Slice.new(to_unsafe, size).sort.

@Maroo-b
Copy link
Contributor Author

Maroo-b commented Mar 28, 2019

@straight-shoota @asterite just to confirm, should I move the sort logic to Slice and update Array to use Slice for sorting?
I'm concerned about creating a Slice instance just for sorting. is it fine?

@asterite
Copy link
Member

It's fine, Slice is a struct. And yes, please move the methods to Slice. Thank you!

@straight-shoota
Copy link
Member

Creating a struct is basically free of cost.

I'd suggest to add Array#to_slice as a shortcut for Slice.new(to_unsafe, size). Might be useful as a public method, too.

@bew
Copy link
Contributor

bew commented Mar 28, 2019

@straight-shoota but we'd get back to #5173 (comment) ?

@RX14 wrote: We removed Array#to_slice because it would cause a segfault if you kept a reference to the slice returned from that method and the array was resized.

@Maroo-b Maroo-b force-pushed the 6498_add_sorting_to_slice branch from 151ffea to 2d8fb36 Compare March 28, 2019 23:31
@straight-shoota
Copy link
Member

Looks great!

I'd prefer to put the sort methods in a separate file (e.g. src/slice/sort.cr) though for some kind of compartmentalization.

@Maroo-b Maroo-b force-pushed the 6498_add_sorting_to_slice branch 2 times, most recently from 722218f to d6d8a5e Compare March 29, 2019 13:34
@Maroo-b
Copy link
Contributor Author

Maroo-b commented Mar 29, 2019

@straight-shoota thank you for the reviews, the PR was updated as suggested.

Copy link
Member

@straight-shoota straight-shoota left a comment

Choose a reason for hiding this comment

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

Looks great

@bew
Copy link
Contributor

bew commented Mar 29, 2019

What about moving the specs to spec/std/slice/sort_spec.cr ?

@Maroo-b
Copy link
Contributor Author

Maroo-b commented Mar 29, 2019

@bew it's an option, but I prefer to keep Slice specs in one file for the moment.

@Maroo-b Maroo-b force-pushed the 6498_add_sorting_to_slice branch from d6d8a5e to 2d58ecc Compare March 29, 2019 17:12
@Maroo-b Maroo-b force-pushed the 6498_add_sorting_to_slice branch from 2d58ecc to a7ef60a Compare April 28, 2019 16:23
@Maroo-b Maroo-b force-pushed the 6498_add_sorting_to_slice branch from a7ef60a to 93ebc49 Compare April 28, 2019 16:46
@Maroo-b
Copy link
Contributor Author

Maroo-b commented Apr 28, 2019

PR updated to resolve conflicts and apply changes from this PR: #7639

@Sija
Copy link
Contributor

Sija commented May 29, 2019

Seems like GTG 🚀

Copy link
Member

@sdogruyol sdogruyol left a comment

Choose a reason for hiding this comment

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

Thank you @Maroo-b 🙏

@asterite asterite added this to the 0.29.0 milestone May 30, 2019
@asterite asterite merged commit f466695 into crystal-lang:master May 30, 2019
@Maroo-b Maroo-b deleted the 6498_add_sorting_to_slice branch June 3, 2019 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants