Menu

[r357]: / libcoding-0.0.0 / Interval.hpp  Maximize  Restore  History

Download this file

126 lines (103 with data), 3.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* This file is part of libcoding, a library handling (re-)coding of
* datatypes with arithmetic, huffman and various other coders
* Copyright (C) 2004-2011 Niels Fröhling
*
* Interval.hpp: interval class for probability intervals
* Copyright 2004-2011 Niels Fröhling
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef INTERVAL_HPP
#define INTERVAL_HPP
#include <assert.h>
#include <stdlib.h>
#include <math.h>
#pragma warning(disable : 4005)
alignto(16)
struct Interval {
public:
/* low <= target < high <= total */
prob low, target, high, total;
public:
forceinline prob getLow() const {return low;}
forceinline prob getTarget() const {return target;}
forceinline prob getRange() const {return high - low;}
forceinline prob getHigh() const {return high;}
forceinline prob getTotal() const {return total;}
/* do we code an interval absolute greater than 0.5 in [0.0, 1.0) */
forceinline bool isMPS () const {return (low != 0);}
forceinline bool isMPSDominating() const {return ((high - low) > (total - (high - low)));}
forceinline void expandRange(const prob range) {
// assert(range >= total);
low = 1 + (prob)(((int64)(low - 1) * (range - 2)) / (total - 2));
high = 1 + (prob)(((int64)(high - 1) * (range - 2)) / (total - 2));
total = 1 + (prob)(((int64)(total - 1) * (range - 2)) / (total - 2));
}
forceinline void expandTarget(const prob range) {
// assert(range >= total);
target = (prob)(((int64)target * total) / range);
total = (prob)(((int64)total * total) / range);
}
forceinline void invert() {
prob l = low;
low = total - high;
high = total - l;
target = total - (target + 1);
}
};
#include "Interval/Bit.hpp"
struct Performance {
long int codes; // number of symbols coded
double bits; // number of bits coded by this coder
double error; // amount of error
long int binary[2];
forceinline void clear() {codes = 0; bits = 0.0; error = 0.0; binary[0] = binary[1] = 0;}
forceinline double averageBits() {return bits / codes;}
forceinline double averageError() {return error / codes;}
forceinline void update(struct BitInterval *ivl) {
/* high & low may not be set */
ivl->setBit(ivl->isBit());
update((struct Interval *)ivl);
}
forceinline void update(struct Interval *ivl) {
if (ivl->getTotal()) {
double prb;
prb = ivl->getHigh();
prb -= ivl->getLow();
prb /= ivl->getTotal();
assert((prb > 0.0) && (prb <= 1.0));
codes++;
bits -= log2(prb);
error += 1.0 - prb;
}
}
forceinline void update(const double decision) {
assert((decision > 0.0) && (decision <= 1.0));
codes++;
bits -= log2(decision);
error += 1.0 - decision;
}
forceinline void log(const bool decision) {
binary[decision ? 1 : 0]++;
}
forceinline void add(struct Performance *a) {
codes += a->codes;
bits += a->bits;
error += a->error;
binary[0] += a->binary[0];
binary[1] += a->binary[1];
}
};
#endif //INTERVAL_HPP
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.