-
Notifications
You must be signed in to change notification settings - Fork 688
Add %TypedArray%.prototype.sort([ compareFunction ]) support. #2437
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
Merged
LaszloLango
merged 1 commit into
jerryscript-project:master
from
AnthonyCalandra:typedarray-sort-support
Aug 22, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
146 changes: 146 additions & 0 deletions
146
jerry-core/ecma/builtin-objects/ecma-builtin-helpers-sort.c
This file contains hidden or 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,146 @@ | ||
| /* Copyright JS Foundation and other contributors, http://js.foundation | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "ecma-builtin-helpers.h" | ||
| #include "ecma-globals.h" | ||
| #include "ecma-try-catch-macro.h" | ||
|
|
||
| /** | ||
| * Function used to reconstruct the ordered binary tree. | ||
| * Shifts 'index' down in the tree until it is in the correct position. | ||
| * | ||
| * @return ecma value | ||
| * Returned value must be freed with ecma_free_value. | ||
| */ | ||
| static ecma_value_t | ||
| ecma_builtin_helper_array_to_heap (ecma_value_t *array_p, /**< heap data array */ | ||
| uint32_t index, /**< current item index */ | ||
| uint32_t right, /**< right index is a maximum index */ | ||
| ecma_value_t compare_func, /**< compare function */ | ||
| const ecma_builtin_helper_sort_compare_fn_t sort_cb) /**< sorting cb */ | ||
| { | ||
| ecma_value_t ret_value = ECMA_VALUE_EMPTY; | ||
|
|
||
| /* Left child of the current index. */ | ||
| uint32_t child = index * 2 + 1; | ||
| ecma_value_t swap = array_p[index]; | ||
| bool should_break = false; | ||
|
|
||
| while (child <= right && ecma_is_value_empty (ret_value) && !should_break) | ||
| { | ||
| if (child < right) | ||
| { | ||
| /* Compare the two child nodes. */ | ||
| ECMA_TRY_CATCH (child_compare_value, sort_cb (array_p[child], array_p[child + 1], compare_func), | ||
| ret_value); | ||
|
|
||
| JERRY_ASSERT (ecma_is_value_number (child_compare_value)); | ||
|
|
||
| /* Use the child that is greater. */ | ||
| if (ecma_get_number_from_value (child_compare_value) < ECMA_NUMBER_ZERO) | ||
| { | ||
| child++; | ||
| } | ||
|
|
||
| ECMA_FINALIZE (child_compare_value); | ||
| } | ||
|
|
||
| if (ecma_is_value_empty (ret_value)) | ||
| { | ||
| JERRY_ASSERT (child <= right); | ||
|
|
||
| /* Compare current child node with the swap (tree top). */ | ||
| ECMA_TRY_CATCH (swap_compare_value, sort_cb (array_p[child], swap, compare_func), ret_value); | ||
| JERRY_ASSERT (ecma_is_value_number (swap_compare_value)); | ||
|
|
||
| if (ecma_get_number_from_value (swap_compare_value) <= ECMA_NUMBER_ZERO) | ||
| { | ||
| /* Break from loop if current child is less than swap (tree top) */ | ||
| should_break = true; | ||
| } | ||
| else | ||
| { | ||
| /* We have to move 'swap' lower in the tree, so shift current child up in the hierarchy. */ | ||
| uint32_t parent = (child - 1) / 2; | ||
| JERRY_ASSERT (parent <= right); | ||
| array_p[parent] = array_p[child]; | ||
|
|
||
| /* Update child to be the left child of the current node. */ | ||
| child = child * 2 + 1; | ||
| } | ||
|
|
||
| ECMA_FINALIZE (swap_compare_value); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * Loop ended, either current child does not exist, or is less than swap. | ||
| * This means that 'swap' should be placed in the parent node. | ||
| */ | ||
| uint32_t parent = (child - 1) / 2; | ||
| JERRY_ASSERT (parent <= right); | ||
| array_p[parent] = swap; | ||
|
|
||
| if (ecma_is_value_empty (ret_value)) | ||
| { | ||
| ret_value = ECMA_VALUE_UNDEFINED; | ||
| } | ||
|
|
||
| return ret_value; | ||
| } /* ecma_builtin_helper_array_to_heap */ | ||
|
|
||
| /** | ||
| * Heapsort function | ||
| * | ||
| * @return ecma value | ||
| * Returned value must be freed with ecma_free_value. | ||
| */ | ||
| ecma_value_t | ||
| ecma_builtin_helper_array_heap_sort_helper (ecma_value_t *array_p, /**< array to sort */ | ||
| uint32_t right, /**< right index */ | ||
| ecma_value_t compare_func, /**< compare function */ | ||
| const ecma_builtin_helper_sort_compare_fn_t sort_cb) /**< sorting cb */ | ||
| { | ||
| ecma_value_t ret_value = ECMA_VALUE_EMPTY; | ||
|
|
||
| /* First, construct the ordered binary tree from the array. */ | ||
| for (uint32_t i = (right / 2) + 1; i > 0 && ecma_is_value_empty (ret_value); i--) | ||
| { | ||
| ECMA_TRY_CATCH (value, | ||
| ecma_builtin_helper_array_to_heap (array_p, i - 1, right, compare_func, sort_cb), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also changed
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
| ret_value); | ||
| ECMA_FINALIZE (value); | ||
| } | ||
|
|
||
| /* Sorting elements. */ | ||
| for (uint32_t i = right; i > 0 && ecma_is_value_empty (ret_value); i--) | ||
| { | ||
| /* | ||
| * The top element will always contain the largest value. | ||
| * Move top to the end, and remove it from the tree. | ||
| */ | ||
| ecma_value_t swap = array_p[0]; | ||
| array_p[0] = array_p[i]; | ||
| array_p[i] = swap; | ||
|
|
||
| /* Rebuild binary tree from the remaining elements. */ | ||
| ECMA_TRY_CATCH (value, | ||
| ecma_builtin_helper_array_to_heap (array_p, 0, i - 1, compare_func, sort_cb), | ||
| ret_value); | ||
| ECMA_FINALIZE (value); | ||
| } | ||
|
|
||
| return ret_value; | ||
| } /* ecma_builtin_helper_array_heap_sort_helper */ | ||
Oops, something went wrong.
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.
To allow having the type of the index a
uint32_tI made a small change here where I added one more toiand changed the condition fromi >= 0toi > 0because before you would decrement wheni = 0(which would previously terminate the loop) but after making the value unsigned this would wrap to the max unsigned value and so the loop would never terminate.