Пример #1
0
int main()
{
    pair_t intervals[] { {5,8}, {3,4}, {13,20}, {7,10} } ;

    std::unordered_map<int,int> counts ;
    for( const pair_t& interval : intervals ) // O(N)
        increment_counts( counts, interval ) ;

    int max_count = 0 ;
    for( const pair_t& p : counts ) // O(N)
        if( p.second > max_count ) max_count = p.second ;

    std::cout << "[ " ;
    for( const pair_t& p : counts ) // O(N)
        if( p.second == max_count ) std::cout << p.first << ' ' ;
    std::cout << "]\n" ;
}
Пример #2
0
END_TEST


START_TEST(prop_test)
  {
  struct pbsnode        n1;
  struct pbsnode        n2;
  struct pbsnode        n3;
  struct pbsnode        n4;
  struct pbsnode       *rtd;
  int                   rc;
  int                   dummy1;
  int                   dummy2;
  int                   dummy3;
  int                   n1_rtd = 0;
  int                   n2_rtd = 0;
  int                   n3_rtd = 0;
  int                   n4_rtd = 0;
  char                 *feature = (char *)"tom";
  char                 *feature2 = (char *)"bob";
  std::vector<prop>     props;
  std::vector<prop>     props2;

  initialize_node_for_testing(&n1);
  initialize_node_for_testing(&n2);
  initialize_node_for_testing(&n3);
  initialize_node_for_testing(&n4);

  n1.add_property(feature);
  n2.add_property(feature);

  proplist(&feature, props, &dummy1, &dummy2, &dummy3);
  proplist(&feature2, props2, &dummy1, &dummy2, &dummy3);

  initialize_login_holder();
  rc = add_to_login_holder(&n1);
  fail_unless(rc == 0);
  rc = add_to_login_holder(&n2);
  fail_unless(rc == 0);
  rc = add_to_login_holder(&n3);
  fail_unless(rc == 0);
  rc = add_to_login_holder(&n4);
  fail_unless(rc == 0);

  rtd = get_next_login_node(&props);
  increment_counts(&n1, &n2, &n3, &n4, rtd, &n1_rtd, &n2_rtd, &n3_rtd, &n4_rtd);
  rtd = get_next_login_node(&props);
  increment_counts(&n1, &n2, &n3, &n4, rtd, &n1_rtd, &n2_rtd, &n3_rtd, &n4_rtd);
  snprintf(buf, sizeof(buf), "Should have used n1 once but is %d", n1_rtd);
  fail_unless(n1_rtd == 1, buf);
  snprintf(buf, sizeof(buf), "Should have used n2 once but is %d", n2_rtd);
  fail_unless(n2_rtd == 1, buf);
  snprintf(buf, sizeof(buf), "Shouldn't have used n3 but is %d", n3_rtd);
  fail_unless(n3_rtd == 0, buf);
  snprintf(buf, sizeof(buf), "Shouldn't have used n4 but is %d", n4_rtd);
  fail_unless(n4_rtd == 0, buf);

  rtd = get_next_login_node(&props);
  increment_counts(&n1, &n2, &n3, &n4, rtd, &n1_rtd, &n2_rtd, &n3_rtd, &n4_rtd);
  rtd = get_next_login_node(&props);
  increment_counts(&n1, &n2, &n3, &n4, rtd, &n1_rtd, &n2_rtd, &n3_rtd, &n4_rtd);
  fail_unless(n1_rtd == 2, "Should have used n1 twice");
  fail_unless(n2_rtd == 2, "Should have used n2 twice");
  fail_unless(n3_rtd == 0, "Shouldn't have used n3");
  fail_unless(n4_rtd == 0, "Shouldn't have used n4");

  fail_unless(get_next_login_node(&props2) == NULL, "Somehow found a node when none have the property");
  }