| 626 | } |
| 627 | |
| 628 | void UArray_leave_thenRemove_(UArray *self, size_t itemsToLeave, |
| 629 | size_t itemsToRemove) { |
| 630 | if (itemsToLeave <= 0) { |
| 631 | UArray_clear(self); |
| 632 | UArray_setSize_(self, 0); |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | if (itemsToRemove <= 0) { |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | { |
| 641 | size_t tailChunkSizeInBytes; |
| 642 | |
| 643 | size_t period = itemsToLeave + itemsToRemove; |
| 644 | size_t tailItemCount = UArray_size(self) % period; |
| 645 | size_t itemSize = self->itemSize; |
| 646 | size_t chunkSizeInBytes = itemSize * itemsToLeave; |
| 647 | |
| 648 | if (tailItemCount == 0) { |
| 649 | tailChunkSizeInBytes = 0; |
| 650 | } else if (tailItemCount <= itemsToLeave) { |
| 651 | tailChunkSizeInBytes = tailItemCount * itemSize; |
| 652 | } else { |
| 653 | tailChunkSizeInBytes = chunkSizeInBytes; |
| 654 | } |
| 655 | |
| 656 | { |
| 657 | size_t chunkCount = UArray_size(self) / period; |
| 658 | size_t newItemCount = |
| 659 | chunkCount * itemsToLeave + tailChunkSizeInBytes / itemSize; |
| 660 | uint8_t *newData = malloc(newItemCount * itemSize); |
| 661 | |
| 662 | { |
| 663 | size_t chunkPos; |
| 664 | |
| 665 | for (chunkPos = 0; chunkPos < chunkCount; chunkPos++) { |
| 666 | memmove(newData + chunkPos * chunkSizeInBytes, |
| 667 | UARRAY_BYTESAT_(self, chunkPos * period), |
| 668 | chunkSizeInBytes); |
| 669 | } |
| 670 | |
| 671 | if (tailChunkSizeInBytes) { |
| 672 | memmove(newData + chunkPos * chunkSizeInBytes, |
| 673 | UARRAY_BYTESAT_(self, chunkPos * period), |
| 674 | tailChunkSizeInBytes); |
| 675 | } |
| 676 | |
| 677 | UArray_setData_type_size_copy_( |
| 678 | self, newData, UArray_itemType(self), newItemCount, 0); |
| 679 | UArray_changed(self); |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | BASEKIT_API void UArray_removeFirst(UArray *self) { |
no test coverage detected