View Javadoc

1   package net.obsearch.index;
2   /*
3   		OBSearch: a distributed similarity search engine This project is to
4       similarity search what 'bit-torrent' is to downloads. 
5       Copyright (C) 2008 Arnoldo Jose Muller Molina
6   
7     	This program is free software: you can redistribute it and/or modify
8       it under the terms of the GNU General Public License as published by
9       the Free Software Foundation, either version 3 of the License, or
10      (at your option) any later version.
11  
12      This program is distributed in the hope that it will be useful,
13      but WITHOUT ANY WARRANTY; without even the implied warranty of
14      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15      GNU General Public License for more details.
16  
17      You should have received a copy of the GNU General Public License
18      along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20  import java.io.IOException;
21  import net.obsearch.constants.ByteConstants;
22  import net.obsearch.exception.OBException;
23  import net.obsearch.ob.OBFloat;
24  import java.nio.ByteBuffer;
25  import net.obsearch.utils.bytes.ByteBufferFactoryConversion;
26  import java.util.Random;
27  import java.util.Arrays;
28  /**
29   * L1 distance implementation for floats.
30   * @author Arnoldo Jose Muller Molina
31   *
32   */
33  //*************************************************************************
34  //****** Warning: this is a generated file ********************************
35  //****** The source file is: OBVector.java    
36  //*************************************************************************
37  public class OBVectorFloat implements OBFloat {
38  	
39  	private float[] data;
40  	
41  	/**
42       * Default constructor must be provided by every object that implements the
43       * interface OB.
44       */
45      public OBVectorFloat() {
46      	data = null;
47      }
48  	
49  	public OBVectorFloat(float[] data){
50  		this.data = data;
51  	}
52  
53  		/**
54  	 * Creates a new vector with dim dimensions created by Random.nextFloat()
55  	 */
56  	public OBVectorFloat(Random r, int dim){
57  			int i = 0;
58  			data = new float[dim];
59  			while(i < dim){
60  								data[i] = r.nextFloat();
61  
62  					i++;
63  			}
64  	}
65  
66  	public boolean equals(Object o){
67  			OBVectorFloat other = (	OBVectorFloat) o;
68  			return Arrays.equals(data, other.data);
69  	}
70  
71  
72  	@Override
73  	public float distance(OBFloat object) throws OBException {
74  		// TODO Auto-generated method stub
75  		OBVectorFloat o = (OBVectorFloat) object;
76  		assert data.length == o.data.length;
77  		float res = 0;
78  		int i = 0;
79  		while(i < data.length){
80  				assert (data[i] - o.data[i]) >= Float.NEGATIVE_INFINITY 
81   && (data[i] - o.data[i]) <= Float.MAX_VALUE : "a: " + data[i] + " b: "  + o.data[i] ;		 
82  
83  				
84  			res += Math.abs(data[i] - o.data[i]);
85  			i++;
86  		}
87  		return res;
88  	}
89  
90  //*************************************************************************
91  //****** Warning: this is a generated file ********************************
92  //****** The source file is: OBVector.java    
93  //*************************************************************************
94  	@Override
95  	public void load(byte[] input) throws OBException, IOException {
96  		ByteBuffer in = ByteBufferFactoryConversion.createByteBuffer(input);
97  		int size = in.getInt();
98  		
99  		data = new float[size];
100 		int i = 0;
101 		while(i < size){
102 			data[i] = in.getFloat();
103 			i++;
104 		}
105 	}
106 	
107 //*************************************************************************
108 //****** Warning: this is a generated file ********************************
109 //****** The source file is: OBVector.java    
110 //*************************************************************************
111 	@Override
112 	public byte[] store() throws OBException, IOException {
113 			ByteBuffer out = ByteBufferFactoryConversion.createByteBuffer(ByteConstants.Float.getSize() * data.length + ByteConstants.Int.getSize() );
114 		
115 		out.putInt(data.length);
116 		for(float v : data){
117 			out.putFloat(v);
118 		}
119 			return out.array();
120 	}
121 
122 }
123