/*****************************************************************************
* huffmancode.h
*
* Huffman coding algorighm implemented by C++ template.
*
* This class is designed for Huffman codeing and decoding algorighm. The
* priority queue is used in building Huffman tree. For comparing pointers
* of Huffman Tree Node by their pointing contents, I use the smart pointer
* replacing nomal pointer. And for saving memory, the coded word is stored
* bit-by-bit in an unsigned char array.
*
* Zhang Ming, 20010-07, Xi'an Jiaotong University.
*****************************************************************************/
#ifndef HUFFMANCODE_H
#define HUFFMANCODE_H
#include
#include
using namespace std;
namespace itlab
{
/**
* Object node to be coded
*/
template
struct CodeObject
{
Object data;
Weight cost;
};
// struct CodeObject
/**
* Huffman codeword node
*/
const int CODESIZE = 2;
template
struct HCNode
{
Object data;
unsigned char bits[CODESIZE];
unsigned int length;
};
// struct HCNode
/**
* Huffman tree node
*/
template
struct HTNode
{
Object data;
Weight cost;
HTNode
实现文件:
/*****************************************************************************
* huffmancode-impl.h
*
* Implementation for Huffman code.
*
* Zhang Ming, 2010-07
*****************************************************************************/
/**
* constructors and destructor
*/
template
HuffmanTree::HuffmanTree() : root(NULL)
{
arraySize = 0;
codeTable = NULL;
}
template
HuffmanTree::~HuffmanTree()
{
HTNode *r = root;
destroy(r);
root = NULL;
delete []codeTable;
}
/**
* Huffman codeing.
*/
template
void HuffmanTree::code( CodeObject *codeArray, int length )
{
arraySize = length;
if( codeTable != NULL )
delete []codeTable;
codeTable = new HCNode[arraySize];
createHuffmanTree( codeArray );
createCodeTable();
}
/**
* If decodeing successful return ture, else return false;
*/
template
bool HuffmanTree::decode( unsigned char codeword[CODESIZE], unsigned int length,
Object &decodeword )
{
unsigned int pos = 0;
HTNSmartPtr p = root;
for( pos=0; posleft;
else
p = p->right;
}
else
break;
if( pos == length && p != NULL && p->left == NULL && p->right == NULL )
{
decodeword = p->data;
return true;
}
else
return false;
}
/**
* Print the codedword.
*/
template
inline void HuffmanTree::printCode( unsigned char codewrod[CODESIZE], unsigned int length )
{
for( unsigned int j=0; j
void HuffmanTree::printCodeTable()
{
cout << "Object\tCode\tSize" << endl;
for( int i=0; i
void HuffmanTree::createHuffmanTree( CodeObject *codeArray )
{
BinaryHeap > heap( arraySize );
HTNSmartPtr tree = NULL,
subtreeL = NULL,
subtreeR = NULL;
for( int i=0; i( codeArray[i].data, codeArray[i].cost, NULL, NULL );
heap.insert( tree );
}
while( heap.size() > 1 )
{
heap.deleteMin( subtreeL );
heap.deleteMin( subtreeR );
tree = new HTNode( Object(), subtreeL->cost+subtreeR->cost, subtreeL, subtreeR );
heap.insert( tree );
}
heap.deleteMin( root );
}
/**
* Create the coded character table.
*/
template
void HuffmanTree::createCodeTable()
{
for( int i=0; i
void HuffmanTree::createCodeTableRecursive( HTNSmartPtr ht,
unsigned char *code, int pos, int &index )
{
if( ht->left )
{
setBit( code, pos, 0 );
createCodeTableRecursive( ht->left, code, pos+1, index );
}
if( ht->right )
{
setBit( code, pos, 1 );
createCodeTableRecursive( ht->right, code, pos+1, index );
}
if( ht->left==NULL && ht->right==NULL )
{
codeTable[index].data = ht->data;
for( int i=0; i
void HuffmanTree::setBit( unsigned char *bits, unsigned int pos, unsigned int state )
{
unsigned char mask = 0x80;
for( unsigned int i=0; i<(pos % 8); ++i )
mask = mask >> 1;
if( state == 1 )
bits[pos/8] = bits[pos/8] | mask;
else if( state == 0 )
bits[pos/8] = bits[pos/8] & (~mask);
else
cerr << endl << "The bit to be set should be '1' or '0'!" << endl;
return;
}
/**
* Get the state of the bit at position pos in the array bits.
*/
template
unsigned int HuffmanTree::getBit( unsigned char *bits, unsigned int pos )
{
unsigned char mask = 0x80;
for( unsigned int i=0; i<(pos%8); ++i )
mask = mask >> 1;
return ( ((mask & bits[(int)(pos/8)]) == mask) ? 1 : 0 );
}
/**
* Destroy the tree.
*/
template
void HuffmanTree::destroy( HTNode *&r )
{
if( r != NULL )
{
destroy( r->left );
destroy( r->right );
delete r;
}
r = NULL;
}
系统中统计数据,由于调用统计过程,执行时间超过了weblogic设置的时间,提示如下错误:
统计数据出错!
原因:The transaction is no longer active - status: 'Rolling Back. [Reason=weblogic.transaction.internal
Totally five patchs committed to erlang otp, just small patchs.
IMO, erlang really is a interesting programming language, I really like its concurrency feature.
but the functional programming style
两个步骤:
1.用w命令找到要踢出的用户,比如下面:
[root@localhost ~]# w
18:16:55 up 39 days, 8:27, 3 users, load average: 0.03, 0.03, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
package edu.xidian.graph;
class MyStack {
private final int SIZE = 20;
private int[] st;
private int top;
public MyStack() {
st = new int[SIZE];
top = -1;
}
public void push(i