Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
Static Public Member Functions | List of all members
kome::core::SearchTool Class Reference

This class has some searching algorithm method. More...

#include <SearchTool.h>

Static Public Member Functions

template<typename S , typename T >
static int binarySearch (S value, boost::function< T(int) > getElementFun, boost::function< int(T, S) > compareFun, int size)
 binary search More...
 

Detailed Description

This class has some searching algorithm method.

Definition at line 26 of file SearchTool.h.

Member Function Documentation

template<typename S , typename T >
static int kome::core::SearchTool::binarySearch ( value,
boost::function< T(int) >  getElementFun,
boost::function< int(T, S) >  compareFun,
int  size 
)
inlinestatic

binary search

Parameters
[in]valuespecified value
[in]getElementFunfunction which get element from array.
[in]compareFuncomparison function. return value
  • more than zero : element's value is greater than specified value.
  • zero: element's value is equal to specified value.
  • less than zero: element's value is less than specified value.
[in]sizesize of array
Returns
If it contains a element which has specified value, this function returns the index. [0 or positive] Otherwise it returns (- insertion point - 1). [negative]

Definition at line 48 of file SearchTool.h.

53  {
54  // range of search
55  int top = 0;
56  int bottom = size - 1;
57  int idx = -1;
58 
59  // search
60  while( top <= bottom && idx < 0 ) {
61  // get element located middle of searching range
62  int mid = ( top + bottom ) / 2;
63  T elm0 = getElementFun( mid );
64 
65  // compare
66  int cmp = compareFun( elm0, value );
67  if( cmp == 0 ) { // particular element is found.
68  idx = mid;
69 
70  // return the first of particular elements.
71  mid--;
72  while( mid >= top ) {
73  T elm1 = getElementFun( mid );
74  if( compareFun( elm1, value ) == 0 ) { // particular element
75  idx = mid;
76  mid--;
77  }
78  else {
79  mid = top - 1;
80  }
81  }
82  }
83  else if( cmp > 0 ) { // Element's value is greater than searching value.
84  bottom = mid - 1;
85  }
86  else { // Element's value is less than searching value.
87  top = mid + 1;
88  }
89  }
90 
91  if( idx < 0 ) { // not found
92  idx = - top - 1;
93  }
94 
95  return idx;
96  }

The documentation for this class was generated from the following file: