// File: /workspace/github_cpp_makefile_star2_O2/wichtounet[P]articles/plf_colony_alpha/plf_colony.h
// Function: plf::colony<NonTrivialStringMovable, std::allocator<NonTrivialStringMovable>, unsigned int>::insert(NonTrivialStringMovable const&)
iterator insert(const element_type &element)
	{
		if (end_iterator.element_pointer != NULL)
		{
			switch(((erased_locations.total_number_of_elements != 0) << 1) | (end_iterator.element_pointer == reinterpret_cast<element_pointer_type>(end_iterator.group_pointer->skipfield)))
			{
				case 0: // ie. erased_locations is empty and end_iterator is not at end of current final group
				{
					const iterator return_iterator = end_iterator; /* Make copy for return before adjusting components */
					PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), end_iterator.element_pointer, element);

					++end_iterator.element_pointer; // not postfix incrementing prev statement as it would necessitate a try-catch block to reverse increment if necessary (which would decrease speed by increasing code size)
					++end_iterator.skipfield_pointer;
					++(end_iterator.group_pointer->last_endpoint);
					++(end_iterator.group_pointer->number_of_elements);
					++total_number_of_elements;

					return return_iterator; // return value before incrementing
				}
				case 1:	// ie. erased_locations is empty and end_iterator is at end of current final group - ie. colony is full - create new group
				{
					end_iterator.group_pointer->next_group = PLF_COLONY_ALLOCATE(group_allocator_type, group_allocator_pair, 1, end_iterator.group_pointer);
					group &next_group = *(end_iterator.group_pointer->next_group);

					try
					{
						#ifdef PLF_COLONY_VARIADICS_SUPPORT
							PLF_COLONY_CONSTRUCT(group_allocator_type, group_allocator_pair, &next_group, (total_number_of_elements < static_cast<size_type>(group_allocator_pair.max_elements_per_group)) ? static_cast<const skipfield_type>(total_number_of_elements) : group_allocator_pair.max_elements_per_group, end_iterator.group_pointer);
						#else // C++03 only supports copy construction
							PLF_COLONY_CONSTRUCT(group_allocator_type, group_allocator_pair, &next_group, group((total_number_of_elements < static_cast<size_type>(group_allocator_pair.max_elements_per_group)) ? static_cast<const skipfield_type>(total_number_of_elements) : group_allocator_pair.max_elements_per_group, end_iterator.group_pointer));
						#endif
					}
					catch (...)
					{
						PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, &next_group, 1);
						end_iterator.group_pointer->next_group = NULL;
						throw;
					}

					try
					{
						PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), next_group.elements, element);
					}
					catch (...)
					{
						PLF_COLONY_DESTROY(group_allocator_type, group_allocator_pair, &next_group);
						PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, &next_group, 1);
						end_iterator.group_pointer->next_group = NULL;
						throw;
					}

					end_iterator.group_pointer = &next_group;
					end_iterator.element_pointer = next_group.last_endpoint;
					end_iterator.skipfield_pointer = next_group.skipfield + 1;
					++total_number_of_elements;

					return iterator(end_iterator.group_pointer, next_group.elements, next_group.skipfield); /* returns value before incrementation */
				}
				default: // ie. erased_locations is not empty, reuse previous-erased element locations
				{
					iterator new_location;
					new_location.element_pointer = *erased_locations.top_element;
					PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), new_location.element_pointer, element);
					erased_locations.pop();

					new_location.group_pointer = end_iterator.group_pointer; // Start with last group first, as will be the largest group

					while (new_location.element_pointer < new_location.group_pointer->elements || new_location.element_pointer >= reinterpret_cast<element_pointer_type>(new_location.group_pointer->skipfield))
					{
						new_location.group_pointer = new_location.group_pointer->previous_group;
					}

					new_location.skipfield_pointer = new_location.group_pointer->skipfield + (new_location.element_pointer - new_location.group_pointer->elements);

					++(new_location.group_pointer->number_of_elements);

					if (new_location.group_pointer == first_group && new_location.element_pointer < begin_iterator.element_pointer)
					{ /* ie. begin_iterator was moved forwards as the result of an erasure at some point, this erased element is before the current begin, hence, set current begin iterator to this element */
						begin_iterator = new_location;
					}

					++total_number_of_elements;

					// Code logic for next section:
					// ============================
					// check whether location we are restoring to has a skipfield node before or after which is erased
					// if it has only a node before which is erased (ie. at end of erasure block), update the prime erasure point
					// if it only has a node after it which is erased, (ie. this is the prime erasure point), change next node to prime erasure point and update all subsequent erasure points (ie. decrement by 1)
					// if it has both a node before and after which is erased (ie. is in middle of erasure block), do both of the above

					// Explanation of the following optimization: we must avoid testing the left-hand skipfield node if we are already at the beginning of the skipfield, otherwise we create an out-of-bounds memory access.
					// To avoid this would Normally require a branching test ie. !is_at_start && left-hand-node != 0 (&& and || operations are conditional executation of the right-hand instruction, which causes branching). But instead we subtract 'test' (which is 0 if the skipfield node is at start of skipfield, 1 if not) from the skipfield node.
					// If not start of skipfield, this means we check to see if left-hand node is == 0 (value * 0).
					// If at start of skipfield, we perform an unneccesary test to see if the current skipfield node's value (*(skipfield - 0))
					// is == it's own value (value * 1). This may seem ridiculous, but it's less costly than branching - and since
					// node == skipfield_start is almost always going to be false, this needless check is only occasional.
					const skipfield_type value = *(new_location.skipfield_pointer);
					const bool test = (new_location.skipfield_pointer == new_location.group_pointer->skipfield);
					const unsigned char prev_skipfield = *(new_location.skipfield_pointer - !test) != value * test;
					const unsigned char after_skipfield = *(new_location.skipfield_pointer + 1) != 0; // NOTE: first test removed due to extra unused node in skipfield (required by operator ++)

					// Now we create a switch for the four different possible states (there are no additional instructions for state 0) depending on
					// whether the left and right-hand skipfield nodes are non-zero (or out-of-bounds), by bit-shifting the right-hand test and
					// logical-OR'ing it with the left-hand test:
					switch (prev_skipfield | (after_skipfield << 1))
					{
						case 1: // previous erased consecutive elements, none following
						{
							*(new_location.skipfield_pointer - (value - 1)) = value - 1;
							break;
						}
						case 2: // No previous consecutive erased points, at least one following ie. this was the prime erasure point
						{
							std::memmove(&*(new_location.skipfield_pointer + 2), &*(new_location.skipfield_pointer + 1), sizeof(skipfield_type) * (value - 2));
							*(new_location.skipfield_pointer + 1) = value - 1;
							break;
						}
						case 3: // both preceding and following consecutive erased elements
						{
							const skipfield_pointer_type start_node = new_location.skipfield_pointer - (value - 1);
							const skipfield_type update_count = *start_node - value;
							*start_node = value - 1;

							std::memmove(&*(new_location.skipfield_pointer + 2), &*(start_node + 1), sizeof(skipfield_type) * (update_count - 1));
							*(new_location.skipfield_pointer + 1) = update_count;
						}
					}

					*new_location.skipfield_pointer = 0;
					return new_location;
				}
			}
		}
		else // ie. newly-constructed colony, no insertions yet and no groups
		{
			initialize(min_elements_per_group);

			try
			{
				PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), end_iterator.element_pointer++, element);
			}
			catch (...)
			{
				clear();
				throw;
			}

			++end_iterator.skipfield_pointer;
			total_number_of_elements = 1;

			return begin_iterator; // returns value before incrementation
		}
	}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdi, %rbx
movq 0x8(%rsi), %rdi
movq %rdx, %r15
movq %rsi, %r14
testq %rdi, %rdi
je 0x15307
xorl %ecx, %ecx
cmpq $0x0, 0x70(%r14)
movq (%r14), %rax
setne %cl
xorl %edx, %edx
cmpq 0x18(%rax), %rdi
sete %dl
leal (%rdx,%rcx,2), %ecx
cmpl $0x1, %ecx
je 0x1534b
testl %ecx, %ecx
jne 0x153b3
movq %rax, (%rbx)
movq %rdi, 0x8(%rbx)
movq %r15, %rsi
movq 0x10(%r14), %rax
movq %rax, 0x10(%rbx)
callq 0x14020
vmovdqu 0x8(%r14), %xmm0
vpaddq 0x743d2(%rip), %xmm0, %xmm0 # 0x896c0
vmovdqu %xmm0, 0x8(%r14)
movq (%r14), %rax
addq $0x28, (%rax)
incl 0x30(%rax)
incq 0x38(%r14)
jmp 0x154d6
movl 0x40(%r14), %esi
movq %r14, %rdi
callq 0x155ae
movq 0x8(%r14), %rdi
leaq 0x28(%rdi), %rax
movq %rax, 0x8(%r14)
movq %r15, %rsi
callq 0x14020
addq $0x4, 0x10(%r14)
movq $0x1, 0x38(%r14)
vmovdqu 0x18(%r14), %xmm0
vmovdqu %xmm0, (%rbx)
movq 0x28(%r14), %rax
movq %rax, 0x10(%rbx)
jmp 0x154d6
pushq $0x38
popq %rdi
callq 0x41a0
movq (%r14), %rcx
movq %rax, %r12
movq %r12, 0x8(%rcx)
movq 0x38(%r14), %rsi
movl 0x44(%r14), %eax
movq (%r14), %rdx
cmpq %rax, %rsi
cmovael %eax, %esi
movq %r12, %rdi
callq 0x1565a
movq 0x10(%r12), %rdi
movq %r15, %rsi
callq 0x14020
movq %r12, (%r14)
movq (%r12), %rax
movq %rax, 0x8(%r14)
incq 0x38(%r14)
movq %r12, (%rbx)
vmovdqu 0x10(%r12), %xmm0
movq 0x18(%r12), %rax
addq $0x4, %rax
movq %rax, 0x10(%r14)
vmovdqu %xmm0, 0x8(%rbx)
jmp 0x154d6
movq 0x58(%r14), %rax
leaq 0x48(%r14), %r13
movq %r15, %rsi
movq (%rax), %r12
movq %r12, %rdi
movq %r12, 0x8(%rbx)
callq 0x14020
movq %r13, %rdi
callq 0x1556a
movq %r14, %rcx
movq (%rcx), %rcx
movq %r12, %rax
movq 0x10(%rcx), %rsi
subq %rsi, %rax
jb 0x153f0
movq 0x18(%rcx), %r15
cmpq %r15, %r12
jb 0x153f6
addq $0x20, %rcx
jmp 0x153d8
movq %rcx, (%rbx)
pushq $0xa
popq %rdi
cqto
idivq %rdi
addq %rax, %r15
movq %r15, 0x10(%rbx)
incl 0x30(%rcx)
cmpq 0x30(%r14), %rcx
jne 0x15423
cmpq 0x20(%r14), %r12
jae 0x15423
movq %rcx, 0x18(%r14)
movq %r12, 0x20(%r14)
movq %r15, 0x28(%r14)
incq 0x38(%r14)
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rsi, %r12
movq %r15, %rdx
movl (%r15), %r14d
setne %cl
cmovel %r14d, %eax
shll $0x2, %ecx
subq %rcx, %rdx
xorl %ecx, %ecx
cmpl %eax, (%rdx)
setne %cl
xorl %eax, %eax
cmpl $0x0, 0x4(%r15)
setne %al
leal (%rcx,%rax,2), %eax
leaq 0x74334(%rip), %rcx     # 0x89790
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
decl %r14d
movq %r15, %rcx
leaq (,%r14,4), %rax
subq %rax, %rcx
movl %r14d, (%rcx)
jmp 0x154d2
leal -0x2(%r14), %edx
leaq 0x4(%r15), %rsi
leaq 0x8(%r15), %rdi
shlq $0x2, %rdx
callq 0x4290
decl %r14d
movl %r14d, 0x4(%r15)
jmp 0x154d2
leal -0x1(%r14), %eax
movq %r15, %rsi
leaq 0x8(%r15), %rdi
leaq (,%rax,4), %rcx
subq %rcx, %rsi
negq %rcx
movl (%rsi), %r12d
addq $0x4, %rsi
movl %eax, (%r15,%rcx)
subl %r14d, %r12d
leal -0x1(%r12), %edx
shlq $0x2, %rdx
callq 0x4290
movl %r12d, 0x4(%r15)
andl $0x0, (%r15)
movq %rbx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq %rax, %rdi
callq 0x4090
movq %r12, %rdi
callq 0x156e4
movq %r12, %rdi
callq 0x4180
movq (%r14), %rax
andq $0x0, 0x8(%rax)
callq 0x4240
jmp 0x15552
movq %rax, %rbx
callq 0x42b0
jmp 0x1555a
movq %rax, %rdi
callq 0x4090
movq %r12, %rdi
callq 0x4180
movq (%r14), %rax
andq $0x0, 0x8(%rax)
callq 0x4240
jmp 0x15552
movq %rax, %rbx
callq 0x42b0
jmp 0x1555a
movq %rax, %rdi
callq 0x4090
movq %r14, %rdi
callq 0x15630
callq 0x4240
movq %rax, %rbx
callq 0x42b0
movq %rbx, %rdi
callq 0x42c0
movq %rax, %rdi
callq 0x7b4a