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

« back to all changes in this revision

Viewing changes to src/de/lmu/ifi/dbs/elki/application/cache/CacheDoubleDistanceKNNLists.java

  • Committer: Package Import Robot
  • Author(s): Erich Schubert
  • Date: 2014-01-22 16:23:20 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20140122162320-dtqtgcdiki8t9unc
Tags: 0.6.0-1
* New upstream final.
* 3DPC extension is not included, but may be uploaded as a separate
  package when there is actual need (it is a demo software, not meant
  for use outside of research, so just get the source code!)
* Upgrade to policy 3.9.5.0 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
import de.lmu.ifi.dbs.elki.persistent.ByteArrayUtil;
49
49
import de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException;
50
50
import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID;
51
 
import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.GreaterEqualConstraint;
 
51
import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.CommonConstraints;
52
52
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.Parameterization;
53
53
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.FileParameter;
54
54
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.IntParameter;
135
135
 
136
136
      FiniteProgress prog = LOG.isVerbose() ? new FiniteProgress("Computing kNN", relation.size(), LOG) : null;
137
137
 
138
 
      for (DBIDIter it = relation.iterDBIDs(); it.valid(); it.advance()) {
 
138
      for(DBIDIter it = relation.iterDBIDs(); it.valid(); it.advance()) {
139
139
        final KNNList<D> nn = knnQ.getKNNForDBID(it, k);
140
140
        final int nnsize = nn.size();
141
141
 
142
142
        // Grow the buffer when needed:
143
 
        if (nnsize * 12 + 10 > bufsize) {
144
 
          while (nnsize * 12 + 10 > bufsize) {
 
143
        if(nnsize * 12 + 10 > bufsize) {
 
144
          while(nnsize * 12 + 10 > bufsize) {
145
145
            bufsize <<= 1;
146
146
          }
147
147
          buffer = ByteBuffer.allocateDirect(bufsize);
151
151
        ByteArrayUtil.writeUnsignedVarint(buffer, it.internalGetIndex());
152
152
        ByteArrayUtil.writeUnsignedVarint(buffer, nnsize);
153
153
        int c = 0;
154
 
        if (nn instanceof DoubleDistanceDBIDList) {
155
 
          for (DoubleDistanceDBIDListIter ni = ((DoubleDistanceDBIDList) nn).iter(); ni.valid(); ni.advance(), c++) {
 
154
        if(nn instanceof DoubleDistanceDBIDList) {
 
155
          for(DoubleDistanceDBIDListIter ni = ((DoubleDistanceDBIDList) nn).iter(); ni.valid(); ni.advance(), c++) {
156
156
            ByteArrayUtil.writeUnsignedVarint(buffer, ni.internalGetIndex());
157
157
            buffer.putDouble(ni.doubleDistance());
158
158
          }
159
 
        } else {
160
 
          for (DistanceDBIDListIter<D> ni = nn.iter(); ni.valid(); ni.advance(), c++) {
 
159
        }
 
160
        else {
 
161
          for(DistanceDBIDListIter<D> ni = nn.iter(); ni.valid(); ni.advance(), c++) {
161
162
            ByteArrayUtil.writeUnsignedVarint(buffer, ni.internalGetIndex());
162
163
            buffer.putDouble(ni.getDistance().doubleValue());
163
164
          }
164
165
        }
165
 
        if (c != nn.size()) {
 
166
        if(c != nn.size()) {
166
167
          throw new AbortException("Sizes did not agree. Cache is invalid.");
167
168
        }
168
169
 
169
170
        buffer.flip();
170
171
        channel.write(buffer);
171
 
        if (prog != null) {
 
172
        if(prog != null) {
172
173
          prog.incrementProcessed(LOG);
173
174
        }
174
175
      }
175
 
      if (prog != null) {
 
176
      if(prog != null) {
176
177
        prog.ensureCompleted(LOG);
177
178
      }
178
179
      lock.release();
179
 
    } catch (IOException e) {
 
180
    }
 
181
    catch(IOException e) {
180
182
      LOG.exception(e);
181
183
    }
182
184
    // FIXME: close!
240
242
      input = config.tryInstantiate(InputStep.class);
241
243
      // Distance function parameter
242
244
      final ObjectParameter<DistanceFunction<O, D>> dpar = new ObjectParameter<>(DISTANCE_ID, DistanceFunction.class);
243
 
      if (config.grab(dpar)) {
 
245
      if(config.grab(dpar)) {
244
246
        distance = dpar.instantiateClass(config);
245
247
      }
246
248
      final IntParameter kpar = new IntParameter(K_ID);
247
 
      kpar.addConstraint(new GreaterEqualConstraint(1));
248
 
      if (config.grab(kpar)) {
 
249
      kpar.addConstraint(CommonConstraints.GREATER_EQUAL_ONE_INT);
 
250
      if(config.grab(kpar)) {
249
251
        k = kpar.intValue();
250
252
      }
251
253
      // Output file parameter
252
254
      final FileParameter cpar = new FileParameter(CACHE_ID, FileParameter.FileType.OUTPUT_FILE);
253
 
      if (config.grab(cpar)) {
 
255
      if(config.grab(cpar)) {
254
256
        out = cpar.getValue();
255
257
      }
256
258
    }