X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fphylogeny%2FPhylogeny.java;h=a4102d20377f63f79e92bc4233374664724d4273;hb=7ad84913635263ddc191fcbebd8a9bd46a9ac7e3;hp=b1677b1c513db326045aa9477c296d8b8ce5ccb5;hpb=e174d62534f7fc6f3de133d523a402a87735b27f;p=jalview.git diff --git a/forester/java/src/org/forester/phylogeny/Phylogeny.java b/forester/java/src/org/forester/phylogeny/Phylogeny.java index b1677b1..a4102d2 100644 --- a/forester/java/src/org/forester/phylogeny/Phylogeny.java +++ b/forester/java/src/org/forester/phylogeny/Phylogeny.java @@ -68,7 +68,7 @@ public class Phylogeny { private Confidence _confidence; private Identifier _identifier; private boolean _rerootable; - private HashMap _idhash; + private HashMap _id_to_node_map; private List _external_nodes_set; private Collection _sequenceRelationQueries; private Collection _relevant_sequence_relation_types; @@ -213,7 +213,7 @@ public class Phylogeny { } /** - * Need the delete and/or rehash _idhash (not done automatically + * Need to call clearHashIdToNodeMap() afterwards (not done automatically * to allow client multiple deletions in linear time). * Need to call 'recalculateNumberOfExternalDescendants(boolean)' after this * if tree is to be displayed. @@ -269,7 +269,6 @@ public class Phylogeny { } } remove_us.removeConnections(); - setIdHash( null ); externalNodesHaveChanged(); } @@ -376,11 +375,8 @@ public class Phylogeny { return _identifier; } - // --------------------------------------------------------- - // Modification of Phylogeny topology and Phylogeny appearance - // --------------------------------------------------------- - private HashMap getIdHash() { - return _idhash; + private HashMap getIdToNodeMap() { + return _id_to_node_map; } /** @@ -392,29 +388,16 @@ public class Phylogeny { /** * Finds the PhylogenyNode of this Phylogeny which has a matching ID number. - * Takes O(n) time. After method hashIDs() has been called it runs in - * constant time. - * - * @param id - * ID number (int) of the PhylogenyNode to find * @return PhylogenyNode with matching ID, null if not found */ public PhylogenyNode getNode( final int id ) throws NoSuchElementException { if ( isEmpty() ) { throw new NoSuchElementException( "attempt to get node in an empty phylogeny" ); } - if ( _idhash != null ) { - return _idhash.get( id ); - } - else { - for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) { - final PhylogenyNode node = iter.next(); - if ( node.getId() == id ) { - return node; - } - } + if ( ( getIdToNodeMap() == null ) || getIdToNodeMap().isEmpty() ) { + reHashIdToNodeMap(); } - return null; + return getIdToNodeMap().get( id ); } /** @@ -724,17 +707,21 @@ public class Phylogeny { * constant time. Important: The user is responsible for calling this method * (again) after this Phylogeny has been changed/created/renumbered. */ - public void hashIDs() { + private void reHashIdToNodeMap() { if ( isEmpty() ) { return; } - setIdHash( new HashMap() ); + setIdToNodeMap( new HashMap() ); for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) { final PhylogenyNode node = iter.next(); - getIdHash().put( node.getId(), node ); + getIdToNodeMap().put( node.getId(), node ); } } + public void clearHashIdToNodeMap() { + setIdToNodeMap( null ); + } + /** * Deletes this Phylogeny. */ @@ -745,7 +732,7 @@ public class Phylogeny { _description = ""; _type = ""; _distance_unit = ""; - _idhash = null; + _id_to_node_map = null; _confidence = null; _identifier = null; _rerootable = true; @@ -849,7 +836,7 @@ public class Phylogeny { if ( isEmpty() ) { return; } - _idhash = null; + _id_to_node_map = null; int max = 0; for( final PhylogenyNodeIterator it = iteratorPreorder(); it.hasNext(); ) { final PhylogenyNode node = it.next(); @@ -870,7 +857,7 @@ public class Phylogeny { if ( isEmpty() ) { return; } - setIdHash( null ); + setIdToNodeMap( null ); int i = PhylogenyNode.getNodeCount(); for( final PhylogenyNodeIterator it = iteratorPreorder(); it.hasNext(); ) { it.next().setId( i++ ); @@ -1165,8 +1152,8 @@ public class Phylogeny { _identifier = identifier; } - void setIdHash( final HashMap idhash ) { - _idhash = idhash; + private void setIdToNodeMap( final HashMap idhash ) { + _id_to_node_map = idhash; } /**