Revision 11858 trunk/libraries/libIverUtiles/src/com/iver/utiles/search/BinarySearchUsingFirstCharacters.java

View differences:

BinarySearchUsingFirstCharacters.java
293 293
		return null;
294 294
	}
295 295
	
296
//	/** THIS VERSION FAILURES IN SOME PARTICULAR SITUATIONS
297
//	 * This method should be used when is wanted distinguish small letters from capital letters during the search, and the comparation of items
298
//	 *   done according an algorithm we define.
299
//	 *   
300
//	 * And it's also necessary that the value returned by the <i>toString()</i> method of each item (supposing 
301
//	 *   they inherit from Object) would be the expected value user saw (that would be used to compare the items).
302
//	 *   
303
//	 * @param text java.lang.String
304
//	 * @param sortOrderedItems java.util.Vector
305
//	 * @param comp An Comparator object which implements the <i><b>compareTo()</b><i>  method.
306
//	 */
307
//	public synchronized static List doSearchConsideringCaseSensitive(String text, Vector sortOrderedItems, Comparator comp) {
308
//		int currentIteration = 0;
309
//		int size = sortOrderedItems.size();
310
//		int maxNumberOfIterations = (int) MathExtension.log2(size);
311
//		int lowIndex = 0;
312
//		int highIndex = sortOrderedItems.size() - 1;
313
//		int mIndx;
314
//
315
//
316
//		while ((lowIndex <= highIndex) && (currentIteration <= maxNumberOfIterations)) {
317
//			mIndx = ( lowIndex + highIndex ) / 2;
318
//
319
//			if ( sortOrderedItems.get( mIndx ).toString().startsWith( text ) ) {
320
//				lowIndex = highIndex = mIndx;
321
//				highIndex ++;
322
//
323
//				// Expand the rank to up
324
//				while ( ( highIndex < size ) && ( sortOrderedItems.get( highIndex ).toString().startsWith( text ) ) ) {
325
//					highIndex ++;
326
//				}
327
//
328
//				// Expand the rank to down
329
//				while ( ( (lowIndex - 1) > -1 ) && ( sortOrderedItems.get( (lowIndex - 1) ).toString().startsWith( text ) ) ) {
330
//					lowIndex --;
331
//				}
332
//
333
//				// It's possible that items with different case, should be between the same case, then this item will be added individually:
334
//				List list = new Vector(sortOrderedItems.subList(lowIndex, highIndex));
335
//				
336
//
337
//				// Expand to down
338
//				lowIndex --;
339
//				while ( ( lowIndex > -1 ) && ( sortOrderedItems.get( (lowIndex ) ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) ) {
340
//					if (sortOrderedItems.get( lowIndex ).toString().startsWith( text )) {
341
//						list.add(0, sortOrderedItems.get( lowIndex ));
342
//					}
343
//					
344
//					lowIndex --;
345
//				}
346
//				
347
//				// Expand to up
348
//				while ( ( highIndex < size ) && ( sortOrderedItems.get( highIndex ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) ) {
349
//					if (sortOrderedItems.get( highIndex ).toString().startsWith( text )) {						
350
//						list.add(list.size(), sortOrderedItems.get( highIndex ));
351
//					}
352
//					
353
//					highIndex ++;
354
//				}
355
//				
356
//				// Returns all items in the rank
357
//				return list; // Breaks the loop
358
//			}
359
//			else {
360
//				if ( comp.compare(sortOrderedItems.get( mIndx ), text ) > 0 ) {
361
//					highIndex = mIndx - 1;
362
//				}
363
//				else {
364
//					lowIndex = mIndx + 1;
365
//				}
366
//			}
367
//
368
//			currentIteration ++;
369
//		}
370
//
371
//		// If no item has been found -> return null
372
//		return null;
373
//	}
374
	
296 375
	/**
297 376
	 * This method should be used when is wanted distinguish small letters from capital letters during the search, and the comparation of items
298 377
	 *   done according an algorithm we define.
......
305 384
	 * @param comp An Comparator object which implements the <i><b>compareTo()</b><i>  method.
306 385
	 */
307 386
	public synchronized static List doSearchConsideringCaseSensitive(String text, Vector sortOrderedItems, Comparator comp) {
308
		int currentIteration = 0;
309
		int size = sortOrderedItems.size();
310
		int maxNumberOfIterations = (int) MathExtension.log2(size);
311
		int lowIndex = 0;
312
		int highIndex = sortOrderedItems.size() - 1;
313
		int mIndx;
387
		List results_list = doSearchIgnoringCaseSensitive(text, sortOrderedItems, comp);
314 388

  
389
		if (results_list == null)
390
			return null;
315 391

  
316
		while ((lowIndex <= highIndex) && (currentIteration <= maxNumberOfIterations)) {
317
			mIndx = ( lowIndex + highIndex ) / 2;
392
		List results = new ArrayList();
318 393

  
319
			if ( sortOrderedItems.get( mIndx ).toString().startsWith( text ) ) {
320
				lowIndex = highIndex = mIndx;
321
				highIndex ++;
322

  
323
				// Expand the rank to up
324
				while ( ( highIndex < size ) && ( sortOrderedItems.get( highIndex ).toString().startsWith( text ) ) ) {
325
					highIndex ++;
326
				}
327

  
328
				// Expand the rank to down
329
				while ( ( (lowIndex - 1) > -1 ) && ( sortOrderedItems.get( (lowIndex - 1) ).toString().startsWith( text ) ) ) {
330
					lowIndex --;
331
				}
332

  
333
				// It's possible that items with different case, should be between the same case, then this item will be added individually:
334
				List list = new Vector(sortOrderedItems.subList(lowIndex, highIndex));
335
				
336

  
337
				// Expand to down
338
				lowIndex --;
339
				while ( ( lowIndex > -1 ) && ( sortOrderedItems.get( (lowIndex ) ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) ) {
340
					if (sortOrderedItems.get( lowIndex ).toString().startsWith( text )) {
341
						list.add(0, sortOrderedItems.get( lowIndex ));
342
					}
343
					
344
					lowIndex --;
345
				}
346
				
347
				// Expand to up
348
				while ( ( highIndex < size ) && ( sortOrderedItems.get( highIndex ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) ) {
349
					if (sortOrderedItems.get( highIndex ).toString().startsWith( text )) {						
350
						list.add(list.size(), sortOrderedItems.get( highIndex ));
351
					}
352
					
353
					highIndex ++;
354
				}
355
				
356
				// Returns all items in the rank
357
				return list; // Breaks the loop
394
		for (int i = 0; i < (results_list.size()); i++) {
395
			if (results_list.get(i).toString().startsWith(text)) {
396
				results.add(results_list.get(i));
358 397
			}
359
			else {
360
				if ( comp.compare(sortOrderedItems.get( mIndx ), text ) > 0 ) {
361
					highIndex = mIndx - 1;
362
				}
363
				else {
364
					lowIndex = mIndx + 1;
365
				}
366
			}
367

  
368
			currentIteration ++;
369 398
		}
370 399

  
371
		// If no item has been found -> return null
372
		return null;
400
		return results;
373 401
	}
374 402
	
375 403
	/**
......
393 421
		
394 422
		while ((lowIndex <= highIndex) && (currentIteration <= maxNumberOfIterations)) {
395 423
			mIndx = ( lowIndex + highIndex ) / 2;
396
			
424

  
397 425
			if ( sortOrderedItems.get( mIndx ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) {
398 426
				lowIndex = highIndex = mIndx;
399 427
				highIndex ++;

Also available in: Unified diff