-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
Sortable set #5083
Merged
Merged
Sortable set #5083
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0eaa847
add SortableSet as a new collection type
timse 747efca
use SortableSet in chunks
timse 4e5ef0d
use SortableSet in modules
timse bd8c6cf
make sure ConcatenatedModules also uses SortedSet
timse 1a16a3e
make sort function private
timse 7a99930
optimize sortableSet to prevent unnecessary sorting
timse f7ffed8
allow to set chunks and use it in ConcatenatedModule
timse 5f16dd8
clean up SortableSet
timse b2a4244
add tests for sortableset
timse 8f3d65a
fix linting/formatting issues
timse File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"use strict"; | ||
|
||
module.exports = class SortableSet extends Set { | ||
|
||
constructor(initialIterable, defaultSort) { | ||
super(initialIterable); | ||
this._sortFn = defaultSort; | ||
this._lastActiveSortFn = null; | ||
} | ||
|
||
/** | ||
* @param {any} value - value to add to set | ||
* @returns {SortableSet} - returns itself | ||
*/ | ||
add(value) { | ||
this._lastActiveSortFn = null; | ||
super.add(value); | ||
return this; | ||
} | ||
|
||
/** | ||
* @param {Function} sortFn - function to sort the set | ||
* @returns {void} | ||
*/ | ||
sortWith(sortFn) { | ||
if(this.size === 0 || sortFn === this._lastActiveSortFn) { | ||
// already sorted - nothing to do | ||
return; | ||
} | ||
|
||
const sortedArray = Array.from(this).sort(sortFn); | ||
super.clear(); | ||
for(let i = 0; i < sortedArray.length; i += 1) { | ||
this.add(sortedArray[i]); | ||
} | ||
this._lastActiveSortFn = sortFn; | ||
} | ||
|
||
/** | ||
* @returns {void} | ||
*/ | ||
sort() { | ||
this.sortWith(this._sortFn); | ||
} | ||
}; |
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,29 @@ | ||
/* globals describe, it */ | ||
"use strict"; | ||
|
||
const SortableSet = require("../lib/util/SortableSet"); | ||
|
||
describe("util/SortableSet", () => { | ||
it("Can be constructed like a normal Set", () => { | ||
const sortableSet = new SortableSet([1, 1, 1, 1, 1, 4, 5, 2], () => {}); | ||
Array.from(sortableSet).should.eql([1, 4, 5, 2]); | ||
}); | ||
|
||
it("Can sort its content", () => { | ||
const sortableSet = new SortableSet( | ||
[1, 1, 1, 6, 6, 1, 1, 4, 5, 2, 3, 8, 5, 7, 9, 0, 3, 1], | ||
(a, b) => a - b | ||
); | ||
sortableSet.sort(); | ||
Array.from(sortableSet).should.eql([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | ||
}); | ||
|
||
it("Can sort by a specified function", () => { | ||
const sortableSet = new SortableSet( | ||
[1, 1, 1, 6, 6, 1, 1, 4, 5, 2, 3, 8, 5, 7, 9, 0, 3, 1], | ||
(a, b) => a - b | ||
); | ||
sortableSet.sortWith((a, b) => b - a); | ||
Array.from(sortableSet).should.eql([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]); | ||
}); | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 should not happen?
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.
see: bd8c6cf
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.
Ah okey. 👍
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.
It seem to be different again...
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.
ha yea, it tries to use the sort method from
Module
but its no longer exposed...meh, i'd love to just have a
compareBy
method on them :P maybe in another PR if thats ok :D