Beispiel #1
0
int main( 	int argc,
			char** argv)
{
	// Initialise the C-library random mechanism by seeding it with the current time
	// See http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
	std::srand( std::time( NULL));

	// For more info on exception handling: http://www.cplusplus.com/doc/tutorial/exceptions/
	// and http://www.parashift.com/c++-faq/exceptions.html
	try
	{
		GameContext game1("1");
		boost::thread table1([&game1] {game1.play();});

		GameContext game2("2");
		boost::thread table2([&game2] {game2.play();});

		GameContext game3("3");
		boost::thread table3([&game3] {game3.play();});


		table1.join();
		table2.join();
		table3.join();
	}
	catch (std::exception& e)
	{
		std::cout << "Exception caught: " << e.what() << std::endl;
	}
	catch (...)
	{
		std::cout << "Unknown exception caught: " << std::endl;
	}
	return 0;
}
Beispiel #2
0
TEST_F(HashTableTest, AssignmentOperatorWorksOnCorrectSizedMap)
{
    capu::HashTable<capu::uint32_t, capu::uint32_t> table(4, true);

    table.put(1, 2);
    table.put(3, 4);
    table.put(5, 6);

    capu::HashTable<capu::uint32_t, capu::uint32_t> table2(4, false); // not resizable

    table2 = table;

    ASSERT_EQ(3u, table2.count()); // modification occurred
}
Beispiel #3
0
TEST_F(HashTableTest, AssignmentOperatorDoesNothingOnWrongSizedMap)
{
    capu::HashTable<capu::uint32_t, capu::uint32_t> table(5, true);

    table.put(1, 2);
    table.put(3, 4);
    table.put(5, 6);

    capu::HashTable<capu::uint32_t, capu::uint32_t> table2(4, false); // not resizable

    table2 = table;

    ASSERT_EQ(0u, table2.count()); // no modification occurred
}
Beispiel #4
0
int
main(int argc, char** argv){

 
 if(argc != 5){
     Usage(argc,argv);
     return 1;
 }
	 
 // amount of mask attenuation (dB)
 float amount = (float)pow(10, atof(argv[4])/20.f);
 
 // masking file
 SndWave inmask(argv[2], READ);
 // input file
 SndWave infile(argv[1], READ);

 // Hamming window
 HammingTable window(DEF_FFTSIZE, 0.54f);
 // masking table
 PVTable table2(DEF_FFTSIZE,&inmask,&window, 0.f, 1.f);

 // input signal
 SndIn  in(&infile);
 // PV analysis
 PVA   anal(&window, &in, 0.6f);
 // Masking
 PVMask mask(amount, &table2, &anal);
 // PV synthesis
 PVS   synth(&window, &mask);
 // output file
 SndWave  output(argv[3], OVERWRITE, 1);
 output.SetOutput(1, &synth);
 // processing loop
 while(!infile.Eof()){ 

   infile.Read();
   in.DoProcess();
   anal.DoProcess();
   mask.DoProcess();
   synth.DoProcess();
   output.Write();  
   
}

return 0;

}
Beispiel #5
0
static void test_emptytable(skiatest::Reporter* reporter) {
    SkAutoTUnref<SkDataTable> table0(SkDataTable::NewEmpty());
    SkAutoTUnref<SkDataTable> table1(SkDataTable::NewCopyArrays(NULL, NULL, 0));
    SkAutoTUnref<SkDataTable> table2(SkDataTable::NewCopyArray(NULL, 0, 0));
    SkAutoTUnref<SkDataTable> table3(SkDataTable::NewArrayProc(NULL, 0, 0,
                                                               NULL, NULL));

    test_datatable_is_empty(reporter, table0);
    test_datatable_is_empty(reporter, table1);
    test_datatable_is_empty(reporter, table2);
    test_datatable_is_empty(reporter, table3);

    test_is_equal(reporter, table0, table1);
    test_is_equal(reporter, table0, table2);
    test_is_equal(reporter, table0, table3);
}
Beispiel #6
0
int main()
{
    /*/ Create 26 Items to store in the Hash Table.
    Item * A = new Item {"Apple", NULL};
    Item * B = new Item {"Banana", NULL};
    Item * C = new Item {"Caterpillar", NULL};
    Item * D = new Item {"Dog", NULL};
    Item * E = new Item {"Elephant", NULL};
    Item * F = new Item {"Fedora", NULL};
    Item * G = new Item {"Goosebumps", NULL};
    Item * H = new Item {"House", NULL};
    Item * I = new Item {"Insects", NULL};
    Item * J = new Item {"Jam", NULL};
    Item * K = new Item {"Kite", NULL};
    Item * L = new Item {"Limestone", NULL};
    Item * M = new Item {"Mountaineering", NULL};
    Item * N = new Item {"Night", NULL};
    Item * O = new Item {"Open Sesame", NULL};
    Item * P = new Item {"Potatoes", NULL};
    Item * Q = new Item {"Quantum Mechanics", NULL};
    Item * R = new Item {"Rrrrrrrrrrawr", NULL};
    Item * S = new Item {"Snakes", NULL};
    Item * T = new Item {"Tizzy Tube", NULL};
    Item * U = new Item {"Underworld", NULL};
    Item * V = new Item {"Volcanic Ash", NULL};
    Item * W = new Item {"Who When What Why", NULL};
    Item * X = new Item {"XXX", NULL};
    Item * Y = new Item {"Yellow", NULL};
    Item * Z = new Item {"Zest of Lemon", NULL};
    
    // Create a Hash Table of 13 Linked List elements.
    HashTable table;
    
    // Add 3 Items to Hash Table.
    table.insertItem(A);
    table.insertItem(B);
    table.insertItem(C);
    table.printTable();
    table.printHistogram();
    
    // Remove one item from Hash Table.
    table.removeItem("Apple");
    table.printTable();
    table.printHistogram();
    
    
    // Add 23 items to Hash Table.
    table.insertItem(D);
    table.insertItem(E);
    table.insertItem(F);
    table.insertItem(G);
    table.insertItem(H);
    table.insertItem(I);
    table.insertItem(J);
    table.insertItem(K);
    table.insertItem(L);
    table.insertItem(M);
    table.insertItem(N);
    table.insertItem(O);
    table.insertItem(P);
    table.insertItem(Q);
    table.insertItem(R);
    table.insertItem(S);
    table.insertItem(T);
    table.insertItem(U);
    table.insertItem(V);
    table.insertItem(W);
    table.insertItem(X);
    table.insertItem(Y);
    table.insertItem(Z);
    table.printTable();
    table.printHistogram();
    
    // Look up an Item in the Hash Table.
    Item * result = table.getItemByKey("Snakes");
    cout << result -> key << endl;
	*/
	int N=20, B=97;
	HashTable table1(B), table2(B);
	for (int i = 0; i < N;i++){
		char s[10];
		gen_random(s, 10);

		Item * A = new Item{ s, NULL };
		table1.insertItem(A);
		table2.insertItem1(A);
	}
//	table1.printTable();
//	table1.printHistogram();
	cout<< table1.countHistogram(N) << endl;
//	table2.printTable();
//	table2.printHistogram();
	cout << table2.countHistogram(N) << endl;

    return 0;
}
Beispiel #7
0
void test_perm(int N, int NN, unsigned int M)
{
   int j; unsigned long i; double sum2 = 0;
   Array<long> count(0,NN-1);   // count number of times each ball is drawn
   Array<int> last(0,NN-1);     // details of last draw
   long consec = 0;             // count number of consecutive balls
   last = 0; count = 0;         // set the arrays to zero


   cout << endl;
   cout << "Size of urn = " << NN << ";  ";
   cout << "no. of balls = " << N << ";  ";
   cout << "no. of trials = " << M << endl;
   cout << BaseTest::Header << endl;   
   if (N > NN) { Throw(Runtime_error("N > NN")); }
   long double S0 = 0.0;             // sum of ball numbers
   long double S1 = 0.0;             // sum of ball * draw
   double centre = (NN+1) / 2.0;     // average ball number
   double avj = (N+1)/2.0;           // average draw number
   Array2<int> table1(0,NN-1,0,N-1);     // times ball i occurs at draw j
   Array2<int> table2(0,NN-1,0,NN-1);    // times ball i and ball j both occur
   table1 = 0; table2 = 0;           // clear the tables
   long turnings = 0;                // number of turning points
   RandomPermutation RP;             // reset the Urn
   Array<int> Draw(0,N-1);           // for the draw


   for (i=1; i<=M; i++)              // iterate over total number of trials
   {
      int last_ball = -1;
      RP.Next(NN,N,Draw.base(),1);   // do the draw
      Array<int> check(0,NN-1); check = 0;  // for checking no repeats
      for (j = 1; j <= N; j++)       // N balls in draw
      {
	 int x = Draw(j-1);
	 if (x<1 || x>NN) Throw(Runtime_error("Invalid number"));
	 check(x-1)++; count(x-1)++; sum2 += last(x-1);
	 if (x == last_ball+1 || x == last_ball-1) consec++;
	 last_ball = x;
	 double xc = x - centre;
	 S0 += xc; S1 += xc * (j-avj);
	 table1(x-1,j-1)++;
      }
      for (j=0; j<NN; j++)
      {
	 if (check(j) > 1) Throw(Runtime_error("Repeated number"));
	 last(j) = check(j);
	 if (check(j))              // increment table of pairs
	 {
	    for (int k = j+1; k<NN; k++) if (check(k)) table2(j,k)++;
	 }
      }

      // count number of turnings
      for (j = 2; j < N; j++)
      {
         long last = Draw(j-2);
         long current = Draw(j-1);
         long next = Draw(j);
         if ( (last < current && next < current) ||
            (last > current && next > current) ) turnings++;
      }

   }

   // do the tests
   double sum1 = 0.0; double d = M * (N / (double)NN);
   for (j=0; j<NN; j++) { sum1 += square(count(j) - d); }

   if (N < NN)
   {
      sum1 /= d;
      double sd = (NN - N) * sqrt( 2 * (M - 1)  / (M * ((double)(NN - 1))) );
      NormalTestTwoSided freq_test("Ball freq.", sum1, NN - N, square(sd));
      freq_test.DoTest();

      NormalTestTwoSided repeats_test("Repeats", sum2, (M - 1) * (N * N / (double)NN),
         square((double)(N * (NN - N)) / (double)NN) * (M - 1) / (NN - 1) );
      repeats_test.DoTest();

      double V0 = ((double)N * (NN+1.0) * (NN-N))/12.0;
      NormalTestTwoSided average_test("Average", S0, 0, M * V0);
      average_test.DoTest();
   }

   if (N > 1)
   {
      double V1 = (double)N * (N*N - 1) * NN * (NN + 1) / 144.0;
      NormalTestTwoSided ball_times_draw("Ball * draw", S1, 0, M * V1);
      ball_times_draw.DoTest();

      double e = 2 * M * (N - 1) / (double)NN;
				   // expected # of consecutive balls
      NormalTestTwoSided consec_test("Consec balls", consec, e, e);
      consec_test.DoTest();
   }

   if (N > 1)
   {
      // balls vs draws
      double CS = 0; double ev = (double)M / NN;
      for (int i = 0; i < NN; i++) for (int j = 0; j < N; j++)
	 CS += square(table1(i,j)-ev);
      CS /= ev; ev = N * (NN - 1);
      double v =  2.0 * N * (M - 1) * (N - (2 - NN) * NN) / M / (NN - 1);
      NormalTestTwoSided ball_draw("Ball vs draw", CS, ev, v);
      ball_draw.DoTest();
   }

   if (N>1 && N < NN)
   {
      // pairs of balls
      double CS = 0; double ev = (double)M*N*(N-1)/ NN / (NN-1);
      for (int i = 0; i < NN; i++) for (int j = i+1; j < NN; j++)
	 CS += square(table2(i,j)-ev);
      CS /= ev; ev = (NN-N)*(NN+N-1)/2.0;
      double v =  (double)(M-1)*square(NN-N)*(-3+6*N-3.0*N*N+2*NN-6.0*N*NN+
	 2.0*N*N*NN+NN*NN)/(M*(NN-2)*(NN-3));
      NormalTestTwoSided pairs_test("Pairs table", CS, ev, v);
      pairs_test.DoTest();
   }

   if (N > 2)
   {
      double V2 = (16.0 * N - 29.0) / 90.0;
      NormalTestTwoSided runs_test("Runs test", turnings,
         (double)M * (N-2) / 1.5, M * V2);
      runs_test.DoTest();
   }

}
Beispiel #8
0
Status Operators::SMJ(const string& result,           // Output relation name
                      const int projCnt,              // Number of attributes in the projection
                      const AttrDesc attrDescArray[], // Projection list (as AttrDesc)
                      const AttrDesc& attrDesc1,      // The left attribute in the join predicate
                      const Operator op,              // Predicate operator
                      const AttrDesc& attrDesc2,      // The left attribute in the join predicate
                      const int reclen)               // The length of a tuple in the result relation
{
    cout << "Algorithm: SM Join" << endl;

    /* Your solution goes here */
    Status status;
    
    RID rid_old1;
    Record rec_old1;
    int maxitem1;
    status = get_maxItems(string(attrDesc1.relName), maxitem1);
    if (status != OK) return status;
    SortedFile table1 (attrDesc1.relName, attrDesc1.attrOffset, attrDesc1.attrLen,
            (Datatype)attrDesc1.attrType, maxitem1, status);
    if (status != OK) return status;

    RID rid_old2;
    Record rec_old2;
    int maxitem2;
    status = get_maxItems(string(attrDesc2.relName), maxitem2);
    if (status != OK) return status;
    SortedFile table2 (attrDesc2.relName, attrDesc2.attrOffset, attrDesc2.attrLen,
            (Datatype)attrDesc2.attrType, maxitem2, status);
    if (status != OK) return status;

    HeapFile new_hf(result, status);
    if (status != OK) return status;
    RID rid_new;
    Record rec_new;
    

    bool matched = false;
    bool end_loop = false;
    Record prev_rec1;
    int diff; 
    status = table1.next(rec_old1);
    if (status == FILEEOF) return OK;
    else if (status != OK) return status;
    status = table2.next(rec_old2);
    if (status == FILEEOF) return OK;
    else if (status != OK) return status;

    //initial new_rec
    rec_new.length = reclen;
    rec_new.data = operator new (reclen);

    while (!end_loop){
        matched = false;
        diff = matchRec(rec_old1, rec_old2, attrDesc1, attrDesc2); 
        if (diff < 0){
            //next tuple in rel1
            memcpy (&prev_rec1, &rec_old1, sizeof(Record));  //copy the previous rec in rel1
            status = table1.next(rec_old1);
            if (status == FILEEOF){
                end_loop = true;
            } else if (status != OK){
                operator delete (rec_new.data);
                return status;
            }
            if (matchRec(prev_rec1, rec_old1, attrDesc1, attrDesc1)==0){  //is equal
                table2.gotoMark();
                table2.next(rec_old2);
            } 
        } else if (diff > 0){
            //next tuple in rel2
            status = table2.next(rec_old2);
            if (status == FILEEOF){
                end_loop = true;
            } else if (status != OK){
                operator delete (rec_new.data);
                return status;
            }
        }
        while ((diff == 0)&&(!end_loop)){ //match
            if (!matched){
                table2.setMark();
                matched = true;
            }
            //insert new tuple
            int offset = 0;
            for (int i=0; i<projCnt; i++){
                if (!strcmp(attrDescArray[i].relName, attrDesc1.relName)){  //in rel1
                    memcpy(rec_new.data+offset, rec_old1.data+attrDescArray[i].attrOffset,
                            attrDescArray[i].attrLen);
                    offset += attrDescArray[i].attrLen;
                } else {
                    memcpy(rec_new.data+offset, rec_old2.data+attrDescArray[i].attrOffset,
                            attrDescArray[i].attrLen);
                    offset += attrDescArray[i].attrLen;
                }
            }
            status = new_hf.insertRecord(rec_new, rid_new);
            if (status != OK) {
                operator delete (rec_new.data);
                return status;
            }
            //get next tuple
            status = table2.next(rec_old2);
            if (status == FILEEOF){ //if end of inner loop
                memcpy (&prev_rec1, &rec_old1, sizeof(Record));  //copy the previous rec in rel1
                status  = table1.next(rec_old1);
                if (status == FILEEOF){
                    end_loop = true;
                } else if (status != OK){
                    operator delete (rec_new.data);
                    return status;
                }
                if (matchRec(prev_rec1, rec_old1, attrDesc1, attrDesc1)==0){  //is equal
                    table2.gotoMark();
                    table2.next(rec_old2);
                } else {
                    end_loop = true;
                }
            } else if (status != OK){
                operator delete (rec_new.data);
                return status;
            }
            diff = matchRec(rec_old1, rec_old2, attrDesc1, attrDesc2);
        }
    }
    operator delete (rec_new.data);
    return OK;
}
Beispiel #9
0
Status Operators::INL(const string& result,           // Name of the output relation
                      const int projCnt,              // Number of attributes in the projection
                      const AttrDesc attrDescArray[], // The projection list (as AttrDesc)
                      const AttrDesc& attrDesc1,      // The left attribute in the join predicate
                      const Operator op,              // Predicate operator
                      const AttrDesc& attrDesc2,      // The left attribute in the join predicate
                      const int reclen)               // Length of a tuple in the output relation
{
    cout << "Algorithm: Indexed NL Join" << endl;
    /* Your solution goes here */
    Status status;
    RID rid_old1;
    Record rec_old1;
    HeapFileScan table1(attrDesc1.relName, status);
    if (status != OK) return status;

    RID rid_old2;
    Record rec_old2;
    Index index(attrDesc2.relName, attrDesc2.attrOffset, attrDesc2.attrLen, (Datatype)
            attrDesc2.attrType, 0, status);
    if (status != OK) return status;
    HeapFileScan table2(attrDesc2.relName, status);
    if (status != OK) return status;

    HeapFile new_hf(result, status);
    if (status != OK) return status;
    
    RID rid_new;
    Record rec_new;
    rec_new.length = reclen;
    rec_new.data = operator new (reclen);

    int i = attrDesc1.attrLen;
    void* value = operator new (i);
    while (table1.scanNext(rid_old1, rec_old1) == OK){
        memcpy(value, rec_old1.data+attrDesc1.attrOffset, attrDesc1.attrLen); //get value
        index.startScan(value);
        while (index.scanNext(rid_old2) == OK){
            status = table2.getRandomRecord(rid_old2, rec_old2);
            if (status != OK) {
                operator delete (value);
                operator delete (rec_new.data);   
                return status;
            }
            //fill in rec_new.data
            int offset = 0;
            for (int i=0; i<projCnt; i++){
                if (!strcmp(attrDescArray[i].relName, attrDesc1.relName)){  //in rel1
                    memcpy(rec_new.data+offset, rec_old1.data+attrDescArray[i].attrOffset,
                            attrDescArray[i].attrLen);
                    offset += attrDescArray[i].attrLen;
                } else {
                    memcpy(rec_new.data+offset, rec_old2.data+attrDescArray[i].attrOffset,
                            attrDescArray[i].attrLen);
                    offset += attrDescArray[i].attrLen;
                }
            }
            status = new_hf.insertRecord(rec_new, rid_new);
            if (status != OK){
                operator delete (value);
                operator delete (rec_new.data);
                return status;
            }
        }
        index.endScan();
    }
    operator delete (value);
    operator delete (rec_new.data);
    return OK;
}