📄 channel.c
字号:
if (channel->fileptr == NULL) return(0); if (QccFileWriteMagicNumber(channel->fileptr, QCCCHANNEL_MAGICNUM)) goto QccErr; fprintf(channel->fileptr, "%d\n%d\n", channel->channel_length, channel->alphabet_size); if (ferror(channel->fileptr)) goto QccErr; return(0); QccErr: QccErrorAddMessage("(QccChannelWriteHeader): Error writing header to %s", channel->filename); return(1); }int QccChannelStartWrite(QccChannel *channel){ if (channel == NULL) return(0); if ((channel->fileptr = QccFileOpen(channel->filename, "w")) == NULL) { QccErrorAddMessage("(QccChannelWrite): Error opening %s for writing", channel->filename); return(1); } if (QccChannelWriteHeader(channel)) { QccErrorAddMessage("(QccChannelWrite): Error writing header to %s", channel->filename); return(1); } if (QccChannelAlloc(channel)) { QccErrorAddMessage("(QccChannelWrite): Error calling QccChannelAlloc()"); return(1); } channel->num_blocks_accessed = 0; return(0);}int QccChannelEndWrite(QccChannel *channel){ if (channel == NULL) return(0); QccFileClose(channel->fileptr); QccChannelFree(channel); return(0);}int QccChannelWriteBlock(QccChannel *channel){ int symbol; int num_symbols_to_write; if (channel == NULL) return(0); if ((channel->fileptr == NULL) || (!channel->access_block_size)) return(0); num_symbols_to_write = QccChannelGetBlockSize(channel); for (symbol = 0; symbol < num_symbols_to_write; symbol++) fprintf(channel->fileptr, "%d\n", channel->channel_symbols[symbol]); channel->num_blocks_accessed++; return(0);}int QccChannelNormalize(QccChannel *channel){ int symbol; int offset; if (channel == NULL) return(0); offset = channel->alphabet_size / 2; for (symbol = 0; symbol < QccChannelGetBlockSize(channel); symbol++) channel->channel_symbols[symbol] += offset; return(0);}int QccChannelDenormalize(QccChannel *channel){ int symbol; int offset; if (channel == NULL) return(0); offset = channel->alphabet_size / 2; for (symbol = 0; symbol < QccChannelGetBlockSize(channel); symbol++) channel->channel_symbols[symbol] -= offset; return(0);}int QccChannelGetNumNullSymbols(const QccChannel *channel){ int cnt; int symbol; if (channel == NULL) return(0); if (channel->channel_symbols == NULL) return(0); for (symbol = 0, cnt = 0; symbol < QccChannelGetBlockSize(channel); symbol++) if (channel->channel_symbols[symbol] == QCCCHANNEL_NULLSYMBOL) cnt++; return(cnt);}int QccChannelRemoveNullSymbols(QccChannel *channel){ int num_null_symbols; int *new_channel_symbols; int symbol1, symbol2; int block_size; if (channel == NULL) return(0); if (channel->channel_symbols == NULL) return(0); block_size = QccChannelGetBlockSize(channel); if (block_size != channel->channel_length) { QccErrorAddMessage("(QccChannelRemoveNullSymbols): Block size must be whole channel"); return(1); } num_null_symbols = QccChannelGetNumNullSymbols(channel); if ((new_channel_symbols = (int *)malloc(sizeof(int) * (block_size - num_null_symbols))) == NULL) { QccErrorAddMessage("(QccChannelRemoveNullSymbols): Error allocating memory"); return(1); } for (symbol1 = 0, symbol2 = 0; symbol1 < block_size; symbol1++) if (channel->channel_symbols[symbol1] != QCCCHANNEL_NULLSYMBOL) new_channel_symbols[symbol2++] = channel->channel_symbols[symbol1]; QccFree(channel->channel_symbols); channel->channel_symbols = new_channel_symbols; channel->channel_length -= num_null_symbols; return(0);}double QccChannelEntropy(const QccChannel *channel, int order){ double entropy = 0; int block_size; int symbol; QccMatrix probs = NULL; int symbol_count; int context = 0; int num_contexts = 0; if (channel == NULL) return((double)-1.0); if (channel->channel_symbols == NULL) return((double)-1.0); if ((order != 1) && (order != 2)) { QccErrorAddMessage("(QccChannelEntropy): only order 1 or 2 entropy is supported"); goto Error; } if (channel->alphabet_size < 1) { QccErrorAddMessage("(QccChannelEntropy): channel %s has undefined or illegal alphabet size (%d)", channel->filename, channel->alphabet_size); goto Error; } num_contexts = (order == 2) ? channel->alphabet_size : 1; block_size = QccChannelGetBlockSize(channel); if (!block_size) return((double)0); if ((probs = QccMatrixAlloc(num_contexts, channel->alphabet_size)) == NULL) { QccErrorAddMessage("(QccChannelEntropy): Error calling QccMatrixAlloc()"); goto Error; } for (context = 0; context < num_contexts; context++) for (symbol = 0; symbol < channel->alphabet_size; symbol++) probs[context][symbol] = 0.0; for (symbol = 0, context = 0, symbol_count = 0; symbol < block_size; symbol++) { if (channel->channel_symbols[symbol] != QCCCHANNEL_NULLSYMBOL) { if ((channel->channel_symbols[symbol] >= channel->alphabet_size) || (channel->channel_symbols[symbol] < 0)) { QccErrorAddMessage("(QccChannelEntropy): Invalid symbol %d in channel %s", channel->channel_symbols[symbol], channel->filename); goto Error; } probs[context][channel->channel_symbols[symbol]] += 1; context = (order == 2) ? channel->channel_symbols[symbol] : 0; symbol_count++; } } /* Joint probability */ if (symbol_count) { for (context = 0; context < num_contexts; context++) for (symbol = 0; symbol < channel->alphabet_size; symbol++) probs[context][symbol] /= (double)symbol_count; entropy = QccENTConditionalEntropy(probs, num_contexts, channel->alphabet_size); } else entropy = 0; goto Return; Error: entropy = -1.0; Return: if (probs != NULL) QccMatrixFree(probs, num_contexts); return(entropy);}int QccChannelAddSymbolToChannel(QccChannel *channel, int symbol){ if (channel == NULL) return(0); if (channel->access_block_size != QCCCHANNEL_ACCESSWHOLEFILE) { QccErrorAddMessage("(QccChannelAddSymbolToChannel): Block size must be whole file\n"); return(1); } if (channel->channel_symbols == NULL) { if ((channel->channel_symbols = (int *)malloc(sizeof(int))) == NULL) { QccErrorAddMessage("(QccChannelAddSymbolToChannel): Error allocating memory"); return(1); } channel->channel_symbols[0] = symbol; channel->channel_length = 1; } else { channel->channel_length++; if ((channel->channel_symbols = (int *)realloc((void *)channel->channel_symbols, sizeof(int)*channel->channel_length)) == NULL) { QccErrorAddMessage("(QccChannelAddSymbolToChannel): Error reallocating memory"); return(1); } channel->channel_symbols[channel->channel_length - 1] = symbol; } return(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -