INT32 GenericTreeModel::GetLastChildIndex(INT32 index) { INT32 child = GetChildIndex(index); for (INT32 next_child = GetSiblingIndex(child); next_child != -1; next_child = GetSiblingIndex(child)) { child = next_child; } return child; }
INT32 GenericTreeModel::GetChildCount(INT32 index) { INT32 count = 0; for (INT32 child = GetChildIndex(index); child != -1; child = GetSiblingIndex(child)) { count++; } return count; }
INT32 GenericTreeModel::GetNextLeafIndex(INT32 index) { if (GetSubtreeSize(index) == 0) { // this is a leaf itself. Go up while (GetSiblingIndex(index) == -1 && index != -1) index = GetParentIndex(index); if (index == -1) return -1; index = GetSiblingIndex(index); } // Go down to the leaf while (GetChildIndex(index) != -1) index = GetChildIndex(index); return index; }
void GenericTreeModel::Remove(INT32 index, BOOL all_children, BOOL delete_item) { INT32 subtreesize = all_children ? GetSubtreeSize(index) : 0; BOOL broadcast = SetChangeType(TREE_CHANGED); GenericTreeModelItem* parent = GetGenericParentItem(index); if (broadcast) { BroadcastSubtreeRemoving(parent ? parent->GetIndex() : -1, index, subtreesize); } if (!all_children) { INT32 child = GetChildIndex(index); while (child != -1) { INT32 sibling = GetSiblingIndex(child); GetGenericItemByIndex(child)->SetGenericParentItem(parent); child = sibling; } } INT32 count = subtreesize + 1; Removing(index, count); UpdateSubtreeSizes(parent, -count); OpTreeModelItem* data; for(UINT32 i = index, count_item = index + count; i < count_item; i++) m_id_to_item.Remove(m_items.Get(i)->GetID(), &data); if (delete_item) { m_items.Delete(index, count); } else { m_items.Remove(index, count); } if (broadcast) { BroadcastSubtreeRemoved(parent ? parent->GetIndex() : -1, index, subtreesize); } }
void DependencyLabelerParts::BuildSiblingIndices( const std::vector<int> &heads) { DeleteSiblingIndices(); index_siblings_.assign(heads.size(), std::vector<std::vector<int> >(0)); for (int h = 0; h < heads.size(); ++h) { index_siblings_[h].assign(siblings_[h].size() + 1, std::vector<int>(0)); } int offset, num_siblings; GetOffsetSibling(&offset, &num_siblings); for (int r = 0; r < num_siblings; ++r) { Part *part = (*this)[offset + r]; CHECK(part->type() == DEPENDENCYLABELERPART_SIBLING); int h = static_cast<DependencyLabelerPartSibling*>(part)->head(); int m = static_cast<DependencyLabelerPartSibling*>(part)->modifier(); int i = GetSiblingIndex(h, m); index_siblings_[h][i].push_back(offset + r); } }
void GenericTreeModel::SortChildren(INT32 parent_index, INT32* sort_array, INT32* temp_array, GenericTreeModelItem** result_array, INT32& current_sort_array_pos, INT32& current_result_array_pos) { INT32 child = GetChildIndex(parent_index); if (child == -1) { return; } // fill sort array INT32 sort_array_pos = current_sort_array_pos; INT32 count = 0; while (child != -1) { sort_array[current_sort_array_pos] = child; current_sort_array_pos++; count++; child = GetSiblingIndex(child); } // sort the array m_comparison_counter = 0; //QuickSort(sort_array + sort_array_pos, count); //HeapSort(sort_array + sort_array_pos, count); MergeSort(sort_array + sort_array_pos, temp_array, 0, count - 1); // append result to result array while recursivly sorting grandchildren for (INT32 i = 0; i < count; i++) { INT32 result_index = sort_array[sort_array_pos + i]; GenericTreeModelItem* result_item = GetGenericItemByIndex(result_index); result_array[current_result_array_pos] = result_item; current_result_array_pos++; SortChildren(result_index, sort_array, temp_array, result_array, current_sort_array_pos, current_result_array_pos); } }