예제 #1
0
/** Write to the given BitOutputStream
 *  the sequence of bits coding the given symbol.
 *  PRECONDITION: build() has been called, to create the coding
 *  tree, and initialize root pointer and leaves vector.
 */
void HCTree::encode(byte symbol, BitOutputStream& out) const{

	HCNode* temp = leaves[(int)symbol];
	string ans;
	//read the encoded symbol in string
	while(temp->p != 0){

		if(temp->p->c0 == temp){
			ans = '0' + ans;
		}
		else if(temp->p->c1 == temp){
			ans = '1' + ans;
		}
		temp = temp->p;
	}



	for(int i = 0; i < ans.size(); ++i){
		if(ans[i] == '0'){
			out.writeBit(0);
		}
		else{
			out.writeBit(1);
		}
	}
	


}
예제 #2
0
void HCTree::encodeHelper(HCNode *curr, BitOutputStream& out) {
    if (curr->p == 0) return;
    encodeHelper(curr->p, out);
    if (curr->p->c0 == curr) out.writeBit(0);
    else out.writeBit(1);
    bitCount++;
}
예제 #3
0
void HCTree::encode(byte symbol, BitOutputStream& out) const{
  HCNode* leaf = leaves[symbol];
  list<int> mylist;
  // Store the bit path of symbol from leaf to root
  while(leaf != this->root) {
    if(leaf->isChild0) {
      mylist.push_back(0);
    }
    else {
      mylist.push_back(1);
    }
    leaf = leaf->parent;
  }
  // Reverse the bit path so it now goes from root to leaf
  mylist.reverse();
  // Go through list and write bits into bitoutputstream
  int i;
  while(mylist.size() != 0) {
    i = mylist.front();
    if(i==0) {
      out.writeBit(0);
    }
    else if(i==1) {
      out.writeBit(1);
    }
    mylist.pop_front();
  }
}
예제 #4
0
파일: HCTree.cpp 프로젝트: ElliotY/cse100
void HCTree::encode(byte symbol, BitOutputStream& out) const
{
	std::string code = getCode(symbol);
	for (size_t i = 0; i < code.size(); ++i) {
		if (code[i] == '1') {
			out.writeBit(true);
		} else {
			out.writeBit(false);
		}
	}
}
void HCTree::encode (byte symbol, BitOutputStream& out) const {

	std::vector<int> code;
	// find the path to the leaf to make the code
	for (int i = 0; i < leaves.size(); i++) {
		if (leaves[i] != 0 && leaves[i]->symbol == symbol) {
			HCNode * node = leaves[i];
			while (node->p != 0) {
				if (node == node->p->c0) {
					code.push_back(0);
					node = node->p;
				}
				else if (node == node->p->c1) {
					code.push_back(1);
					node = node->p;
				}
			}
		}
	}	

	// write the code to the file
	for (int v = code.size() - 1; v >= 0 ; v--) {
		out.writeBit(code[v]);
	}
}
예제 #6
0
/** Write to the given BitOutputStream
  *  the sequence of bits coding the given symbol.
  *  PRECONDITION: build() has been called, to create the coding
  *  tree, and initialize root pointer and leaves vector.
  */
void HCTree::encode(byte symbol, BitOutputStream& out) const{ 
    // Assign node to inputed byte
    HCNode *leafSym= this -> leaves[symbol]; 
	
	// Build from top to bottom
    std::stack<int> st;

    //cout << "starting loop" << endl;
	while(leafSym != this->root && leafSym != NULL){

        if (leafSym -> p -> c0 == leafSym) {
            st.push(0);
        }

        else if (leafSym -> p -> c1 == leafSym) {
            st.push(1);
        }
        leafSym = leafSym-> p;

    } 

    while (!st.empty()) {
        out.writeBit( st.top());
        st.pop();
    }
}
예제 #7
0
// Recursive function to write each bit in proper order
void writePattern(HCNode * node, BitOutputStream& out)
{
    if(node->p != NULL)
    {
	writePattern(node->p, out);
	out.writeBit(node->isChild1);
    }
    return;
}
예제 #8
0
//Method that encodes the tree into an out file to be decoded later on
void HCTree::encode(byte symbol, BitOutputStream& out) const
{
  //Gets the certain code sequence of the current symbol
  std::string seq = getValue(symbol);

  //Writes the bits to the correct sequences
  for(size_t index = 0; index < seq.size(); index++)
  {
    if(seq[index] == '1')
    {
      out.writeBit(true);
    }
    else
    {
      out.writeBit(false);
    }
  }
}
예제 #9
0
/** Write to the given BitOutputStream
 *  the sequence of bits coding the given symbol.
 *  PRECONDITION: build() has been called, to create the coding
 *  tree, and initialize root pointer and leaves vector.
 */
void HCTree::encode(byte symbol, BitOutputStream& out) const{
  HCNode* temp = leaves[symbol];
  stack<int> codeStack;
  if (temp->p == 0){
    out.writeBit(0);
  }
  while (temp->p != 0) {
    if (temp->p->c0==temp) {
      codeStack.push(0);
    } else{
      codeStack.push(1);
    }
    temp=temp->p;
  }
  while(!codeStack.empty()){
    out.writeBit(codeStack.top());
    codeStack.pop();
  }
}
예제 #10
0
/** Write to the given BitOutputStream
 *  the sequence of bits coding the given symbol.
 *  PRECONDITION: build() has been called, to create the coding
 *  tree, and initialize root pointer and leaves vector.
 */
void HCTree::encode(byte symbol, BitOutputStream& out) const {
  HCNode *pointer = leaves[symbol];
  std::stack<int> stk;
  while(pointer->p != 0) {
    if(pointer == pointer->p->c0) stk.push(0);
    else stk.push(1);
    pointer = pointer->p;
  }
  while(stk.size() != 0) {
    out.writeBit(stk.top());
    stk.pop();
  }
}
예제 #11
0
//Write char for length of code, char for ascII symbol, then bits for code
void HCTree::encode(byte symbol, BitOutputStream& out) const
{
    HCNode *currNode = leaves[symbol];
    //stack of int since writeBit takes in int parameter
    stack<int> bodyStack;
    stack<int> headerStack;
    //we can't go above root since it has no parent
    while (currNode != root){
        HCNode *parentNode = currNode->p;
        if (parentNode->c0 == currNode){
            //node is on the left side of the parent
           bodyStack.push(0);
        }else{
           bodyStack.push(1);
        }
        currNode = parentNode;
    }

    for (unsigned int i = 0; i < stack.size(); i++){
        int encodedBit = stack.top();
        out.writeBit(encodedBit);
        stack.pop(); //remove each element once it's written
    }
}