#include #include #include #include using namespace std; #include #include #include #include "utility.h" /** * Allocate and read in an array of integers from an istream */ int readintarray(istream *in, ///< input stream int &n, ///< length of array int &max, ///< largest value for elements int * &storage ///< reference for resultant array pointer ) { assert(in); *in >> n; storage = new (nothrow) int [n]; if (storage == 0) { return -1; } *in >> storage[0]; max = storage[0]; for (int i = 1; i < n; i++) { *in >> storage[i]; if (storage[i] > max) { max = storage[i]; } } return 0; } /** * Allocate and generate an array of random integers */ int randomintarray(int n, ///< desired length of array int max, ///< largest value for elements int * &storage ///< reference for resultant array pointer ) { int i; storage = new (nothrow) int [n]; if (storage == 0) { return -1; } for (i = 0; i < n; i++) { storage[i] = random() % max; } return 0; } /** * Copy from one int array to another */ void copyarray(int * source, ///< source array int * dest, ///< destination array int count ///< number of elements to copy ) { for (int i = 0; i < count; i++) { dest[i] = source[i]; } } double tv2sec(struct timeval *tv) { double secs = (double)tv->tv_sec; secs += ((float) tv->tv_usec / 1000000.0); return secs; } double getTime() { struct timeval t; gettimeofday(&t, NULL); return tv2sec(&t); } double timediff(struct timeval *s, struct timeval *e) { struct timeval diff_tv; diff_tv.tv_usec = e->tv_usec - s->tv_usec; diff_tv.tv_sec = e->tv_sec - s->tv_sec; if (s->tv_usec > e->tv_usec) { diff_tv.tv_usec += 1000000; diff_tv.tv_sec--; } return tv2sec(&diff_tv); }