00001 #ifndef ARRAY_UTILITY_FUNCTIONS_HEADER_INCLUDED
00002 #define ARRAY_UTILITY_FUNCTIONS_HEADER_INCLUDED
00003
00004
00005
00006 template <typename FirstItem, typename SecondItem>
00007 bool isEqualTo(const FirstItem & x, const SecondItem & y) {
00008 return (x == y);
00009 }
00010
00011 template <typename FirstItem, typename SecondItem>
00012 bool isLessThan(const FirstItem & x, const SecondItem & y) {
00013 return (x < y);
00014 }
00015
00016 template <typename FirstItem, typename SecondItem>
00017 bool isGreaterThan(const FirstItem & x, const SecondItem & y) {
00018 return (x > y);
00019 }
00020
00021 template <typename ArrayType, typename ElemType>
00022 long count_if(const ArrayType & arr, long low, long high,
00023 const ElemType & dummy,
00024 bool (*if_this)(const ElemType &)) {
00025 long count, i;
00026 for (count = 0, i = low; i <= high; i++) {
00027 if (if_this(arr[i])) {
00028 count++;
00029 }
00030 }
00031 return count;
00032 }
00033
00034 template <typename ArrayType, typename ElemType, typename ItemType>
00035 long count_if(const ArrayType & arr, long low, long high, const ItemType & item,
00036 const ElemType & dummy,
00037 bool (*if_this)(const ElemType &, const ItemType &)) {
00038 long i, count;
00039 for (count = 0, i = low; i <= high; i++) {
00040 if (if_this(arr[i], item)) {
00041 count++;
00042 }
00043 }
00044 return count;
00045 }
00046
00047
00048 template <typename ArrayType, typename ItemType, typename IfFunc>
00049 long count_nonconseq_if(const ArrayType & arr, long low, long high,
00050 const ItemType & item,
00051 IfFunc & if_this) {
00052
00053 long i, count;
00054 count = 0; i = low;
00055 while (i <= high) {
00056 if (if_this(arr[i], item)) {
00057 count++;
00058 }
00059 do {
00060 i++;
00061 } while ((i <= high) && (if_this(arr[i], item)));
00062 }
00063 return count;
00064 }
00065
00066
00067 template <typename ArrayType, typename IfFunc>
00068 long count_nonconseq_if(const ArrayType & arr, long low, long high,
00069
00070 IfFunc & if_this) {
00071
00072 long i, count;
00073 count = 0; i = low;
00074 while (i <= high) {
00075 if (if_this(arr[i])) {
00076 count++;
00077 }
00078 do {
00079 i++;
00080 } while ((i <= high) && (if_this(arr[i])));
00081 }
00082 return count;
00083 }
00084
00085 template <typename ArrayType, typename ItemType, typename IfFunc>
00086 long find_if(const ArrayType & arr, long low, long high, const ItemType & item,
00087 const IfFunc & if_this) {
00088 long i;
00089 i = low;
00090 while ((i <= high) && !if_this(arr[i], item)) {
00091 i++;
00092 }
00093 return ((i > high) ? (low-1) : (i));
00094 }
00095
00096 template <typename ArrayType, typename ItemType, typename IfFunc>
00097 long find_if(const ArrayType & arr, long low, long high, const ItemType & item,
00098 IfFunc & if_this) {
00099 long i;
00100 i = low;
00101 while ((i <= high) && !if_this(arr[i], item)) {
00102 i++;
00103 }
00104 return ((i > high) ? (low-1) : (i));
00105 }
00106
00107 template <typename ArrayType, typename Function>
00108 void for_each(ArrayType & arr, long low, long high,
00109 const Function & do_this) {
00110 for (long i = low; i <= high; i++) {
00111 do_this(arr[i]);
00112 }
00113 return;
00114 }
00115
00116 template <typename ArrayType, typename Function>
00117 void for_each(ArrayType & arr, long low, long high,
00118 Function & do_this) {
00119 for (long i = low; i <= high; i++) {
00120 do_this(arr[i]);
00121 }
00122 return;
00123 }
00124
00125 #endif