优化用于读取文件中某些VLV的代码?

时间:2017-04-03 12:11:49

标签: function optimization variable-length

我试图从我创建的文件中读取一些 variable-length-values

该文件包含以下内容:

View convertView = LayoutInflater.from(ctx).inflate(R.layout.activity_jadwal_batal, null);
 mSwipeRefreshLayout = (SwipeRefreshLayout)convertViewfindViewById(R.id.swifeRefresh);

有两个VLV,81 7F 81 01 2F F3 FF 81 7F,其中81 01255为十进制。

我还创建了一些文件阅读器功能,如下所示:

129

在从文件中读取时,运行void read_byte_from_file_to(std::fstream& file, uint8_t& to) { file.read((char*)&to, 1); } unsigned long readVLV(std::fstream& t_midi_file) { unsigned long result = 0; static unsigned long sum = 0, depth = 0, count = 0; uint8_t c; read_byte_from_file_to(t_midi_file, c); ++count; if (c & 0x80) { readVLV(t_midi_file); } sum += (c & 0x7F) << (7 * depth++); if (count == depth) { result = sum; sum = 0; depth = 0; count = 0; } return result; }; readVLV次为第一个n VLV提供了正确的答案,我绝对讨厌我如何编写它,这么多静态参数和丑陋参数重置。因此,如果有人能指引我朝着正确的方向前进,我会非常高兴。

1 个答案:

答案 0 :(得分:0)

获取函数位置状态的基本_readVLV可以通过编写

来完成
unsigned long _readVLV(
        std::fstream& t_midi_file,
        unsigned long sum,
        unsigned long depth) {
    uint8_t c;
    read_byte_from_file_to(t_midi_file, c);

    if (c & 0x80) {
        sum += _readVLV(t_midi_file, sum, depth);
        ++depth;
    }

    return (c & 0x7F) << (7 * depth);
}

并创建一个全局readVLV函数,该函数获取位置信息和文件,如此

unsigned long readVLV(std::fstream& t_midi_file) {
    unsigned long sum = 0, depth = 0, count = 0;
    return _readVLV(t_midi_file, sum, depth, count);
}