001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.feature.local.list;
031
032import gnu.trove.map.hash.TIntObjectHashMap;
033
034import java.io.DataInput;
035import java.io.DataOutput;
036import java.io.IOException;
037import java.util.ArrayList;
038import java.util.HashMap;
039import java.util.List;
040import java.util.Map.Entry;
041
042import org.openimaj.feature.local.LocalFeature;
043import org.openimaj.feature.local.quantised.QuantisedLocalFeature;
044import org.openimaj.io.ReadWriteable;
045import org.openimaj.io.ReadWriteableBinary;
046
047/**
048 * LocalFeatureListIndex is a @{link ReadWriteable} map of keys to local feature
049 * lists.
050 * 
051 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
052 * @param <K>
053 *            the key type
054 * @param <V>
055 *            the value type
056 * 
057 */
058public class LocalFeatureListIndex<K extends ReadWriteable, V extends LocalFeature<?, ?>>
059                extends
060                        HashMap<K, LocalFeatureList<V>> implements ReadWriteableBinary
061{
062        private static final long serialVersionUID = 1L;
063
064        /** The header used when writing LocalFeatureListIndex to streams and files */
065        public static final byte[] BINARY_HEADER = "LFLI".getBytes();
066
067        protected Class<K> keyClass;
068        protected Class<V> valueClass;
069
070        @SuppressWarnings("unchecked")
071        @Override
072        public void readBinary(DataInput in) throws IOException {
073                try {
074                        final String kclzstr = in.readUTF();
075                        final String vclzstr = in.readUTF();
076
077                        final Class<?> kclz = Class.forName(kclzstr);
078                        final Class<?> vclz = Class.forName(vclzstr);
079
080                        if (keyClass != null && !keyClass.equals(kclz)) {
081                                throw new IOException("type mismatch");
082                        }
083                        if (valueClass != null && !valueClass.equals(vclz)) {
084                                throw new IOException("type mismatch");
085                        }
086
087                        keyClass = (Class<K>) kclz;
088                        valueClass = (Class<V>) vclz;
089
090                        final int size = in.readInt();
091
092                        for (int i = 0; i < size; i++) {
093                                final K key = keyClass.newInstance();
094                                key.readBinary(in);
095
096                                final MemoryLocalFeatureList<V> value = MemoryLocalFeatureList.readNoHeader(in, valueClass);
097
098                                this.put(key, value);
099                        }
100                } catch (final Exception e) {
101                        throw new IOException(e);
102                }
103        }
104
105        @Override
106        public byte[] binaryHeader() {
107                return BINARY_HEADER;
108        }
109
110        @SuppressWarnings("unchecked")
111        @Override
112        public void writeBinary(DataOutput out) throws IOException {
113                if (keyClass == null) {
114                        if (!this.keySet().iterator().hasNext()) {
115                                throw new IOException("unable to guess type");
116                        }
117                        keyClass = (Class<K>) this.keySet().iterator().next().getClass();
118                }
119
120                if (valueClass == null) {
121                        if (!this.values().iterator().hasNext()) {
122                                throw new IOException("unable to guess type");
123                        }
124                        if (!this.values().iterator().next().iterator().hasNext()) {
125                                throw new IOException("unable to guess type");
126                        }
127                        valueClass = (Class<V>) this.values().iterator().next().iterator().next().getClass();
128                }
129
130                out.writeUTF(keyClass.getCanonicalName());
131                out.writeUTF(valueClass.getCanonicalName());
132                out.writeInt(this.size());
133
134                for (final Entry<K, LocalFeatureList<V>> e : this.entrySet()) {
135                        e.getKey().writeBinary(out);
136                        e.getValue().writeBinary(out);
137                }
138        }
139
140        /**
141         * <p>
142         * Invert an index of quantised features. The inversion process swaps keys
143         * and feature {@link QuantisedLocalFeature#id}s around so that the inverted
144         * index is a hash of ids to {@link QuantisedLocalFeature}s with the
145         * {@link Object#hashCode()} of the key stored in the
146         * {@link QuantisedLocalFeature#id} field.
147         * </p>
148         * <p>
149         * The original index is not affected by the inversion operation.
150         * </p>
151         * 
152         * @param <T>
153         *            the type of local feature.
154         * @param <K>
155         *            the type of key.
156         * @param index
157         *            the index to invert.
158         * @return an inverted-index data structure.
159         */
160        public static <K extends ReadWriteable, T extends QuantisedLocalFeature<?>>
161                        TIntObjectHashMap<TIntObjectHashMap<List<T>>>
162                        invert(LocalFeatureListIndex<K, T> index)
163        {
164                final TIntObjectHashMap<TIntObjectHashMap<List<T>>> invertedIndex = new TIntObjectHashMap<TIntObjectHashMap<List<T>>>();
165
166                for (final Entry<K, LocalFeatureList<T>> e : index.entrySet()) {
167                        final K docid = e.getKey();
168
169                        for (final T t : e.getValue()) {
170                                final int termid = t.id;
171
172                                if (!invertedIndex.containsKey(termid))
173                                        invertedIndex.put(termid, new TIntObjectHashMap<List<T>>());
174                                final TIntObjectHashMap<List<T>> postings = invertedIndex.get(termid);
175                                if (!postings.containsKey(docid.hashCode()))
176                                        postings.put(docid.hashCode(), new ArrayList<T>());
177                                postings.get(docid.hashCode()).add(t);
178                        }
179                }
180
181                return invertedIndex;
182        }
183}