~ubuntu-branches/ubuntu/vivid/elki/vivid

« back to all changes in this revision

Viewing changes to src/de/lmu/ifi/dbs/elki/distance/distanceresultlist/DoubleDistanceDBIDList.java

  • Committer: Package Import Robot
  • Author(s): Erich Schubert
  • Date: 2012-12-14 20:45:15 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20121214204515-4m0d0er9ivtu5w9d
Tags: 0.5.5-1
New upstream release: 0.5.5 interim release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package de.lmu.ifi.dbs.elki.distance.distanceresultlist;
 
2
 
 
3
/*
 
4
 This file is part of ELKI:
 
5
 Environment for Developing KDD-Applications Supported by Index-Structures
 
6
 
 
7
 Copyright (C) 2012
 
8
 Ludwig-Maximilians-Universität München
 
9
 Lehr- und Forschungseinheit für Datenbanksysteme
 
10
 ELKI Development Team
 
11
 
 
12
 This program is free software: you can redistribute it and/or modify
 
13
 it under the terms of the GNU Affero General Public License as published by
 
14
 the Free Software Foundation, either version 3 of the License, or
 
15
 (at your option) any later version.
 
16
 
 
17
 This program is distributed in the hope that it will be useful,
 
18
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
 GNU Affero General Public License for more details.
 
21
 
 
22
 You should have received a copy of the GNU Affero General Public License
 
23
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
24
 */
 
25
 
 
26
import java.util.ArrayList;
 
27
import java.util.Collections;
 
28
 
 
29
import de.lmu.ifi.dbs.elki.database.ids.DBIDFactory;
 
30
import de.lmu.ifi.dbs.elki.database.ids.DBIDIter;
 
31
import de.lmu.ifi.dbs.elki.database.ids.DBIDRef;
 
32
import de.lmu.ifi.dbs.elki.database.ids.DBIDUtil;
 
33
import de.lmu.ifi.dbs.elki.database.ids.DoubleDistanceDBIDPair;
 
34
import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance;
 
35
 
 
36
/**
 
37
 * Default class to keep a list of distance-object pairs.
 
38
 * 
 
39
 * @author Erich Schubert
 
40
 * 
 
41
 * @apiviz.composedOf DoubleDistanceDBIDPair
 
42
 * @apiviz.has DoubleDistanceDBIDResultIter
 
43
 */
 
44
public class DoubleDistanceDBIDList implements ModifiableDistanceDBIDResult<DoubleDistance> {
 
45
  /**
 
46
   * Actual storage.
 
47
   */
 
48
  final ArrayList<DoubleDistanceDBIDPair> storage;
 
49
  
 
50
  /**
 
51
   * Constructor.
 
52
   */
 
53
  public DoubleDistanceDBIDList() {
 
54
    super();
 
55
    storage = new ArrayList<DoubleDistanceDBIDPair>();
 
56
  }
 
57
 
 
58
  /**
 
59
   * Constructor.
 
60
   * 
 
61
   * @param initialCapacity Capacity
 
62
   */
 
63
  public DoubleDistanceDBIDList(int initialCapacity) {
 
64
    super();
 
65
    storage = new ArrayList<DoubleDistanceDBIDPair>(initialCapacity);
 
66
  }
 
67
  
 
68
  /**
 
69
   * Add an element.
 
70
   * 
 
71
   * @deprecated Pass a double value instead.
 
72
   * 
 
73
   * @param dist Distance
 
74
   * @param id ID
 
75
   */
 
76
  @Override
 
77
  @Deprecated
 
78
  public void add(DoubleDistance dist, DBIDRef id) {
 
79
    storage.add(DBIDFactory.FACTORY.newDistancePair(dist.doubleValue(), id));
 
80
  }
 
81
 
 
82
  /**
 
83
   * Add an element.
 
84
   * 
 
85
   * @param dist Distance
 
86
   * @param id ID
 
87
   */
 
88
  public void add(double dist, DBIDRef id) {
 
89
    storage.add(DBIDFactory.FACTORY.newDistancePair(dist, id));
 
90
  }
 
91
 
 
92
  /**
 
93
   * Add an element.
 
94
   * 
 
95
   * @param pair Pair to add
 
96
   */
 
97
  public void add(DoubleDistanceDBIDPair pair) {
 
98
    storage.add(pair);
 
99
  }
 
100
 
 
101
  @Override
 
102
  public void sort() {
 
103
    Collections.sort(storage, DistanceDBIDResultUtil.distanceComparator());
 
104
  }
 
105
 
 
106
  @Override
 
107
  public int size() {
 
108
    return storage.size();
 
109
  }
 
110
 
 
111
  @Override
 
112
  public DoubleDistanceDBIDPair get(int off) {
 
113
    return storage.get(off);
 
114
  }
 
115
 
 
116
  @Override
 
117
  public DoubleDistanceDBIDResultIter iter() {
 
118
    return new Iter();
 
119
  }
 
120
 
 
121
  @Override
 
122
  public boolean contains(DBIDRef o) {
 
123
    for(DBIDIter iter = iter(); iter.valid(); iter.advance()) {
 
124
      if(DBIDUtil.equal(iter, o)) {
 
125
        return true;
 
126
      }
 
127
    }
 
128
    return false;
 
129
  }
 
130
 
 
131
  @Override
 
132
  public boolean isEmpty() {
 
133
    return size() == 0;
 
134
  }
 
135
 
 
136
  @Override
 
137
  public String toString() {
 
138
    return DistanceDBIDResultUtil.toString(this);
 
139
  }
 
140
  
 
141
  /**
 
142
   * Iterator class.
 
143
   * 
 
144
   * @author Erich Schubert
 
145
   * 
 
146
   * @apiviz.exclude
 
147
   */
 
148
  protected class Iter implements DoubleDistanceDBIDResultIter {
 
149
    /**
 
150
     * Iterator position.
 
151
     */
 
152
    int pos = 0;
 
153
 
 
154
    @Override
 
155
    public int internalGetIndex() {
 
156
      return get(pos).internalGetIndex();
 
157
    }
 
158
 
 
159
    @Override
 
160
    public boolean valid() {
 
161
      return pos < size();
 
162
    }
 
163
 
 
164
    @Override
 
165
    public void advance() {
 
166
      pos++;
 
167
    }
 
168
 
 
169
    @Override
 
170
    @Deprecated
 
171
    public DoubleDistance getDistance() {
 
172
      return get(pos).getDistance();
 
173
    }
 
174
 
 
175
    @Override
 
176
    public double doubleDistance() {
 
177
      return get(pos).doubleDistance();
 
178
    }
 
179
 
 
180
    @Override
 
181
    public DoubleDistanceDBIDPair getDistancePair() {
 
182
      return get(pos);
 
183
    }
 
184
    
 
185
    @Override
 
186
    public String toString() {
 
187
      return valid() ? getDistancePair().toString() : "null";
 
188
    }
 
189
  }
 
190
}
 
 
b'\\ No newline at end of file'