User Manual, Developers Guide and API Documentation

SimpleBinarySearch.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 5, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #include <WNS/search/SimpleBinarySearch.hpp>
00029 #include <WNS/search/SearchCreator.hpp>
00030 
00031 #include <iostream>
00032 
00033 using namespace wns::search;
00034 
00035 STATIC_FACTORY_REGISTER_WITH_CREATOR(
00036     SimpleBinarySearch,
00037     ISearch,
00038     "wns.search.SimpleBinarySearch",
00039     SearchCreator);
00040 
00041 SimpleBinarySearch::SimpleBinarySearch(int lowerBound, int startValue, Comparator comp)
00042     : lowerBound_(lowerBound),
00043       startValue_(startValue),
00044       comp_(comp)
00045 {
00046 } // SimpleBinarySearch
00047 
00048 
00049 int SimpleBinarySearch::search(int upperBound)
00050 {
00051     int currentValue = startValue_;
00052     int currentLowerBound = lowerBound_;
00053     int currentUpperBound = upperBound;
00054 
00055     while (true)
00056     {
00057         if (comp_(currentValue))
00058         {
00059             currentLowerBound = currentValue;
00060             currentValue += (currentUpperBound - currentValue) / 2;
00061         }
00062         else
00063         {
00064             currentUpperBound = currentValue;
00065             currentValue -= (currentValue - currentLowerBound) / 2;
00066         }
00067 
00068         if (currentLowerBound + 1 == currentUpperBound)
00069         {
00070             return currentLowerBound;
00071         }
00072         else if (currentLowerBound == currentUpperBound)
00073         {
00074             return currentLowerBound - 1;
00075         }
00076     }
00077 } // search

Generated on Fri May 25 03:31:55 2012 for openWNS by  doxygen 1.5.5