svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / vectorUtilities / VectorUtilities.java @ 40561
History | View | Annotate | Download (5.61 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.utils.vectorUtilities; |
25 |
|
26 |
import java.util.Comparator; |
27 |
import java.util.Vector; |
28 |
|
29 |
import org.gvsig.utils.MathExtension; |
30 |
|
31 |
|
32 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
33 |
*
|
34 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
35 |
*
|
36 |
* This program is free software; you can redistribute it and/or
|
37 |
* modify it under the terms of the GNU General Public License
|
38 |
* as published by the Free Software Foundation; either version 2
|
39 |
* of the License, or (at your option) any later version.
|
40 |
*
|
41 |
* This program is distributed in the hope that it will be useful,
|
42 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
43 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
44 |
* GNU General Public License for more details.
|
45 |
*
|
46 |
* You should have received a copy of the GNU General Public License
|
47 |
* along with this program; if not, write to the Free Software
|
48 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
49 |
*
|
50 |
* For more information, contact:
|
51 |
*
|
52 |
* Generalitat Valenciana
|
53 |
* Conselleria d'Infraestructures i Transport
|
54 |
* Av. Blasco Ib??ez, 50
|
55 |
* 46010 VALENCIA
|
56 |
* SPAIN
|
57 |
*
|
58 |
* +34 963862235
|
59 |
* gvsig@gva.es
|
60 |
* www.gvsig.gva.es
|
61 |
*
|
62 |
* or
|
63 |
*
|
64 |
* IVER T.I. S.A
|
65 |
* Salamanca 50
|
66 |
* 46005 Valencia
|
67 |
* Spain
|
68 |
*
|
69 |
* +34 963163400
|
70 |
* dac@iver.es
|
71 |
*/
|
72 |
|
73 |
/**
|
74 |
* New functionality to work with vectors (of elements).
|
75 |
*
|
76 |
* @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
|
77 |
*/
|
78 |
public class VectorUtilities { |
79 |
/**
|
80 |
* Adds an item in alphabetical order.
|
81 |
*
|
82 |
* @param v java.util.Vector in alphabetical order.
|
83 |
* @param obj java.lang.Object
|
84 |
*/
|
85 |
public static synchronized void addAlphabeticallyOrdered(Vector v, Object obj) { |
86 |
int size = v.size();
|
87 |
int currentIteration = 0; |
88 |
int index, aux_index;
|
89 |
int lowIndex = 0; |
90 |
int highIndex = size -1; |
91 |
int maxNumberOfIterations = (int) MathExtension.log2(size); |
92 |
|
93 |
// If there are no items
|
94 |
if (size == 0) { |
95 |
v.add(obj); |
96 |
return;
|
97 |
} |
98 |
|
99 |
while ((lowIndex <= highIndex) && (currentIteration <= maxNumberOfIterations)) {
|
100 |
index = ( lowIndex + highIndex ) / 2;
|
101 |
|
102 |
// If the item in the index position has the same value as obj
|
103 |
if (v.get(index).toString().compareTo(obj.toString()) == 0) { |
104 |
v.add(index, obj); |
105 |
return;
|
106 |
} |
107 |
|
108 |
// If the item in the index position has a lower value than the obj
|
109 |
if (v.get(index).toString().compareTo(obj.toString()) < 0) { |
110 |
aux_index = index + 1;
|
111 |
|
112 |
if ((aux_index) >= size) {
|
113 |
v.add(v.size() , obj); |
114 |
return;
|
115 |
} |
116 |
|
117 |
if (v.get(aux_index).toString().compareTo(obj.toString()) > 0) { |
118 |
v.add(aux_index, obj); |
119 |
return;
|
120 |
} |
121 |
|
122 |
lowIndex = aux_index; |
123 |
} |
124 |
else {
|
125 |
// If the item in the index position has a higher value than the obj
|
126 |
if (v.get(index).toString().compareTo(obj.toString()) > 0) { |
127 |
aux_index = index - 1;
|
128 |
|
129 |
if ((aux_index) < 0) { |
130 |
v.add(0 , obj);
|
131 |
return;
|
132 |
} |
133 |
|
134 |
if (v.get(aux_index).toString().compareTo(obj.toString()) < 0) { |
135 |
v.add(index, obj); |
136 |
return;
|
137 |
} |
138 |
|
139 |
highIndex = aux_index; |
140 |
} |
141 |
} |
142 |
|
143 |
currentIteration ++; |
144 |
} |
145 |
} |
146 |
|
147 |
/**
|
148 |
* Adds an item in alphabetical order using a comparator for compare the objects. The vector must be alhabetically ordered.
|
149 |
*
|
150 |
* @param v java.util.Vector in alphabetical order.
|
151 |
* @param obj java.lang.Object
|
152 |
* @param comp java.util.Comparator
|
153 |
*/
|
154 |
public static synchronized void addAlphabeticallyOrdered(Vector v, Object obj, Comparator comp) { |
155 |
int size = v.size();
|
156 |
int currentIteration = 0; |
157 |
int index, aux_index;
|
158 |
int lowIndex = 0; |
159 |
int highIndex = size -1; |
160 |
int maxNumberOfIterations = (int) MathExtension.log2(size); |
161 |
|
162 |
// If there are no items
|
163 |
if (size == 0) { |
164 |
v.add(obj); |
165 |
return;
|
166 |
} |
167 |
|
168 |
while ((lowIndex <= highIndex) && (currentIteration <= maxNumberOfIterations)) {
|
169 |
index = ( lowIndex + highIndex ) / 2;
|
170 |
|
171 |
// If the item in the index position has the same value as obj
|
172 |
if (comp.compare(v.get(index), obj) == 0) { |
173 |
v.add(index, obj); |
174 |
return;
|
175 |
} |
176 |
|
177 |
// If the item in the index position has a lower value than the obj
|
178 |
if (comp.compare(v.get(index), obj) < 0) { |
179 |
aux_index = index + 1;
|
180 |
|
181 |
if ((aux_index) >= size) {
|
182 |
v.add(v.size() , obj); |
183 |
return;
|
184 |
} |
185 |
|
186 |
if (comp.compare(v.get(aux_index), obj) > 0) { |
187 |
v.add(aux_index, obj); |
188 |
return;
|
189 |
} |
190 |
|
191 |
lowIndex = aux_index; |
192 |
} |
193 |
else {
|
194 |
// If the item in the index position has a higher value than the obj
|
195 |
if (comp.compare(v.get(index), obj) > 0) { |
196 |
aux_index = index - 1;
|
197 |
|
198 |
if ((aux_index) < 0) { |
199 |
v.add(0 , obj);
|
200 |
return;
|
201 |
} |
202 |
|
203 |
if (comp.compare(v.get(aux_index), obj) < 0) { |
204 |
v.add(index, obj); |
205 |
return;
|
206 |
} |
207 |
|
208 |
highIndex = aux_index; |
209 |
} |
210 |
} |
211 |
|
212 |
currentIteration ++; |
213 |
} |
214 |
} |
215 |
} |