OLD | NEW |
---|---|
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 3412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3423 | 3423 |
3424 return new_descriptors; | 3424 return new_descriptors; |
3425 } | 3425 } |
3426 | 3426 |
3427 | 3427 |
3428 void DescriptorArray::Sort() { | 3428 void DescriptorArray::Sort() { |
3429 // In-place heap sort. | 3429 // In-place heap sort. |
3430 int len = number_of_descriptors(); | 3430 int len = number_of_descriptors(); |
3431 | 3431 |
3432 // Bottom-up max-heap construction. | 3432 // Bottom-up max-heap construction. |
3433 for (int i = 1; i < len; ++i) { | 3433 // Index of the last node with children |
3434 int child_index = i; | 3434 const int max_parent_index = len/2 - 1; |
Mads Ager (chromium)
2010/03/04 08:24:41
Space around binary operations and use parens for
| |
3435 while (child_index > 0) { | 3435 for (int i = max_parent_index; i >= 0; --i) { |
3436 int parent_index = ((child_index + 1) >> 1) - 1; | 3436 int parent_index = i; |
3437 uint32_t parent_hash = GetKey(parent_index)->Hash(); | 3437 const uint32_t parent_hash = GetKey(i)->Hash(); |
3438 while (parent_index <= max_parent_index) { | |
3439 int child_index = 2 * parent_index + 1; | |
3438 uint32_t child_hash = GetKey(child_index)->Hash(); | 3440 uint32_t child_hash = GetKey(child_index)->Hash(); |
3439 if (parent_hash < child_hash) { | 3441 if (child_index + 1 < len) { |
3440 Swap(parent_index, child_index); | 3442 uint32_t right_child_hash = GetKey(child_index + 1)->Hash(); |
3441 } else { | 3443 if (right_child_hash > child_hash) { |
3442 break; | 3444 child_index++; |
3445 child_hash = right_child_hash; | |
3446 } | |
3443 } | 3447 } |
3444 child_index = parent_index; | 3448 if (child_hash <= parent_hash) break; |
3449 Swap(parent_index, child_index); | |
3450 // Now element at child_index could be < its children. | |
3451 parent_index = child_index; // parent_hash remains correct. | |
3445 } | 3452 } |
3446 } | 3453 } |
3447 | 3454 |
3448 // Extract elements and create sorted array. | 3455 // Extract elements and create sorted array. |
3449 for (int i = len - 1; i > 0; --i) { | 3456 for (int i = len - 1; i > 0; --i) { |
3450 // Put max element at the back of the array. | 3457 // Put max element at the back of the array. |
3451 Swap(0, i); | 3458 Swap(0, i); |
3452 // Sift down the new top element. | 3459 // Sift down the new top element. |
3453 int parent_index = 0; | 3460 int parent_index = 0; |
3454 while (true) { | 3461 const uint32_t parent_hash = GetKey(parent_index)->Hash(); |
3455 int child_index = ((parent_index + 1) << 1) - 1; | 3462 const int max_parent_index = i/2 - 1; |
Mads Ager (chromium)
2010/03/04 08:24:41
Ditto.
| |
3456 if (child_index >= i) break; | 3463 while (parent_index <= max_parent_index) { |
3457 uint32_t child1_hash = GetKey(child_index)->Hash(); | 3464 int child_index = parent_index * 2 + 1; |
3458 uint32_t child2_hash = GetKey(child_index + 1)->Hash(); | 3465 uint32_t child_hash = GetKey(child_index)->Hash(); |
3459 uint32_t parent_hash = GetKey(parent_index)->Hash(); | 3466 if (child_index + 1 < i) { |
3460 if (child_index + 1 >= i || child1_hash > child2_hash) { | 3467 uint32_t right_child_hash = GetKey(child_index + 1)->Hash(); |
3461 if (parent_hash > child1_hash) break; | 3468 if (right_child_hash > child_hash) { |
3462 Swap(parent_index, child_index); | 3469 child_index++; |
3463 parent_index = child_index; | 3470 child_hash = right_child_hash; |
3464 } else { | 3471 } |
3465 if (parent_hash > child2_hash) break; | |
3466 Swap(parent_index, child_index + 1); | |
3467 parent_index = child_index + 1; | |
3468 } | 3472 } |
3473 if (child_hash <= parent_hash) break; | |
3474 Swap(parent_index, child_index); | |
3475 parent_index = child_index; | |
3469 } | 3476 } |
3470 } | 3477 } |
3471 | 3478 |
3472 SLOW_ASSERT(IsSortedNoDuplicates()); | 3479 SLOW_ASSERT(IsSortedNoDuplicates()); |
3473 } | 3480 } |
3474 | 3481 |
3475 | 3482 |
3476 int DescriptorArray::BinarySearch(String* name, int low, int high) { | 3483 int DescriptorArray::BinarySearch(String* name, int low, int high) { |
3477 uint32_t hash = name->Hash(); | 3484 uint32_t hash = name->Hash(); |
3478 | 3485 |
3479 while (low <= high) { | 3486 while (low <= high) { |
3480 int mid = (low + high) / 2; | 3487 int mid = (low + high) / 2; |
3481 String* mid_name = GetKey(mid); | 3488 String* mid_name = GetKey(mid); |
(...skipping 4880 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
8362 if (break_point_objects()->IsUndefined()) return 0; | 8369 if (break_point_objects()->IsUndefined()) return 0; |
8363 // Single beak point. | 8370 // Single beak point. |
8364 if (!break_point_objects()->IsFixedArray()) return 1; | 8371 if (!break_point_objects()->IsFixedArray()) return 1; |
8365 // Multiple break points. | 8372 // Multiple break points. |
8366 return FixedArray::cast(break_point_objects())->length(); | 8373 return FixedArray::cast(break_point_objects())->length(); |
8367 } | 8374 } |
8368 #endif | 8375 #endif |
8369 | 8376 |
8370 | 8377 |
8371 } } // namespace v8::internal | 8378 } } // namespace v8::internal |
OLD | NEW |