예제 #1
0
void HuffmanTree::decode( stringstream & ss, BinaryFileReader & bfile )
{
	TreeNode * current = root;

	while( bfile.hasBits() )
	{
		/**
		 * @todo Your code here!
		 *
		 * This code is reading in all of the bits in the binary file
		 * given. After reading a bit, we go left if the bit was a 0 (or
		 * false), and we go right if the bit was a 1 (or true).
		 *
		 * Special case: if we are at a leaf node, we should "print" its
		 * character to the stringstream (with operator<<, just like cout)
		 * and start traversing from the root node again.
		 */
	
		if( bfile.getNextBit() )
		{
			current = current->right;
		}
		else
		{
			current = current->left;
		}

		if( (current->left == NULL)&&(current->right == NULL) )
		{
			ss << (current->freq).getCharacter();
			current = root;
		}
		
	}
}
예제 #2
0
//-----------------------------------------------------------------------------------
void AnimationMotion::ReadFromFile(const char* filename)
{
    BinaryFileReader reader;
    ASSERT_OR_DIE(reader.Open(filename), "File Open failed!");
    {
        ReadFromStream(reader);
    }
    reader.Close();
}
예제 #3
0
void Users::load(BinaryFileReader& file)
{	
	int size=file.readInt();
	for(int i=0; i<size; i++) {
		User new_user;
		new_user.load(file);
		addUser(new_user);
	}
	std::cout << "Loaded users from '" << file.getFilename() << "'" << std::endl;

}
예제 #4
0
void Faces::load(BinaryFileReader& file)
{
	resize(file.readInt());
	width=file.readInt();
	height=file.readInt();
	vector_size=width*height;
	for (int i=0; i<getSize(); i++) {
		(*this)[i].load(file);
	}
	average_face.load(file);
	std::cout << "Loaded faces from '" << file.getFilename() << "'" << std::endl;

}
예제 #5
0
void HuffmanTree::decode( stringstream & ss, BinaryFileReader & bfile ) {
	TreeNode * current = root;
	while( bfile.hasBits() ) {
		if (bfile.getNextBit()) {
			current = current->right;
		}
		else {
			current = current->left;
		}
		if (current->right == NULL && current->left == NULL) {
			ss << current->freq.getCharacter();
			current = root;
		}
	}
}
void test_string() {
    cout << "Testing encoding string ..." << endl;
    BinaryFileWriter writer;

    if (!writer.open(file_name + "_string")) {
        return;
    }

    int n = 8;
    string a[] = {
        "cháy tiệm may, cả khu phố náo loạn.",
        "Sau vụ cháy nằm ngay khu dân cư và gần chợ",
        "Sau vụ cháy năm ngay trong khu dân cư và đối diện chợ Tân Đình",
        "Nhận được tin báo, lực lượng phòng cháy chữa cháy quận ba",
        "Nhận được tin báo, lực lượng phòng cháy chữa cháy quận ba",
        "sau, vụ hỏa hoạn đã",
        " hoàn toàn. Cơ quan chức",
        "đang làm rõ nguyên nhân và thiệt hai do vụ cháy trên gây ra"
    };

    for (int i = 0; i < n; ++i) {
        writer.write_string(a[i]);
    }
    writer.close();

    BinaryFileReader reader;
    if (!reader.open(file_name + "_string")) {
        return;
    }

    for (int i = 0; i < n; ++i) {
        string s = reader.read_string();
        if (s != a[i]) {
            cerr << "FAILED: " << s << " != " << a[i] << endl;
            return;
        } else {
            if (debug_test_binary_encoding) cout << "OK: " << s << " = " << a[i] << endl;
        }
    }

    cout << "PASSED." << endl;
    reader.close();
}
void test_gamma() {
    cout << "Testing gamma coding ..." << endl;
    BinaryFileWriter writer;

    if (!writer.open(file_name + "_gamma")) {
        return;
    }

    vector<int> a;
    int n = 100;
    srand(unsigned(time(0)));
    for (int i = 0; i < n; ++i) {
        a.push_back(rand() % 1000000 + 1);
    }
    for (int i = 0; i < n; ++i) {
        if (!writer.write_gamma(a[i])) {
            cerr << "FAILED write gamma " << a[i] << endl;
            return;
        }
    }

    writer.close();

    BinaryFileReader reader;
    if (!reader.open(file_name + "_gamma")) {
        return;
    }

    for (int i = 0; i < n; ++i) {
        int x = reader.read_gamma();
        if (x != a[i]) {
            cerr << "FAILED: " << x << " != " << a[i] << endl;
            return;
        } else {
            if (debug_test_binary_encoding) cout << "OK: " << x << " = " << a[i] << endl;
        }
    }

    cout << "PASSED." << endl;
    reader.close();
}
예제 #8
0
파일: embedded_da.cpp 프로젝트: nowlab/DALM
EmbeddedDA::EmbeddedDA(BinaryFileReader &reader, ValueArray &value_array, EmbeddedDA **neighbours, unsigned char order, Logger &logger)
	: value_array(value_array), logger(logger){
	DA::context_size = order-1;
	reader >> daid;
	reader >> datotal;
	da = neighbours;
	reader >> max_index;
	array_size = max_index+1;

	da_array = new DAPair[array_size];
	reader.read_many(da_array, array_size);

	value_id = NULL;
	first_empty_index=0;
	logger << "EmbeddedDA[" << daid << "] da-size: " << array_size << Logger::endi;
}
void test_all() {
    cout << "Testing all ..." << endl;

    int n = 6;
    const string a[] = {"cháy", "năm", "lượng", "nhân", "trên", "Tân Đình"};

    vector<int> numbers;
    vector<string> words;
    vector<int> which;

    int total = 100;

    BinaryFileWriter writer;

    if (!writer.open(file_name + "_all")) {
        return;
    }

    srand(unsigned(time(0)));

    for (int run = 0; run < total; ++run) {
        int t = rand() % 5;
        which.push_back(t);
        if (t == 0) {
            int at = rand() % n;
            words.push_back(a[at]);
            writer.write_string(a[at]);
        } else {
            int number = rand() % 1000000 + 1;
            numbers.push_back(number);
            if (t == 1) writer.write_unary(number);
            else if (t == 2) writer.write_gamma(number);
            else if (t == 3) writer.write_delta(number);
            else writer.write_omega(number);
        }
    }


    writer.close();

    BinaryFileReader reader;
    if (!reader.open(file_name + "_all")) {
        return;
    }

    int inumber = 0, iword = 0;
    for (int run = 0; run < total; ++run) {
        int t = which[run];
        if (t == 0) {
            string s = reader.read_string();
            string t = words[iword++];

            if (s != t) {
                cerr << "FAILED: " << s << " != " << t << endl;
                return;
            } else {
                if (debug_test_binary_encoding) cout << "OK: " << s << " = " << t << endl;
            }
        } else {
            int number = numbers[inumber++];
            int x = -1;

            if (t == 1) x = reader.read_unary();
            else if (t == 2) x = reader.read_gamma();
            else if (t == 3) x = reader.read_delta();
            else x = reader.read_omega();

            if (x != number) {
                cerr << "FAILED: " << x << " != " << number << endl;
                return;
            } else {
                if (debug_test_binary_encoding) cout << "OK: " << x << " = " << number << endl;
            }
        }
    }

    cout << "PASSED." << endl;
    reader.close();
}
예제 #10
0
void User::load(BinaryFileReader& file)
{
	name=file.readString();
	Vector::load(file);
}