static void splitVec(const std::vector<Sample*>& data, const std::vector<IntIndex>& valSet, std::vector<Sample*>& setA, std::vector<Sample*>& setB, int threshold, int margin = 0) { // search largest value such that val<t std::vector<IntIndex>::const_iterator it_first, it_second; it_first = lower_bound(valSet.begin(), valSet.end(), threshold - margin, less_than()); if (margin == 0) it_second = it_first; else it_second = lower_bound(valSet.begin(), valSet.end(), threshold + margin, less_than()); // Split training data into two sets A,B accroding to threshold t setA.resize(it_second - valSet.begin()); setB.resize(valSet.end() - it_first); std::vector<IntIndex>::const_iterator it = valSet.begin(); typename std::vector<Sample*>::iterator itSample; for (itSample = setA.begin(); itSample < setA.end(); ++itSample, ++it) (*itSample) = data[it->second]; it = it_first; for (itSample = setB.begin(); itSample < setB.end(); ++itSample, ++it) (*itSample) = data[it->second]; };
TEST(tagged, float_32) { ASSERT_EQ(sizeof(uint32_t), sizeof(float32_t)); value_t one = new_float_32(1.0); ASSERT_TRUE(get_float_32_value(one) == 1.0); value_t zero = new_float_32(0.0); ASSERT_TRUE(get_float_32_value(zero) == 0.0); value_t minus_one = new_float_32(-1.0); ASSERT_TRUE(get_float_32_value(minus_one) == -1.0); ASSERT_VALEQ(equal(), value_ordering_compare(one, one)); ASSERT_VALEQ(equal(), value_ordering_compare(zero, zero)); ASSERT_VALEQ(equal(), value_ordering_compare(minus_one, minus_one)); ASSERT_VALEQ(less_than(), value_ordering_compare(minus_one, zero)); ASSERT_VALEQ(less_than(), value_ordering_compare(zero, one)); ASSERT_VALEQ(greater_than(), value_ordering_compare(zero, minus_one)); ASSERT_VALEQ(greater_than(), value_ordering_compare(one, zero)); value_t nan = float_32_nan(); ASSERT_VALEQ(unordered(), value_ordering_compare(nan, nan)); ASSERT_VALEQ(unordered(), value_ordering_compare(nan, one)); ASSERT_VALEQ(unordered(), value_ordering_compare(nan, zero)); ASSERT_VALEQ(unordered(), value_ordering_compare(zero, nan)); ASSERT_VALEQ(unordered(), value_ordering_compare(one, nan)); ASSERT_SAME(nan, nan); value_t inf = float_32_infinity(); value_t minf = float_32_minus_infinity(); ASSERT_VALEQ(unordered(), value_ordering_compare(nan, inf)); ASSERT_VALEQ(unordered(), value_ordering_compare(inf, nan)); ASSERT_VALEQ(unordered(), value_ordering_compare(nan, minf)); ASSERT_VALEQ(unordered(), value_ordering_compare(minf, nan)); ASSERT_VALEQ(less_than(), value_ordering_compare(one, inf)); ASSERT_VALEQ(greater_than(), value_ordering_compare(inf, one)); ASSERT_VALEQ(greater_than(), value_ordering_compare(one, minf)); ASSERT_VALEQ(less_than(), value_ordering_compare(minf, one)); ASSERT_VALEQ(equal(), value_ordering_compare(inf, inf)); ASSERT_VALEQ(greater_than(), value_ordering_compare(inf, minf)); ASSERT_VALEQ(less_than(), value_ordering_compare(minf, inf)); ASSERT_TRUE(is_float_32_nan(nan)); ASSERT_FALSE(is_float_32_nan(minus_one)); ASSERT_FALSE(is_float_32_nan(zero)); ASSERT_FALSE(is_float_32_nan(one)); ASSERT_FALSE(is_float_32_nan(inf)); ASSERT_FALSE(is_float_32_nan(minf)); ASSERT_FALSE(is_float_32_finite(nan)); ASSERT_TRUE(is_float_32_finite(minus_one)); ASSERT_TRUE(is_float_32_finite(zero)); ASSERT_TRUE(is_float_32_finite(one)); ASSERT_FALSE(is_float_32_finite(inf)); ASSERT_FALSE(is_float_32_finite(minf)); }
static int median3(int *arr, int l, int r) { int mid = (l+r)/2; if (less_than(arr[mid], arr[l], &comp)) swap_pivot(&arr[mid], &arr[l]); if (less_than(arr[r], arr[mid], &comp)) swap_pivot(&arr[r], &arr[mid]); if (less_than(arr[mid], arr[l], &comp)) swap_pivot(&arr[mid], &arr[l]); swap_pivot(&arr[mid], &arr[r-1]); return arr[r-1]; }
void main1_3(void *arg) { color Kd = color(diffuse(), diffuse(), diffuse()); normal Nf = faceforward(normalize(N()), I()); vector V = -normalize(I()); while (illuminance(P(), Nf, PI / 2.0f)) { color C = 0.0f; SAMPLE_LIGHT_2(color, C, 0.0f, C += Cl() * ( color_() * Kd * (normalize(L()) % Nf) + cosinePower() * specularColor() * specularbrdf(normalize(L()), Nf, V, 0.1f/*roughness()*/) ) ); Ci() += C; } if ( ! less_than( &transparency(), LIQ_SCALAR_ALMOST_ZERO ) ) {//transparent Ci() = Ci() * ( 1.0f - transparency() ) + trace_transparent() * transparency(); }//else{ opacity } setOutputForMaya(); }
// returns true if degree leading term p1 < degree leading term p2 bool less_than(Polynomial *p1, Polynomial *p2, int ordering) { if (ordering == LEX){ for (int i = 0; i < p1->num_vars; i++){ if (p1->terms[0].pow[i] < p2->terms[0].pow[i]) return true; else if (p1->terms[0].pow[i] > p2->terms[0].pow[i]) return false; } return false; } else if (ordering == GRLEX) { int sum1=0, sum2=0; for (int i = 0; i < p1->num_vars; i++){ sum1 += p1->terms[0].pow[i]; sum2 += p2->terms[0].pow[i]; } if (sum1 < sum2) return true; return less_than(p1, p2, LEX); } else if (ordering == GREVLEX) { int sum1=0, sum2=0; for (int i = 0; i < p1->num_vars; i++){ sum1 += p1->terms[0].pow[i]; sum2 += p2->terms[0].pow[i]; } if (sum1 < sum2) return true; for (int i = p1->num_vars -1; i >= 0; i--){ if (p1->terms[0].pow[i] > p2->terms[0].pow[i]) return true; else if (p1->terms[0].pow[i] < p2->terms[0].pow[i]) return false; } return false; } return true; }
// Subtract one time from another. static duration_type subtract(const time_type& t1, const time_type& t2) { // DWORD tick count values can wrap (see less_than() below). We'll convert // to a duration by taking the absolute difference and adding the sign // based on which is the "lesser" absolute time. return duration_type(less_than(t1, t2) ? -static_cast<LONG>(t2.ticks_ - t1.ticks_) : static_cast<LONG>(t1.ticks_ - t2.ticks_)); }
void PROCEDURE_TIMER::stop(void) { gettimeofday(&now_rep, 0); subtract(&now_rep, &start_rep); last_duration_rep = to_seconds(&now_rep); // cerr << idstr_rep << ": " << kvu_numtostr(length, 16) << " secs." << endl; events_rep++; event_time_total_rep += last_duration_rep; if (events_rep == 1) memcpy(&min_event_rep, &now_rep, sizeof(struct timeval)); if (less_than(&now_rep, &min_event_rep)) memcpy(&min_event_rep, &now_rep, sizeof(struct timeval)); if (less_than(&max_event_rep, &now_rep)) memcpy(&max_event_rep, &now_rep, sizeof(struct timeval)); if (less_than(&now_rep, &lower_bound_rep)) events_under_bound_rep++; if (less_than(&upper_bound_rep, &now_rep)) events_over_bound_rep++; }
void StreamSorter<Message>::streaming_merge(list<cursor_t>& cursors, emitter_t& emitter, size_t expected_messages) { create_progress("merge " + to_string(cursors.size()) + " files", expected_messages == 0 ? 1 : expected_messages); // Count the messages we actually see size_t observed_messages = 0; // Put all the files in a priority queue based on which has a message that comes first. // We work with pointers to cursors because we don't want to be copying the actual cursors around the heap. // We also *reverse* the order, because priority queues put the "greatest" element first auto cursor_order = [&](cursor_t*& a, cursor_t*& b) { if (b->has_next()) { if(!a->has_next()) { // Cursors that aren't empty come first return true; } return less_than(*(*b), *(*a)); } return false; }; priority_queue<cursor_t*, vector<cursor_t*>, decltype(cursor_order)> cursor_queue(cursor_order); for (auto& cursor : cursors) { // Put the cursor pointers in the queue cursor_queue.push(&cursor); } while(!cursor_queue.empty() && cursor_queue.top()->has_next()) { // Until we have run out of data in all the temp files // Pop off the winning cursor cursor_t* winner = cursor_queue.top(); cursor_queue.pop(); // Grab and emit its message, and advance it emitter.write(std::move(winner->take())); // Put it back in the heap if it is not depleted if (winner->has_next()) { cursor_queue.push(winner); } // TODO: Maybe keep it off the heap for the next loop somehow if it still wins observed_messages++; if (expected_messages != 0) { update_progress(observed_messages); } } // We finished the files, so say we're done. // TODO: Should we warn/fail if we expected the wrong number of messages? update_progress(expected_messages == 0 ? 1 : expected_messages); destroy_progress(); }
void egl_config_sort(int *ids, bool use_red, bool use_green, bool use_blue, bool use_alpha) { int i, j; for (i = 1; i < EGL_MAX_CONFIGS; i++) for (j = 0; j < EGL_MAX_CONFIGS - i; j++) if (less_than(ids[j + 1], ids[j], use_red, use_green, use_blue, use_alpha)) { int temp = ids[j]; ids[j] = ids[j + 1]; ids[j + 1] = temp; } }
static int quick_sort_1(int *arr, int l, int r, int size) { if (l >= r) return -1; int pivot = (l+r)/2; //swap the pivot to end swap_pivot(&arr[pivot], &arr[r]); int left = l; int right = r-1; //because the pivot swap to the end, so the right index need to decrease 1 while (/*left < right*/1) { while (less_than(arr[left], arr[r], &comp)) ++ left; while(less_than(arr[r], arr[right], &comp)) -- right; if (left < right) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; ++ left; -- right; ++ swap_times; } else break; } swap_pivot(&arr[r], &arr[left]); for(int n = 0; n < size; ++n) cout << arr[n] << " "; cout<<endl; quick_sort_1(arr, l, left-1, size); quick_sort_1(arr, left+1, r, size); return 0; }
TEST(tagged, relation) { ASSERT_TRUE(test_relation(less_than(), reLessThan)); ASSERT_TRUE(test_relation(less_than(), reLessThan | reEqual)); ASSERT_FALSE(test_relation(less_than(), reEqual)); ASSERT_FALSE(test_relation(less_than(), reGreaterThan)); ASSERT_FALSE(test_relation(less_than(), reGreaterThan | reEqual)); ASSERT_FALSE(test_relation(less_than(), reUnordered)); ASSERT_FALSE(test_relation(equal(), reLessThan)); ASSERT_TRUE(test_relation(equal(), reLessThan | reEqual)); ASSERT_TRUE(test_relation(equal(), reEqual)); ASSERT_FALSE(test_relation(equal(), reGreaterThan)); ASSERT_TRUE(test_relation(equal(), reGreaterThan | reEqual)); ASSERT_FALSE(test_relation(equal(), reUnordered)); ASSERT_FALSE(test_relation(greater_than(), reLessThan)); ASSERT_FALSE(test_relation(greater_than(), reLessThan | reEqual)); ASSERT_FALSE(test_relation(greater_than(), reEqual)); ASSERT_TRUE(test_relation(greater_than(), reGreaterThan)); ASSERT_TRUE(test_relation(greater_than(), reGreaterThan | reEqual)); ASSERT_FALSE(test_relation(greater_than(), reUnordered)); ASSERT_FALSE(test_relation(unordered(), reLessThan)); ASSERT_FALSE(test_relation(unordered(), reLessThan | reEqual)); ASSERT_FALSE(test_relation(unordered(), reEqual)); ASSERT_FALSE(test_relation(unordered(), reGreaterThan)); ASSERT_FALSE(test_relation(unordered(), reGreaterThan | reEqual)); ASSERT_TRUE(test_relation(unordered(), reUnordered)); }
void test_stable_partition_async(ExPolicy p, IteratorTag) { static_assert( hpx::parallel::execution::is_execution_policy<ExPolicy>::value, "hpx::parallel::execution::is_execution_policy<ExPolicy>::value"); typedef std::vector<int>::iterator base_iterator; typedef test::test_iterator<base_iterator, IteratorTag> iterator; std::vector<int> c(10007); std::vector<int> d(c.size()); std::iota(boost::begin(c), boost::end(c), std::rand()); std::copy(boost::begin(c), boost::end(c), boost::begin(d)); int partition_at = std::rand(); auto f = hpx::parallel::stable_partition(p, iterator(boost::begin(c)), iterator(boost::end(c)), less_than(partition_at)); auto result = f.get(); auto partition_pt = std::find_if(boost::begin(c), boost::end(c), great_equal_than(partition_at)); HPX_TEST(result.base() == partition_pt); // verify values std::stable_partition(boost::begin(d), boost::end(d), less_than(partition_at)); std::size_t count = 0; HPX_TEST(std::equal(boost::begin(c), boost::end(c), boost::begin(d), [&count](std::size_t v1, std::size_t v2) -> bool { HPX_TEST_EQ(v1, v2); ++count; return v1 == v2; })); HPX_TEST_EQ(count, d.size()); }
static int quick_sort_2(int *arr, int l, int r, int size) { if (l >= r) return -1; int pivot = median3(arr, l, r); //choosing the median of the first, middle and last element of the partition for the pivot int left = l; int right = r-1; //because the pivot swap to the end, so the right index need to decrease 1 while (/*left < right*/1) { while (less_than(arr[left], /*arr[r-1]*/pivot, &comp)) ++ left; while(less_than(pivot, arr[right], &comp)) -- right; if (left < right) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; ++ left; -- right; ++ swap_times; } else break; } swap_pivot(&arr[r-1], &arr[left]); for(int n = 0; n < size; ++n) cout << arr[n] << " "; cout<<endl; quick_sort_2(arr, l, left-1, size); quick_sort_2(arr, left+1, r, size); return 0; }
void selection_sort_by_list(struct data_list* first){ struct data_list *cur, *min, *inner_cur; for(cur=first; cur!=NULL && cur->next!=NULL;cur=cur->next){ min = cur; for (inner_cur = cur->next; inner_cur!=NULL; inner_cur = inner_cur->next) { if (less_than(inner_cur->num, min->num)) { min = inner_cur; } } if (min!=cur) { swap(min, cur); } } }
void main1(void *arg) { color Kd = color(diffuse(), diffuse(), diffuse()); normal Nf = faceforward(normalize(N()), I()); vector V = -normalize(I()); while (illuminance(P(), Nf, PI / 2.0f)) { color C = 0.0f; color last = 0.0f; int num_samples = 0; while (sample_light()) { C += Cl() * ( color_() * Kd * (normalize(L()) % Nf) + cosinePower() * specularColor() * specularbrdf(normalize(L()), Nf, V, 0.1f/*roughness()*/) ); ++ num_samples; if ((num_samples % 4) == 0) { color current = C * (1.0f / (scalar)num_samples); if (converged(current, last)){ break; } last = current; } } C *= (1.0f / (scalar)num_samples); Ci() += C; } if ( ! less_than( &transparency(), LIQ_SCALAR_ALMOST_ZERO ) ) {//transparent Ci() = Ci() * ( 1.0f - transparency() ) + trace_transparent() * transparency(); }//else{ opacity } setOutputForMaya(); }
//归并排序使用最优的比较次数,但很难用于主存排序中,因为需要引入额外的存储空间 static int merge_sort(int *arr, int l, int r, int *temp, int size) { if (l >= r) return -1; int mid = (l + r) / 2; merge_sort(arr, l, mid, temp, size); merge_sort(arr, mid+1, r, temp, size); int i,j,index; i = l; j = mid+1; index = l; while (i<=mid && j<=r) { if (less_than(arr[i], arr[j], &comp)) { temp[index++] = arr[i++]; ++ swap_times; } else { temp[index++] = arr[j++]; ++ swap_times; } } while (i <= mid) { temp[index++] = arr[i++]; ++ swap_times; } while(j <= r) { temp[index++] = arr[j++]; ++ swap_times; } for (i = l, index=l; i <= r; ++i) { arr[i] = temp[index++]; } return 0; }
void Ordered_array<data_t>::insert(const data_t &data){ // ASSERT(array->less_than); size_t iterator = 0; while( iterator<size() && less_than( base[iterator], data) ) iterator++; if( iterator==size() ){ // just add at the end of the array. base[size()] = data; size( size()+1 ); }else{ data_t tmp=base[iterator]; base[iterator] = data; while( iterator<size() ){ ++iterator; data_t tmp2=base[iterator]; base[iterator]=tmp; tmp=tmp2; } size(size()+1); } }
void round_nearest( float& value ) { if( std::numeric_limits<T>::is_integer ) { // make sure it rounds to nearest when cast to T switch( std::numeric_limits<T>::round_style ) { case 0: // round to 0 if( less_than( value, 0.f ) ) { value -= 0.5f; } else { value += 0.5f; } break; case 2: // round to inf value -= 0.5f; break; case 3: // round to -inf value += 0.5f; break; case -1: // indeterminate default: assert(false); case 1: // round toward nearest break; } } }
Position StreamSorter<Message>::get_min_position(const Message& msg) const { // This holds the min Position we get Position min_pos; // We set this to true when we fill in the min bool have_min = false; WrappingPositionScanner<Message>::scan(msg, [&](const Position& observed) -> bool { if (!have_min || less_than(observed, min_pos)) { // We have a new min position min_pos = observed; have_min = true; } // Keep scanning return true; }); // If we don't have a min, we should return the empty position (which we already have). // Otherwise we should return the min position. return min_pos; }
bool StreamSorter<Message>::less_than(const Message &a, const Message &b) const { return less_than(get_min_position(a), get_min_position(b)); }
static void splitVec(const std::vector<Sample*>& data, const std::vector<IntIndex>& valSet, std::vector<std::vector<Sample*> >& sets, int threshold, int margin) { // search largest value such that val<t std::vector<IntIndex>::const_iterator it_first, it_second; it_first = lower_bound(valSet.begin(), valSet.end(), threshold - margin, less_than()); if (margin == 0) it_second = it_first; else it_second = lower_bound(valSet.begin(), valSet.end(), threshold + margin, less_than()); if (it_first == it_second) // no intersection between the two thresholds { std::vector<IntIndex>::const_iterator it = it_first; sets.resize(2); // Split training data into two sets A,B accroding to threshold t sets[0].resize(it - valSet.begin()); sets[1].resize(data.size() - sets[0].size()); it = valSet.begin(); typename std::vector<Sample*>::iterator itSample; for (itSample = sets[0].begin(); itSample < sets[0].end(); ++itSample, ++it) (*itSample) = data[it->second]; it = valSet.begin() + sets[0].size(); for (itSample = sets[1].begin(); itSample < sets[1].end(); ++itSample, ++it) (*itSample) = data[it->second]; assert( (sets[0].size() + sets[1].size()) == data.size()); } else { sets.resize(3); // Split training data into two sets A,B accroding to threshold t sets[0].resize(it_first - valSet.begin()); sets[1].resize(it_second - it_first); sets[2].resize(valSet.end() - it_second); std::vector<IntIndex>::const_iterator it = valSet.begin(); typename std::vector<Sample*>::iterator itSample; for (itSample = sets[0].begin(); itSample < sets[0].end(); ++itSample, ++it) (*itSample) = data[it->second]; it = valSet.begin() + sets[0].size(); for (itSample = sets[1].begin(); itSample < sets[1].end(); ++itSample, ++it) (*itSample) = data[it->second]; it = valSet.begin() + sets[0].size() + sets[1].size(); for (itSample = sets[2].begin(); itSample < sets[2].end(); ++itSample, ++it) (*itSample) = data[it->second]; assert( (sets[0].size() + sets[1].size() + sets[2].size()) == data.size()); } };
int main() { std::cout << less_than(2, 3) << '\n'; std::cout << less_than(2.2, 2.7) << '\n'; std::cout << less_than(2.7, 2.2) << '\n'; }
inline bool greater_than( const Left& l, const Right& r ) { return less_than(r,l); }
/*! * @brief Displays info about the timer. * * This function prints a load of information about the captured timing information * to the console. * * @param timer The timer for which info should be shown. * @param show_all After the average, etc. show all elapsed times, 1 per line. * Set false if only a summary of the times is needed. * * @return 0 if successful */ void timing_print_info(TIMING_TIMER_T * timer, bool show_all) { int i; struct timeval lowest_elapsed_time = {100000000, 0}; int lowest_elapsed_time_index = 0; struct timeval highest_elapsed_time = {0, 0}; int highest_elapsed_time_index = 0; unsigned long std_dev = 0; int elapsed_usec; int average_usec; struct timeval elapsed_time = {0, 0}; struct timeval total_elapsed_time = {0, 0}; struct timeval average_elapsed_time = {0, 0}; struct timeval delta; if(timer != NULL) { if(timer->num_times_recorded == 0) { tracemsg(_a("\nTiming: no results captured.")); return; } tracemsg(_a("Timing: number of results captured: %d"), timer->num_times_recorded); /* Go figure out some info about the timers captured. */ for(i = 0; i < timer->num_times_recorded; i++) { /* Get elapsed_time. */ subtract(&(timer->times[i].start_time), &(timer->times[i].stop_time), &elapsed_time); /* Check elapsed time against highest. */ if(greater_than(&elapsed_time, &highest_elapsed_time)) { highest_elapsed_time.tv_sec = elapsed_time.tv_sec; highest_elapsed_time.tv_usec = elapsed_time.tv_usec; highest_elapsed_time_index = i; } /* Check elapsed time against lowest. */ if(less_than(&elapsed_time, &lowest_elapsed_time)) { lowest_elapsed_time.tv_sec = elapsed_time.tv_sec; lowest_elapsed_time.tv_usec = elapsed_time.tv_usec; lowest_elapsed_time_index = i; } /* Add this elapsed time to total. */ add(&elapsed_time, &total_elapsed_time, &total_elapsed_time); if(show_all) { tracemsg(_a(" result %4d: elapsed time %2lds, %6ld usec"), i, elapsed_time.tv_sec, elapsed_time.tv_usec); } } /* Figure out the average elapsed time. */ average(&total_elapsed_time, timer->num_times_recorded, &average_elapsed_time); tracemsg(_a("Average elapsed %2lds, %6ld usec"), average_elapsed_time.tv_sec, average_elapsed_time.tv_usec); /* Figure out standard deviation for the set of elapsed times. This needs to be * done after the average is calculated. */ average_usec = USECS(average_elapsed_time); for(i = 0; i < timer->num_times_recorded; i++) { /* Get elapsed_time between the start and stop. */ subtract(&(timer->times[i].start_time), &(timer->times[i].stop_time), &elapsed_time); elapsed_usec = USECS(elapsed_time); std_dev += ((elapsed_usec - average_usec) * (elapsed_usec - average_usec)); } std_dev = std_dev / i; /* Rounding error shouldn't matter too much.*/ std_dev = square_root(std_dev); tracemsg(_a(" Approx std dev %6ld usec"), std_dev); /* For highest time recorded, figure out delta between this and average. */ subtract(&average_elapsed_time, &highest_elapsed_time, &delta); tracemsg(_a("Highest elapsed %2lds, %6ld usec (delta from avg %lds, %6ldus, result %d)"), highest_elapsed_time.tv_sec, highest_elapsed_time.tv_usec, delta.tv_sec, delta.tv_usec, highest_elapsed_time_index); /* Same for lowest. */ subtract(&lowest_elapsed_time, &average_elapsed_time, &delta); tracemsg(_a(" Lowest elapsed %2lds, %6ld usec (delta from avg %lds, %6ldus, result %d)"), lowest_elapsed_time.tv_sec, lowest_elapsed_time.tv_usec, delta.tv_sec, delta.tv_usec, lowest_elapsed_time_index); } }
/*! * @brief Displays rate info about the timer. * * This function prints a load of information about the rate of a timer's start times * to the console. * * @param timer The timer for which info should be shown. * @param show_all After the average, etc. show all periods, 1 per line. Set false if * only a summary of the times is needed. * * @return 0 if successful */ void timing_print_rate_info(TIMING_TIMER_T * timer, bool show_all) { int i; struct timeval lowest_elapsed_time = {100000000, 0}; int lowest_elapsed_time_index = 0; struct timeval highest_elapsed_time = {0, 0}; int highest_elapsed_time_index = 0; int rate; unsigned long std_dev = 0; int elapsed_usec; int average_usec; struct timeval elapsed_time = {0, 0}; struct timeval total_elapsed_time = {0, 0}; struct timeval average_elapsed_time = {0, 0}; struct timeval delta; /* Since we are looking at the start times for rate information, we need two times stored * for one period, three times for two periods, etc. */ int num_periods = timer->num_times_recorded - 1; /* There's no point in continuing if there is insufficient information available * to show period information. */ if(num_periods <= 0) { tracemsg(_a("Rate info: %d timer start/stops is insufficient for rate analysis."), timer->num_times_recorded); return; } tracemsg(_a("Rate info: number of periods captured: %d"), num_periods); /* Go figure out some info about the timers captured. */ for(i = 0; i < num_periods; i++) { /* Get elapsed_time between the two start times. */ subtract(&(timer->times[i].start_time), &(timer->times[i+1].start_time), &elapsed_time); /* Check elapsed time against highest. */ if(greater_than(&elapsed_time, &highest_elapsed_time)) { highest_elapsed_time.tv_sec = elapsed_time.tv_sec; highest_elapsed_time.tv_usec = elapsed_time.tv_usec; highest_elapsed_time_index = i; } /* Check elapsed time against lowest. */ if(less_than(&elapsed_time, &lowest_elapsed_time)) { lowest_elapsed_time.tv_sec = elapsed_time.tv_sec; lowest_elapsed_time.tv_usec = elapsed_time.tv_usec; lowest_elapsed_time_index = i; } /* Add this elapsed time to total. */ add(&elapsed_time, &total_elapsed_time, &total_elapsed_time); if(false) { tracemsg(_a(" result %4d: elapsed time %2lds, %6ld usec"), i, elapsed_time.tv_sec, elapsed_time.tv_usec); } } /* Figure out the average elapsed time. */ average(&total_elapsed_time, timer->num_times_recorded, &average_elapsed_time); rate = rate_per_sec(&average_elapsed_time); tracemsg(_a(" Average period %2lds, %6ld usec (%d/sec)"), average_elapsed_time.tv_sec, average_elapsed_time.tv_usec, rate); /* Figure out standard deviation for the set of periods. */ average_usec = USECS(average_elapsed_time); for(i = 0; i < num_periods; i++) { /* Get elapsed_time between the two start times. */ subtract(&(timer->times[i].start_time), &(timer->times[i+1].start_time), &elapsed_time); elapsed_usec = USECS(elapsed_time); std_dev += ((elapsed_usec - average_usec) * (elapsed_usec - average_usec)); } std_dev = std_dev / i; /* Rounding error shouldn't matter too much.*/ std_dev = square_root(std_dev); tracemsg(_a(" Approx std dev %6ld usec"), std_dev); /* For highest time recorded, figure out delta between this and average. */ subtract(&average_elapsed_time, &highest_elapsed_time, &delta); rate = rate_per_sec(&highest_elapsed_time); tracemsg(_a(" Highest period %2lds, %6ld usec (%3d/sec, delta from avg %lds, %6ld us, result %d)"), highest_elapsed_time.tv_sec, highest_elapsed_time.tv_usec, rate, delta.tv_sec, delta.tv_usec, highest_elapsed_time_index); /* Same for lowest. */ subtract(&lowest_elapsed_time, &average_elapsed_time, &delta); rate = rate_per_sec(&lowest_elapsed_time); tracemsg(_a(" Lowest period %2lds, %6ld usec (%3d/sec, delta from avg %lds, %6ld us, result %d)"), lowest_elapsed_time.tv_sec, lowest_elapsed_time.tv_usec, rate, delta.tv_sec, delta.tv_usec, lowest_elapsed_time_index); /* If told to show everything, dump all of the values to text in units of microseconds. * Put commas before and after each value to make this easier to import into excel or * whatever as CSV as there's some sort of timestamp prepended to every printk these days... */ if(show_all) { tracemsg(_a("")); tracemsg(_a(" All periods recorded (usecs):")); for(i = 0; i < num_periods; i++) { subtract(&(timer->times[i].start_time), &(timer->times[i+1].start_time), &elapsed_time); tracemsg(",%ld,\n", USECS(elapsed_time)); } } } /* End of rate info. */
point<T,2> interpolate_x( T x ) const { T t = x / base_t::m_vector[0]; if( less_than( x, 0 ) ) { t += 1; } return point<T,2>{ base_t::m_point + t * base_t::m_vector }; }
int main(int argc, char const *argv[]) { int rc; unsigned ii, jj, kk, A, TV, E; unsigned long N; char *lineptr; size_t nbytes; ssize_t bytes_read; lineptr = NULL; nbytes = 0; bytes_read = 0; FILE * stream; /*read first line with unknown length of data*/ errno = 0; bytes_read = getline(&lineptr, &nbytes, stdin); if(bytes_read < 0) { /*this checks for when there is only EOF */ perror("reached end of input stream before expected"); exit(-1); } stream = fmemopen(lineptr, bytes_read, "r"); Vector(unsigned)* _target_set_bits; _target_set_bits = newVector(unsigned); NULL_CHECK(_target_set_bits, "failed to allocate new vector"); N = 0; TV = 0; for(;;) { rc = fscanf(stream," %u ", &A); if(rc != 1) { if(feof(stream)) break; if(ferror(stream)) { perror("error reading input stream"); exit(-1); } break; } /*A should be 1 or zero*/ assert(A < 2); /*this stores K as little endian, position 0 is represented by the low bit*/ vector_push_back(unsigned, _target_set_bits, A); ++N; } fclose(stream); free(lineptr); lineptr = NULL; /*use an array for programming convience to represent which * bits of the target value are set*/ unsigned* target_bits; target_bits = (unsigned*)malloc(N*sizeof(unsigned)); NULL_CHECK(target_bits, "failed to allocate target bit buffer"); for(ii = 0; ii < N; ++ii) { if( _target_set_bits->items[ii] == 1) { target_bits[ii] = 1; } else { target_bits[ii] = 0; } } /* Allocate a buffer to record which indices are set * for use in reading in each mask below*/ unsigned* index_buffer, *cursor, * bit_counts; index_buffer = (unsigned*)malloc(N* sizeof(unsigned)); NULL_CHECK(index_buffer, "failed to allocate a buffer") errno = 0; bit_counts = (unsigned*)calloc(N, sizeof(unsigned)); NULL_CHECK(bit_counts, "failed to allocate metadata buffer") /* * Store each masks, we will only read up to N masks from the file, * this is the defined input format */ struct MaskData* mask_buffer; /*initialize each mask with a length of zero*/ mask_buffer = (struct MaskData*)calloc(N, sizeof(struct MaskData)); NULL_CHECK(mask_buffer, "failed to allocate buffer for all masks"); /*keep track total bits set for building the metadata offset table*/ for(ii = 0; ii < N; ++ii) { lineptr = NULL; nbytes = 0; errno = 0; bytes_read = getline(&lineptr, &nbytes, stdin); if(bytes_read < 0) { /*this checks for when there is only EOF */ perror("reached end of input stream before expected"); exit(-1); } if(bytes_read == 1) { if(*lineptr == '\n'){ continue; } else { perror("unexpected input data"); exit(-1); } } stream = fmemopen(lineptr, bytes_read, "r"); char colon; /*make sure that each line begins with an index and then * the colon character delimeter follows*/ rc = fscanf(stream, "%u %c", &E, &colon); /*check if input line has correct format*/ if(colon == ':') { /*for the masks to make sense in the context of our problem * where touching an egg at index I flips the bits given by XOR * the mask with the current state vector, we cannot touch an egg * outside of the the range*/ assert(E < N); /*TODO: make strong check that the new mask does not overwrite * any of the previous masks, aka there should no be two rules * for touching the same egg */ mask_buffer[ii].key = E; /*walk over buffer starting from begining*/ cursor = index_buffer; /*the below code depends on these two fields being zeroed*/ mask_buffer[ii].len = 0; mask_buffer[ii].value = 0; for(;;) { rc = fscanf(stream," %u ", &A); if(rc == EOF) { break; } /*value will be pretty useless if there are more than 64 masks*/ mask_buffer[ii].value |= 1<<A; mask_buffer[ii].len++; /*store and advance buffer*/ *cursor++ = A; bit_counts[A]++; } } fclose(stream); /*check to see if pattern already exists*/ /*allocate an appropriate size buffer for the MaskData * instance and copy valid contents of buff into*/ mask_buffer[ii].bits = (unsigned*)malloc(mask_buffer[ii].len * sizeof(unsigned)); NULL_CHECK(mask_buffer[ii].bits, "failed to allocate mask bit count"); for(kk = 0; kk < mask_buffer[ii].len; ++kk) { mask_buffer[ii].bits[kk] = index_buffer[kk]; } } /*we wont use index_buffer again*/ free(index_buffer); index_buffer = NULL; /*======END OF READING INPUT==========*/ struct MetaData meta; MetaData_init(&meta, N, bit_counts, mask_buffer, N); /* print_MetaData(&meta); */ /*allocate a buffer that marks masks as available*/ char* possible; possible = (char*)malloc((N+1) * sizeof(char)); NULL_CHECK(possible, "failed to allocate buffer for flags"); memset(possible, MASK_ACTIVE, N*sizeof(char)); /*null terminate so we can print it directly for debugging*/ possible[N] = '\0'; /*remove duplicates by creating a shadow pointer buffer and sorting * those pointers*/ unsigned (*less)(struct MaskData**, struct MaskData**); less = &less_than; struct MaskData** shadow; shadow = (struct MaskData**)malloc(N * sizeof(struct MaskData*)); NULL_CHECK(shadow, "failed to allocate shadow buffer"); for(ii = 0; ii < N; ++ii) { shadow[ii] = mask_buffer+ii; } yam_quicksort(MaskData_ptr,shadow, 0, N-1, less); for(ii = 1; ii < N; ii++) { if((less_than(&shadow[ii-1], &shadow[ii]) == 0) && \ (less_than(&shadow[ii], &shadow[ii-1]) == 0)) { possible[shadow[ii]->key] = MASK_INACTIVE; /*remove the bit counts*/ for(jj = 0; jj < shadow[ii]->len; ++jj) { --bit_counts[shadow[ii]->bits[jj]]; } } } printf("After eliminating duplicates\n%s\n\n", possible); /*remove any masks that are the only one to set the bit * only masks that are possible to be removed */ unsigned removed; MetaData_iterator md_iter, end; do { removed = 0; for(ii = 0; ii < N; ++ii) { if(bit_counts[ii] == 1) { /*find mask which sets this bit: * so first get the bin of all masks that set this * and find the one that is marked active*/ md_iter = MetaData_row_start(&meta, ii); end = MetaData_row_end(&meta, ii); for(; md_iter != end; ++md_iter) { if(possible[*md_iter] != MASK_INACTIVE) { break; } } if(possible[*md_iter] == MASK_KEEP) { continue; } if(target_bits[ii] == 1){ possible[ii] = MASK_KEEP; ++removed; } else { possible[ii] = MASK_INACTIVE; /*change the bit counts in metadata to reflect change*/ for(jj = 0; jj < mask_buffer[*md_iter].len; ++jj) { --bit_counts[mask_buffer[*md_iter].bits[jj]]; } ++removed; } } } } while(removed != 0); printf("After first pass filtering:\n\t%s", possible); printf("\n"); return 0; }
// Called from "findThreshold" and "applyOptimalsplit" static void splitSamples ( const std::vector<Sample*> &samples, const std::vector<IntIndex> &val_set, std::vector< std::vector<Sample*> > &sets, int thresh, int margin ) { // Search largest value such that value < t std::vector<IntIndex>::const_iterator it_first, it_second; it_first = lower_bound(val_set.begin(), val_set.end(), thresh-margin, less_than()); if (margin == 0) it_second = it_first; else it_second = lower_bound(val_set.begin(), val_set.end(), thresh+margin, less_than()); // Split training samples into two different sets A, B according to threshold t if (it_first == it_second) { // No intersection between the two thresholds sets.resize(2); sets[0].resize(it_first - val_set.begin()); sets[1].resize(samples.size() - sets[0].size()); std::vector<IntIndex>::const_iterator it; typename std::vector<Sample*>::iterator it_sample; it = val_set.begin(); for (it_sample = sets[0].begin(); it_sample < sets[0].end(); ++it_sample, ++it) (*it_sample) = samples[it->second]; it = val_set.begin() + sets[0].size(); for (it_sample = sets[1].begin(); it_sample < sets[1].end(); ++it_sample, ++it) (*it_sample) = samples[it->second]; CV_Assert((sets[0].size() + sets[1].size()) == samples.size()); } else { sets.resize(3); sets[0].resize(it_first - val_set.begin()); sets[1].resize(it_second - it_first); sets[2].resize(val_set.end() - it_second); std::vector<IntIndex>::const_iterator it; typename std::vector<Sample*>::iterator it_sample; it = val_set.begin(); for (it_sample = sets[0].begin(); it_sample < sets[0].end(); ++it_sample, ++it) (*it_sample) = samples[it->second]; it = val_set.begin() + sets[0].size(); for (it_sample = sets[1].begin(); it_sample < sets[1].end(); ++it_sample, ++it) (*it_sample) = samples[it->second]; it = val_set.begin() + sets[0].size() + sets[1].size(); for (it_sample = sets[2].begin(); it_sample < sets[2].end(); ++it_sample, ++it) (*it_sample) = samples[it->second]; CV_Assert((sets[0].size() + sets[1].size() + sets[2].size()) == samples.size()); } };
// positive starts from base of direction // negative starts from end of direction point<T,D> interpolate( T distance ) const { T t = distance / base_t::m_vector.length( ); if( less_than( distance, 0 ) ) { t += 1; } return point<T,D>{ base_t::m_point + t * base_t::m_vector }; }
point<T,2> interpolate_y( T y ) const { T t = y / base_t::m_vector[1]; if( less_than( y, 0 ) ) { t += 1; } return point<T,2>{ base_t::m_point + t * base_t::m_vector }; }