Ejemplo n.º 1
0
void testNoDefaultConstructor() {
    Deque<NoDefaultConstructor> deque;

    deque.reserve(8);

    for(int i = 0; i < 5; ++i) {
        deque.push_back(NoDefaultConstructor(i));
        SINVARIANT(deque.back().v == i);
        SINVARIANT(deque.size() == static_cast<size_t>(i + 1));
        INVARIANT(NoDefaultConstructor::ndc_count == i + 1, format("%d != %d + 1")
                  % NoDefaultConstructor::ndc_count % i);
    }

    for(int i = 0; i < 5; ++i) {
        SINVARIANT(deque.front().v == i);
        deque.pop_front();
        deque.push_back(NoDefaultConstructor(i));
        SINVARIANT(deque.back().v == i);
        SINVARIANT(deque.size() == 5);
        SINVARIANT(NoDefaultConstructor::ndc_count == 5);
    }

    for(int i = 0; i < 5; ++i) {
        SINVARIANT(deque.front().v == i);
        deque.pop_front();
        SINVARIANT(deque.size() == static_cast<size_t>(4 - i));
        SINVARIANT(NoDefaultConstructor::ndc_count == 4 - i);
    }
    SINVARIANT(NoDefaultConstructor::ndc_count == 0);
}
Ejemplo n.º 2
0
void testPushBack() {
    Deque<int> deque;

    SINVARIANT(deque.empty());
    deque.reserve(8);
    SINVARIANT(deque.empty() && deque.capacity() == 8);
    for(int i = 0; i < 5; ++i) {
        deque.push_back(i);
        SINVARIANT(deque.back() == i);
        SINVARIANT(deque.size() == static_cast<size_t>(i + 1));
    }

    for(int i = 0; i < 5; ++i) {
        SINVARIANT(deque.at(i) == i);
    }

    for(int i = 0; i < 5; ++i) {
        SINVARIANT(deque.front() == i);
        deque.pop_front();
        deque.push_back(i);
        SINVARIANT(deque.back() == i);
        SINVARIANT(deque.size() == 5);
    }

    for(int i = 0; i < 5; ++i) {
        SINVARIANT(deque.at(i) == i);
    }

    {
        Deque<int>::iterator i = deque.begin();
        int j = 0;
        while(i != deque.end()) {
            INVARIANT(*i == j, format("%d != %d") % *i % j);
            ++i;
            ++j;
        }
    }

    vector<int> avec;
    for(int i = 5; i < 10; ++i) {
        avec.push_back(i);
    }
    deque.push_back(avec);

    for(int i = 0; i < 10; ++i) {
        SINVARIANT(deque.front() == i);
        deque.pop_front();
    }
    SINVARIANT(deque.empty());
}
Ejemplo n.º 3
0
 void update(int64_t packet_raw, int packet_size, const Int64TimeField &packet_at,
             const string &filename) {
     LintelLogDebug("IPRolling::packet", format("UPD %d %d") % packet_raw % packet_size);
     if (!packets_in_flight.empty()) {
         int64_t cur_back_ts_raw = packets_in_flight.back().timestamp_raw;
         INVARIANT(packet_raw >= cur_back_ts_raw,
                   format("out of order by %.4fs in %s; %d < %d") 
                   % packet_at.rawToDoubleSeconds(cur_back_ts_raw - packet_raw) % filename
                   % packet_raw % cur_back_ts_raw);
     }
     while ((packet_raw - cur_time_raw) > interval_width_raw) {
         // update statistics for the interval from cur_time to cur_time + interval_width
         // all packets in p_i_f must have been received in that interval
         double bw = cur_bytes_in_queue * MiB_per_second_convert;
         MiB_per_second.add(bw);
         double pps = packets_in_flight.size() * kpackets_per_second_convert;
         kpackets_per_second.add(pps);
         LintelLogDebug("IPRolling::detail", format("[%d..%d[: %.0f, %d -> %.6g %.6g")
                        % cur_time_raw % (cur_time_raw + update_step_raw)
                        % cur_bytes_in_queue % packets_in_flight.size() % bw % pps);
         cur_time_raw += update_step_raw;
         while (! packets_in_flight.empty() &&
               packets_in_flight.front().timestamp_raw < cur_time_raw) {
             cur_bytes_in_queue -= packets_in_flight.front().packetsize;
             packets_in_flight.pop_front();
         }
     }
     packets_in_flight.push_back(packetTimeSize(packet_raw, packet_size));
     cur_bytes_in_queue += packet_size;
 }
Ejemplo n.º 4
0
int main(){

	int n;
	cin>>n;
	string s;
	getline(cin,s);
	Deque dteams;
	Map mteams;
	bool first=true;
	for(int i=0;i<n;i++){
		dteams.clear(); mteams.clear();
		if(first) first=false; else cout<<endl;
		getline(cin,s);
		cout<<s<<endl;
		int numTeam,numMatch;
		cin>>numTeam;
		getline(cin,s);
		for(int j=0;j<numTeam;j++){
			getline(cin,s);
			dteams.push_back(Team(s));
			mteams.insert(make_pair(s,&(dteams.back())));
		}
		cin>>numMatch;
		getline(cin,s);
		for(int j=0;j<numMatch;j++){
			Team* t1,*t2;
			int score1,score2;
			getline(cin,s,'#');
			t1=mteams.find(s)->second;
			cin>>score1;
			getline(cin,s,'@');
			cin>>score2;
			getline(cin,s,'#');
			getline(cin,s);
			t2=mteams.find(s)->second;
			t1->goalScored+=score1;
			t1->goalAgainst+=score2;
			t2->goalScored+=score2;
			t2->goalAgainst+=score1;
			if(score1>score2){t1->win++;t2->lose++;}
			else if(score1<score2){t1->lose++;t2->win++;}
			else {t1->tie++;t2->tie++;}
		}
		sort(dteams.begin(),dteams.end());
		int rank=1;
		for(Deque::iterator it=dteams.begin();it!=dteams.end();it++,rank++){
			cout<<rank<<") "<<it->name<<" "<<it->point()<<"p, "<<it->total()<<
				"g ("<<it->win<<"-"<<it->tie<<"-"<<it->lose<<"), "<<
				it->goalDiff()<<"gd ("<<it->goalScored<<"-"<<it->goalAgainst<<")"<<endl;
		}
		//cout<<endl;
	}	
}
Ejemplo n.º 5
0
//--------------------Linear solution----------------------
void minimumValues(int v[], int k) {
	Deque<int> d = Deque<int>(); // In the queue I store positions, no elements

	for (int i = 0; i < N; i++)
	{
		if (i >= k) // Until we have not consider a subarray, we can not print the minimums.
			cout << v[d.front()] << " ";

		while (!d.empty() && v[d.back()] >= v[i]) { // While the queue is not empty and the element I want to insert is
												   // greater or equal than the last one I extract the last element
			d.pop_back();
		}
		d.push_back(i); // I insert the new element

		if (d.front() <= i - k) //This remove the minimum once is out of the subarray
			d.pop_front();
	}
	cout << v[d.front()] << endl;
}
Ejemplo n.º 6
0
int main()
{

#if 0
  CString cstr;

  cstr.init("abc");
  cstr.print();
  printf("\n");

  Deque<CString> deque;
  deque.init();

  deque.push_back(cstr);

  CString ps;
  ps = "102";

  cstr.init("def");
  deque.push_back(cstr);

  deque.print();
  deque.back(0, ps);
  ps.print();
  printf("\n");
#endif

#if 1
  setlocale(LC_ALL, "");

  initscr();
  keypad(stdscr,true);
  curs_set(0);
  // KEY_UP
  // KEY_DOWN

  //Deque<string> deque;
  Deque<CString> deque;

  deque.init();

  move(0,0);
  int fail_time=1;
  while(1)
  {
    char str[128];
    getstr(str);
    move(0,0);
    //std::string s(str);
    CString s;
    s.init(str);
    if (deque.push_back(s) == false)
    {
      mvprintw(20, 0, "push back fail ## %d", fail_time++);
      deque.pop_front();
      deque.push_back(s);
    }

    deque.print();

    int index=0;
    noecho();
    //string ps;
    CString ps;
    while(1)
    {
      mvprintw(17, 0, "index: %d", index);
      refresh();
      int ch = getch();
      switch(ch)
      {
        case KEY_UP:
        {
          ++index;
          if (deque.back(index, ps) == true)
          {
            mvprintw(0, 15, "%s", ps.c_str());
          }
          else
            --index;
          break;
        }
        case KEY_DOWN:
        {
          --index;
          if (deque.back(index, ps) == true)
          {
            mvprintw(0, 15, "%s", ps.c_str());
          }
          else
          {
            //mvprintw(17, 5, "back fail");
            ++index;
          }
          break;
        }
        case 'q':
        {
          goto end;
        }
        default:
        {
          ungetch(ch);
          move(0, 0);
          goto outer;
          break;
        }
      }
      mvprintw(17, 0, "index: %d", index);
      refresh();
    }
    outer:
    echo();
  }
end:
  endwin();
#endif
  return 0;
}