From 86f6122c4537264ac1a6203aaf589f08d725e648 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Sun, 7 Aug 2016 16:13:43 +0100 Subject: [PATCH] JAL-2154 add in missing class for createDatasetAlignment(), and add support for recovering position for an object in the set. --- src/jalview/datamodel/Alignment.java | 2 +- src/jalview/util/LinkedIdentityHashSet.java | 109 +++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 src/jalview/util/LinkedIdentityHashSet.java diff --git a/src/jalview/datamodel/Alignment.java b/src/jalview/datamodel/Alignment.java index e9c0b4c..dcfbaad 100755 --- a/src/jalview/datamodel/Alignment.java +++ b/src/jalview/datamodel/Alignment.java @@ -1095,7 +1095,7 @@ public class Alignment implements AlignmentI { return; } - // try to avoid using equals at this stage, it will be expensive + // try to avoid using SequenceI.equals at this stage, it will be expensive Set seqs = new jalview.util.LinkedIdentityHashSet(); for (int i = 0; i < getHeight(); i++) diff --git a/src/jalview/util/LinkedIdentityHashSet.java b/src/jalview/util/LinkedIdentityHashSet.java new file mode 100644 index 0000000..5cdbeb1 --- /dev/null +++ b/src/jalview/util/LinkedIdentityHashSet.java @@ -0,0 +1,109 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ +package jalview.util; + +import java.util.AbstractSet; +import java.util.Iterator; +import java.util.LinkedHashMap; + +/** + * Order preserving Set based on System.identityHashCode() for an object, which + * also supports Object->index lookup. + * + * @author Jim Procter (2016) based on Evgeniy Dorofeev's response: via + * https://stackoverflow.com/questions/17276658/linkedidentityhashset + * + */ +public class LinkedIdentityHashSet extends AbstractSet +{ + LinkedHashMap set = new LinkedHashMap(); + + static class IdentityWrapper + { + Object obj; + + public int p; + + IdentityWrapper(Object obj, int p) + { + this.obj = obj; + this.p = p; + } + + @Override + public boolean equals(Object obj) + { + return this.obj == obj; + } + + @Override + public int hashCode() + { + return System.identityHashCode(obj); + } + } + + @Override + public boolean add(E e) + { + IdentityWrapper el = (new IdentityWrapper(e, set.size())); + return set.putIfAbsent(el, el) == null; + } + + @Override + public Iterator iterator() + { + return new Iterator() + { + final Iterator se = set.keySet().iterator(); + + @Override + public boolean hasNext() + { + return se.hasNext(); + } + + @SuppressWarnings("unchecked") + @Override + public E next() + { + return (E) se.next().obj; + } + }; + } + + @Override + public int size() + { + return set.size(); + } + + /** + * Lookup the index for e in the set + * + * @param e + * @return position of e in the set when it was added. + */ + public int indexOf(E e) + { + return set.get(e).p; + } +} -- 1.7.10.2