View Javadoc

1   package net.obsearch.index.bucket.impl;
2   
3   import java.nio.ByteBuffer;
4   import java.util.Arrays;
5   import net.obsearch.index.bucket.BucketObject;
6   import net.obsearch.exception.OBException;
7   import net.obsearch.ob.OBInt;
8   
9   /*
10   OBSearch: a distributed similarity search engine This project is to
11   similarity search what 'bit-torrent' is to downloads. 
12   Copyright (C) 2008 Arnoldo Jose Muller Molina
13  
14   This program is free software: you can redistribute it and/or modify
15   it under the terms of the GNU General Public License as published by
16   the Free Software Foundation, either version 3 of the License, or
17   (at your option) any later version.
18  
19   This program is distributed in the hope that it will be useful,
20   but WITHOUT ANY WARRANTY; without even the implied warranty of
21   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22   GNU General Public License for more details.
23  
24   You should have received a copy of the GNU General Public License
25   along with this program.  If not, see <http://www.gnu.org/licenses/>.
26   */
27  
28  /**
29   * BucketObjectInt extends {@link BucketObject} by adding an SMAP vector used to minimize
30   * the number of distance computations required per object.
31   * @author Arnoldo Jose Muller Molina
32   */
33  //*************************************************************************
34  //****** Warning: this is a generated file ********************************
35  //****** The source file is: BucketObject.java    
36  //*************************************************************************
37  		public class BucketObjectInt<O extends OBInt>
38          extends BucketObject<O> implements Comparable<BucketObjectInt>{
39  			
40  			
41      /**
42       * SMAP vector of the object.
43       */
44      private int[] smapVector;
45      
46      public BucketObjectInt(){
47      	super(-1);
48      	smapVector = null;
49      }
50  
51      /**
52       * Creates a new bucket int with
53       * @param bucket
54       *                Bucket number.
55       * @param level
56       *                Level within the hash table.
57       * @param smapVector
58       *                The distances of the corresponding object and the pivots
59       *                for the given level.
60       * @param exclusionBucket
61       *                If true, the corresponding object is in the exclusion
62       *                zone.
63       * @param id Optional id of the given object. Not always used.
64       */
65      public BucketObjectInt(int[] smapVector,
66               long id) {
67          this(smapVector,id,null);
68      }
69  
70  		/**
71       * Creates a new bucket int with
72       * @param bucket
73       *                Bucket number.
74       * @param level
75       *                Level within the hash table.
76       * @param smapVector
77       *                The distances of the corresponding object and the pivots
78       *                for the given level.
79       * @param exclusionBucket
80       *                If true, the corresponding object is in the exclusion
81       *                zone.
82       * @param id Optional id of the given object. Not always used.
83       */
84      public BucketObjectInt(int[] smapVector,
85  															 long id, O object) {
86          super(id, object);
87  				this.smapVector = smapVector;
88      }
89  
90  		public boolean equals(Object other){
91  				BucketObjectInt<O> another = (BucketObjectInt<O>) other;
92          boolean pivotVectorsEqual = comparePivotVectors(smapVector, another.smapVector);
93          boolean idsEqual = getId() == another.getId();
94          boolean objectsEqual = getObject().equals(another.getObject());
95  				return pivotVectorsEqual && idsEqual  &&  objectsEqual;
96  	}
97  
98      private boolean comparePivotVectors(int[] a, int[] b){
99  				int la = 0;
100 				int lb = 0;
101 				if(a != null){
102 						la = a.length;
103 				}
104 				if(b != null){
105 						lb = b.length;
106 				}
107 				if(la == 0 && lb == 0){
108 						return true;
109 				}
110 
111 				return  Arrays.equals(a,b);
112 		}
113 
114     
115     /**
116      * Execute l-inf between this object and b.
117      * @param b
118      * @return l-inf
119      */
120     public int lInf(BucketObjectInt b){
121         return lInf(smapVector, b.getSmapVector());
122     }
123 
124 		private int pivotCount(){
125 				if(smapVector == null){
126 						return 0;
127 				}else{
128 						return smapVector.length;
129 				}
130 		}
131 
132 
133 		public static int lInf(int[] smapVector, int[] other){
134         int cx = 0;
135         int max = 0;
136         int t;
137 				
138 				if(smapVector == null || smapVector.length == 0){
139 						assert other == null || other.length == 0;
140 						return max;
141 				}
142         
143         while (cx < smapVector.length) {
144             t = (int) Math.abs(smapVector[cx] - other[cx]);
145             if (t > max) {
146                 max = t;               
147             }
148             cx++;
149         }
150         return max;
151     }
152     
153 
154 		public static int[] convertTuple(OBInt object, OBInt[] pivots) throws OBException{
155 				int i = 0;
156 				int[] smapVector = new int[pivots.length];
157 				while (i < pivots.length) {
158 						int distance = pivots[i].distance(object);
159 						smapVector[i] = distance;
160 						i++;
161 				}
162 				return smapVector;
163 		}
164    
165 
166     /**
167      * @return the smapVector
168      */
169     public int[] getSmapVector() {
170         return smapVector;
171     }
172     
173     /**
174      * Writes itself into the given data buffer.
175      * @param data The ByteBuffer that will be modified.
176      */
177     public void write(ByteBuffer out){
178 				if(getSmapVector() != null){
179 						for (int j : getSmapVector()) {
180 								out.putInt(j);
181 						}
182 				}
183         out.putLong(getId());
184     }
185     
186     /**
187      * Reads the given # of pivots from the given bytebuffer.
188      * @param out
189      * @param pivots
190      */
191     public void read(ByteBuffer in, int pivots){
192 				if(pivots != 0){
193 						this.smapVector = new int[pivots];
194 				int i = 0;
195 				while(i < pivots){
196 						smapVector[i] = in.getInt();
197 						i++;
198 				}        
199 		}
200 		    smapVector = null;
201         super.setId(in.getLong());
202     }
203     
204     
205     /**
206      * @param smapVector
207      *                the smapVector to set
208      */
209     public void setSmapVector(int[] smapVector) {
210         this.smapVector = smapVector;
211     }
212     
213     public int getPivotSize(){
214 				if(smapVector == null){
215 						return 0;
216 				}else{
217 						return smapVector.length;
218 				}
219     }
220 
221     /* (non-Javadoc)
222      * @see java.lang.Comparable#compareTo(java.lang.Object)
223      */
224     @Override
225     public int compareTo(BucketObjectInt o) {
226         int i = 0;
227         assert smapVector.length == o.smapVector.length;
228         while(i < smapVector.length){
229             int res = compareDim(smapVector[i], o.smapVector[i]);
230             if(res != 0){
231                 return res;
232             }
233             i++;
234         }
235         // if we finish the loop both objects are equal.
236         return 0;
237     }
238     
239     /*public int compareTo(BucketObjectInt o) {
240         if(smapVector[0] < o.smapVector[0]){
241             return -1;
242         }else if (smapVector[0] > o.smapVector[0]){
243             return 1;
244         }else{
245             return 0;
246         }
247     }*/
248     
249     /**
250      * Compare one dimension.
251      * @param a one vector value.
252      * @param b another vector value.
253      * @return -1 if a < b, 0 if a == b otherwise 1.
254      */
255     private final int compareDim(int a, int b){
256         if( a < b){
257             return -1;
258         }else if( a > b){
259             return 1;
260         }else{
261             return 0;
262         }
263     }
264     
265     public String toString(){
266         return Arrays.toString(smapVector);
267     }
268 //*************************************************************************
269 //****** Warning: this is a generated file ********************************
270 //****** The source file is: BucketObject.java    
271 //*************************************************************************
272 }
273