// File: search.h
// Template functions for search and vectors
// plus counters for project

#include <vector>
using namespace std;

int searchCount = 0;  // instruction counter for LinSearch

// Function: LinSearch
// Returns the index the target if in range [first, last), last otherwise
// Uses iterative sequential search
// Precondition: first < last
template <typename T>
int LinSearch (const vector<T> & v,    // RECEIVED: vector to search
               int first,              // RECEIVED: lower bound of search
               int last,               // RECEIVED: upper bound of search
               const T & target)       // RECEIVED: value to search for
{
   // scan indices from first <= index < last for a match
   for (int index = first; index < last; index++)
   {
      searchCount += 2;    // for header and if test
      if (v[index] == target)
      {
	 searchCount++;    // return
	 return index;
      }
   }
   searchCount+=2;         // extra for header and return

   // no match
   return last;
}  // end LinSearch
