// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/../../pkgs/RNBasics/RNHeap.cpp
// Function: gaps::RNHeap<gaps::R3MeshEdge*>::Remove(gaps::R3MeshEdge*)
void RNHeap<PtrType>::
Remove(PtrType entry)
{
  // Search for entry
  PtrType *entryp = NULL;
  if (entry_offset >= 0) entryp = *((PtrType **) ((unsigned char *) entry + entry_offset));
  if (entry_callback) entryp = *((PtrType **) (*entry_callback)(entry, callback_data));
  else {
    // Find entry in heap
    for (int i = 0; i < nentries; i++) {
      if (entries[i] == entry) {
        entryp = &entries[i];
        break;
      }
    }
  }

  // Move data into place
  if (!entryp) return;
  int index = entryp - entries;
  assert((0 <= index) && (index < nentries));

  // Update deleted entry backpointer
  if (entry_offset >= 0) *((PtrType **) ((unsigned char *) entries[index] + entry_offset)) = NULL;
  if (entry_callback) *((PtrType **) (*entry_callback)(entries[index], callback_data)) = NULL;

  // Remove entry, by copying tail over it
  entries[index] = entries[nentries-1];

  // Update new entry[0] backpointer
  if (entry_offset >= 0) *((PtrType **) ((unsigned char *) entries[index] + entry_offset)) = &entries[index];
  if (entry_callback) *((PtrType **) (*entry_callback)(entries[index], callback_data)) = &entries[index];

  // Decrement number of entries
  nentries--;

  // Bubble the entry down to its rightful spot
  BubbleDown(index);
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
cmpl $0x0, 0x14(%rax)
jl 0x37fce
movq 0x10(%rsp), %rcx
movq 0x28(%rsp), %rax
movslq 0x14(%rcx), %rcx
movq (%rax,%rcx), %rax
movq %rax, 0x20(%rsp)
movq 0x10(%rsp), %rax
cmpq $0x0, 0x20(%rax)
je 0x37ff8
movq 0x10(%rsp), %rcx
movq 0x20(%rcx), %rax
movq 0x28(%rsp), %rdi
movq 0x28(%rcx), %rsi
callq *%rax
movq (%rax), %rax
movq %rax, 0x20(%rsp)
jmp 0x38052
movl $0x0, 0x1c(%rsp)
movq 0x10(%rsp), %rcx
movl 0x1c(%rsp), %eax
cmpl 0x8(%rcx), %eax
jge 0x38050
movq 0x10(%rsp), %rax
movq (%rax), %rax
movslq 0x1c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
cmpq 0x28(%rsp), %rax
jne 0x38041
movq 0x10(%rsp), %rax
movq (%rax), %rax
movslq 0x1c(%rsp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
jmp 0x38050
jmp 0x38043
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x38000
jmp 0x38052
cmpq $0x0, 0x20(%rsp)
jne 0x3805f
jmp 0x38171
movq 0x10(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rax), %rdx
subq %rdx, %rcx
sarq $0x3, %rcx
movl %ecx, 0x18(%rsp)
cmpl $0x0, 0x14(%rax)
jl 0x3809a
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
movslq 0x18(%rsp), %rdx
movq (%rax,%rdx,8), %rax
movslq 0x14(%rcx), %rcx
movq $0x0, (%rax,%rcx)
movq 0x10(%rsp), %rax
cmpq $0x0, 0x20(%rax)
je 0x380c8
movq 0x10(%rsp), %rcx
movq 0x20(%rcx), %rax
movq (%rcx), %rdx
movslq 0x18(%rsp), %rsi
movq (%rdx,%rsi,8), %rdi
movq 0x28(%rcx), %rsi
callq *%rax
movq $0x0, (%rax)
movq 0x10(%rsp), %rax
movq (%rax), %rcx
movl 0x8(%rax), %edx
subl $0x1, %edx
movslq %edx, %rdx
movq (%rcx,%rdx,8), %rsi
movq (%rax), %rcx
movslq 0x18(%rsp), %rdx
movq %rsi, (%rcx,%rdx,8)
cmpl $0x0, 0x14(%rax)
jl 0x38117
movq 0x10(%rsp), %rcx
movq (%rcx), %rdx
movslq 0x18(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rdx
movq (%rcx), %rax
movslq 0x18(%rsp), %rsi
movq (%rax,%rsi,8), %rax
movslq 0x14(%rcx), %rcx
movq %rdx, (%rax,%rcx)
movq 0x10(%rsp), %rax
cmpq $0x0, 0x20(%rax)
je 0x3815a
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
movslq 0x18(%rsp), %rdx
shlq $0x3, %rdx
addq %rdx, %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rcx), %rax
movq (%rcx), %rdx
movslq 0x18(%rsp), %rsi
movq (%rdx,%rsi,8), %rdi
movq 0x28(%rcx), %rsi
callq *%rax
movq 0x8(%rsp), %rcx
movq %rcx, (%rax)
movq 0x10(%rsp), %rdi
movl 0x8(%rdi), %eax
addl $-0x1, %eax
movl %eax, 0x8(%rdi)
movl 0x18(%rsp), %esi
callq 0x3cb10
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)