public IMaxTree <ElementType> BuildMaxTree(ElementType[] element_array, IComparer <ElementType> element_value_comparer, ITopologyElement topology, int real_element_count, IProgressReporter reporter) { this.d_element_array = element_array; this.element_value_comparer = element_value_comparer; this.element_index_comparer = new ComparerArrayIndex <ElementType>(element_value_comparer, element_array); this.d_topology = topology; this.d_elements_to_processed = new bool[d_element_array.Length]; this.d_neigbor_element_array = new int[d_topology.MaximumConnectivity]; this.d_fringe = new Stack <OrderedBag <int> >(); this.d_component_stack = new Stack <Tuple <ElementType, IList <int>, IList <MaxTreeNode <ElementType> > > >(); this.d_bottom_level_node = null; process(0); // build tree while (d_fringe.Count != 0) { if (d_fringe.Peek().Count == 0) { decrease_process_level(); // continue on a lower level } else { // otherwise keep on processing the fringe process(d_fringe.Peek().RemoveFirst()); } } return(new MaxTree <ElementType>(d_bottom_level_node, d_element_array.Length, real_element_count, element_value_comparer)); }
public int [] GetElementIndexArrayCulmativeFull() { int [] elements = new int [CulmativeFullSize]; int element_index = 0; // Depth first for low memory Stack <Tuple <MaxTreeNode <ElementType>, int> > node_stack = new Stack <Tuple <MaxTreeNode <ElementType>, int> >(); node_stack.Push(new Tuple <MaxTreeNode <ElementType>, int>(this, 0)); while (node_stack.Count != 0) { Tuple <MaxTreeNode <ElementType>, int> node_index_pair = node_stack.Pop(); MaxTreeNode <ElementType> node = node_index_pair.Item1; int node_index = node_index_pair.Item2; if (node_index < node.children.Count) { node_stack.Push(new Tuple <MaxTreeNode <ElementType>, int>(node, node_index + 1)); node_stack.Push(new Tuple <MaxTreeNode <ElementType>, int>(node.children[node_index], 0)); } else { node.GetElementIndexArrayCulmativeRealRBA(elements, element_index); element_index = element_index + node.NodeElementIndexes.Count; } } return(elements); }
public IMaxTree <ElementType> BuildMaxTree(ElementType[] element_array, IComparer <ElementType> element_value_comparer, ITopologyElement topology, int real_element_count, IProgressReporter reporter) { this.d_element_array = element_array; this.element_value_comparer = element_value_comparer; this.element_index_comparer = new ComparerArrayIndex <ElementType>(element_value_comparer, element_array); this.d_topology = topology; this.d_reporter = reporter; this.d_done = 0; this.d_elements_to_queued = new bool[d_element_array.Length]; this.d_neigbor_element_array = new int[d_topology.MaximumConnectivity]; this.d_fringe = new PriorityQueueC5 <int>(element_index_comparer); this.d_component_stack = new Stack <Tuple <ElementType, IList <int>, IList <MaxTreeNode <ElementType> > > >(); d_fringe.Enqueue(0); d_elements_to_queued[0] = true; // Build tree while (d_fringe.Count != 0) { Process(d_fringe.PeekLast()); } Tuple <ElementType, IList <int>, IList <MaxTreeNode <ElementType> > > component = d_component_stack.Pop(); MaxTreeNode <ElementType> bottom_level_node = new MaxTreeNode <ElementType>(component.Item1, component.Item2, component.Item3, element_value_comparer); return(new MaxTree <ElementType>(bottom_level_node, d_element_array.Length, real_element_count, element_value_comparer)); }
private void decrease_process_level() { OrderedBag <int> fringe_top = d_fringe.Pop(); MaxTreeNode <ElementType> popped_node = pop_component_stack(); if (fringe_top.Count == 0) // if the popped fringe is empty { if (d_component_stack.Count == 0) { d_bottom_level_node = popped_node; // we are done. } else { // and add the new node to there d_component_stack.Peek().Item3.Add(popped_node); } } else // if the popped fringe is not empty { if (d_component_stack.Count == 0) { // find where tocontinue ElementType next_process_level = d_element_array[fringe_top.GetFirst()]; d_fringe.Push(fringe_top); // push the fringe back in d_component_stack.Push(new Tuple <ElementType, IList <int>, IList <MaxTreeNode <ElementType> > >(next_process_level, new List <int>(), new List <MaxTreeNode <ElementType> >())); // add the popped node as child d_component_stack.Peek().Item3.Add(popped_node); } else { ElementType fringe_level = d_element_array[fringe_top.GetFirst()]; ElementType component_stack_level = d_component_stack.Peek().Item1; if (element_value_comparer.Compare(component_stack_level, fringe_level) == -1) //TODO { d_fringe.Push(fringe_top); // push the fringe back in d_component_stack.Push(new Tuple <ElementType, IList <int>, IList <MaxTreeNode <ElementType> > >(fringe_level, new List <int>(), new List <MaxTreeNode <ElementType> >())); // add the popped node as child d_component_stack.Peek().Item3.Add(popped_node); } else { while (0 < fringe_top.Count) { d_fringe.Peek().Add(fringe_top.RemoveFirst()); } d_component_stack.Peek().Item3.Add(popped_node); } } } }
public List <MaxTreeNode <ElementType> > GetCulmativeChildrenBottomToTopInner() { List <MaxTreeNode <ElementType> > node_list_bottom_to_top = new List <MaxTreeNode <ElementType> >(); // Bread first, list should be bottom to top Queue <MaxTreeNode <ElementType> > queue = new Queue <MaxTreeNode <ElementType> >(); queue.Enqueue(this); while (queue.Count != 0) { MaxTreeNode <ElementType> node = queue.Dequeue(); node_list_bottom_to_top.Add(node); foreach (MaxTreeNode <ElementType> child in node.children) { queue.Enqueue(child); } } return(node_list_bottom_to_top); }
public void ComputePrimitives( IMaxTree <ElementType> max_tree) { int real_image_size = max_tree.RealElementCount; // Depth first for low memory and Stack <Tuple <MaxTreeNode <ElementType>, int> > node_stack = new Stack <Tuple <MaxTreeNode <ElementType>, int> >(); node_stack.Push(new Tuple <MaxTreeNode <ElementType>, int>(this, 0)); while (node_stack.Count != 0) { Tuple <MaxTreeNode <ElementType>, int> node_index_pair = node_stack.Pop(); MaxTreeNode <ElementType> node = node_index_pair.Item1; int index = node_index_pair.Item2; if (index < node.children.Count) { node_stack.Push(new Tuple <MaxTreeNode <ElementType>, int>(node, index + 1)); node_stack.Push(new Tuple <MaxTreeNode <ElementType>, int>(node.children[index], 0)); } else { node.MaxTree = max_tree; node.PeakComponentValue = node.Value; node.CulmativeFullSize = 0; foreach (int element in node.NodeElementIndexes) { node.CulmativeFullSize++; if (element < real_image_size) { node.CulmativeRealSize++; } } foreach (MaxTreeNode <ElementType> child in node.children) { if (element_value_comparer.Compare(PeakComponentValue, child.PeakComponentValue) == -1) { node.PeakComponentValue = child.PeakComponentValue; } node.CulmativeFullSize += child.CulmativeFullSize; node.CulmativeRealSize += child.CulmativeRealSize; } } } }
private void DecreaseProcessLevel(int element_index) { //Pop the top of the stack Tuple <ElementType, IList <int>, IList <MaxTreeNode <ElementType> > > component = d_component_stack.Pop(); MaxTreeNode <ElementType> popped_node = new MaxTreeNode <ElementType>(component.Item1, component.Item2, component.Item3, element_value_comparer); if (d_component_stack.Count == 0) { d_component_stack.Push(new Tuple <ElementType, IList <int>, IList <MaxTreeNode <ElementType> > >(d_element_array[element_index], new List <int>(), new List <MaxTreeNode <ElementType> >())); } else { if (!d_component_stack.Peek().Item1.Equals(d_element_array[element_index])) { d_component_stack.Push(new Tuple <ElementType, IList <int>, IList <MaxTreeNode <ElementType> > >(d_element_array[element_index], new List <int>(), new List <MaxTreeNode <ElementType> >())); } } // And add the new tree element node to there d_component_stack.Peek().Item3.Add(popped_node); }
} //TODO remove //TODO make array of nodes bottom to upper at sequence of indexing, this will make iteration over them up or down simpler!!! public MaxTree(MaxTreeNode <ElementType> bottom_level_node, int full_element_count, int real_element_count, IComparer <ElementType> element_value_comparer) { this.element_value_comparer = element_value_comparer; this.FullElementCount = full_element_count; // includes virtual elements this.RealElementCount = real_element_count; this.d_elements_to_nodes = new MaxTreeNode <ElementType> [full_element_count]; foreach (int element in bottom_level_node.GetElementIndexArrayNodeFull()) //!Note unused element in alfa trees { this.d_elements_to_nodes[element] = bottom_level_node; } bottom_level_node.ComputePrimitives(this); this.node_list_bottom_to_top = bottom_level_node.GetCulmativeChildrenBottomToTopInner(); for (int node_index = 0; node_index < node_list_bottom_to_top.Count; node_index++) { MaxTreeNode <ElementType> node = node_list_bottom_to_top[node_index]; node.NodeIndex = node_index; foreach (int element_index in node.GetElementIndexArrayNodeFull()) { this.d_elements_to_nodes[element_index] = node; } } }