1 package net.obsearch.pivots.bustos.impl;
2
3 import java.util.Arrays;
4 import java.util.HashMap;
5
6 import hep.aida.bin.StaticBin1D;
7
8 import net.obsearch.Index;
9 import net.obsearch.dimension.DimensionInt;
10 import net.obsearch.exception.IllegalIdException;
11 import net.obsearch.exception.OBException;
12 import net.obsearch.pivots.Pivotable;
13 import net.obsearch.pivots.bustos.AbstractIncrementalBustosNavarroChavez;
14 import net.obsearch.pivots.IncrementalPivotSelector;
15
16 import net.obsearch.ob.OBInt;
17
18 import com.sleepycat.je.DatabaseException;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 /
45
46
47
48
49
50
51
52
53
54 private transient HashMap<Long, int[]> smapCache;
55
56
57
58
59
60
61
62
63
64 public IncrementalBustosNavarroChavezInt(Pivotable < O > pivotable,
65 int l, int m) {
66 super(pivotable, l , m);
67 smapCache = new HashMap<Long, int[]>(l);
68 }
69
70 @Override
71 protected double calculateMedian(long[] pivots, long[] x, long[] y, Index<O> index) throws DatabaseException,
72 IllegalIdException, IllegalAccessException, InstantiationException,
73 OBException{
74 StaticBin1D data = new StaticBin1D();
75 int i = 0;
76 while ( i < x.length){
77 int[] tupleA = getTuple(pivots, x[i], (Index<OBInt>)index);
78 int[] tupleB = getTuple(pivots, y[i], (Index<OBInt>)index);
79 int distance = DimensionInt.lInfinite(tupleA, tupleB);
80 data.add(distance);
81 i++;
82 }
83 return data.mean();
84 }
85
86 protected void resetCache(int x){
87 smapCache = new HashMap<Long, int[]>(x);
88 }
89
90 private int[] getTuple(long[] pivots, long id, Index<OBInt>index )throws DatabaseException,
91 IllegalIdException, IllegalAccessException, InstantiationException,
92 OBException{
93 int[] t = smapCache.get(id);
94 if(t == null){
95 t = new int[pivots.length];
96 smapCache.put(id, t);
97 assert pivots.length == 1;
98 }else{
99 int [] td = new int[pivots.length];
100 System.arraycopy(t, 0, td, 0, t.length );
101 smapCache.put(id, td);
102 t = td;
103 }
104 int i = pivots.length-1;
105 t[i] = index.getObject(id).distance(index.getObject(pivots[i]));
106 return t;
107 }
108
109
110
111
112 @Override
113 protected boolean validatePivots(long[] pivots, long id, Index<O> index) throws DatabaseException,
114 IllegalIdException, IllegalAccessException, InstantiationException,
115 OBException {
116 int[] real = DimensionInt.getPrimitiveTuple(pivots, id, index);
117 int[] localTuple = smapCache.get(id);
118 return Arrays.equals(real, localTuple);
119 }
120
121
122
123 }
124