//****************************************************************// // Title: range.h // Description: template functions for simple range things // assumes that > and >= are defined for any class this will be used for // Functions: // between_incl - tells if an object (value) is between a range - // specified by two objects of the same class // inclusive version // between - tells if an object (value) is between a range - // specified by two objects of the same class // exclusive version (cannot equal either end point) // // Author: Dr Michael A. Redmond // Date: 4/06/97 // Last Revised Date: 4/06/97 // // // Copyright (c) 1997. Dr. Michael A. Redmond // This software may not be distributed further without permission from // Dr Michael A. Redmond. // This software is distributed WITHOUT ANY WARRANTY. // //****************************************************************// // conditional compilation to avoid trying to include same thing twice #ifndef RANGE_H #define RANGE_H #include // determine if an object is between two other objects // (requires that objects of the class are orderable) // inclusive version // template function rather than in class template bool between_incl(const T& testing_obj, const T& obj1, const T& obj2) { bool between = false; T high = obj2; T low = obj1; if (obj1 > obj2) { // however defined obj1 is greater than obj2 // so make obj1 the high value high = obj1; low = obj2; } // else take the default // return true if testing obj is between high and low if ((high >= testing_obj) && (testing_obj >= low)) { between = true; } return between; } // determine if an object is between two other objects // (requires that objects of the class are orderable) // exclusive version // template function rather than in class template bool between(const T& testing_obj, const T& obj1, const T& obj2) { bool between = false; T high = obj2; T low = obj1; //DEBUG clog << "in between " << testing_obj << " " << obj1 << " " << obj2 << endl; if (obj1 > obj2) { // however defined obj1 is greater than obj2 // so make obj1 the high value high = obj1; low = obj2; } // else take the default //DEBUG clog << "high: " << high << " low: " << low << endl; // return true if testing obj is between high and low if ((high > testing_obj) && (testing_obj > low)) { //DEBUG clog << "between high and low" << endl; between = true; } return between; } #endif