index.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: index.hpp 293 2006-11-10 16:39:56Z rickmach $"
00002 
00003 //============================================================================
00004 //
00005 //  This file is part of GPSTk, the GPS Toolkit.
00006 //
00007 //  The GPSTk is free software; you can redistribute it and/or modify
00008 //  it under the terms of the GNU Lesser General Public License as published
00009 //  by the Free Software Foundation; either version 2.1 of the License, or
00010 //  any later version.
00011 //
00012 //  The GPSTk is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU Lesser General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU Lesser General Public
00018 //  License along with GPSTk; if not, write to the Free Software Foundation,
00019 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //  
00021 //  Copyright 2004, The University of Texas at Austin
00022 //
00023 //============================================================================
00024 
00025 //============================================================================
00026 //
00027 //This software developed by Applied Research Laboratories at the University of
00028 //Texas at Austin, under contract to an agency or agencies within the U.S. 
00029 //Department of Defense. The U.S. Government retains all rights to use,
00030 //duplicate, distribute, disclose, or release this software. 
00031 //
00032 //Pursuant to DoD Directive 523024 
00033 //
00034 // DISTRIBUTION STATEMENT A: This software has been approved for public 
00035 //                           release, distribution is unlimited.
00036 //
00037 //=============================================================================
00038 
00044 #ifndef INDEX_ROUTINE_INCLUDE
00045 #define INDEX_ROUTINE_INCLUDE
00046 
00047 //------------------------------------------------------------------------------------
00048 // find the index of first occurance of item t (of type T) in vector<T> v;
00049 // i.e. j = index(v,t); implies v[j] == t. Return -1 if t is not found.
00050 template<class T> int index(const std::vector<T> v, const T& t) 
00051 {
00052    for(int i=0; i<v.size(); i++) {
00053       if(v[i] == t) return i;
00054    }
00055    return -1;
00056 }
00057 
00058 /*
00059 // find the index of first occurance of item t (of type T) in vector<T> v;
00060 // i.e. j = index(v,t); implies v[j] == t. Return -1 if t is not found.
00061 // assume that the vector<T> is strictly increasing,
00062 // that is that J > I strictly implies v[J] > v[I].
00063 // let istart be a suggested starting point for the search;
00064 // that is istart+(small) or istart-(small) may very well be the desired index
00065 template<class T> int index_uniform(const std::vector<T> v, const T& t, int istart=0)
00066 {
00067    if(istart < 0 || istart > v.size()) istart=0;
00068    int i=istart,k=0;
00069    while(i<v.size() && i>=0) {
00070       if(v[i] == t) {
00071          return i;
00072       }
00073       else if(v[i] < t) {
00074          if(k == -1) return -1;
00075          k = 1;                  // increasing
00076       }
00077       else {
00078          if(k == 1) return -1;
00079          k = -1;                 // decreasing
00080       }
00081       i += k;
00082    }
00083    return -1;
00084 }
00085 */
00086 
00087 #endif

Generated on Wed Feb 8 03:31:00 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1