private IEnumerable <Tuple <int, int> > FindBreaks(IBitEncoding e, int imin, Code min, int imax, Code max)
        {
            if (imax == imin + 1)
            {
                yield return(Tuple.Create(imax, max.Length));

                yield break;
            }

            int  imid = imin + (imax - imin) / 2;
            Code mid  = e.GetCode(imid);

            if (mid.Length > min.Length)
            {
                foreach (var k in FindBreaks(e, imin, min, imid, mid))
                {
                    yield return(k);
                }
            }

            if (max.Length > mid.Length)
            {
                foreach (var k in FindBreaks(e, imid, mid, imax, max))
                {
                    yield return(k);
                }
            }
        }
Beispiel #2
0
        internal Golomb(int divisor)
        {
            if (divisor < 2)
            {
                throw new ArgumentOutOfRangeException();
            }

            this.divisor  = divisor;
            remainderCode = Codes.TruncatedBinary(divisor);
            var r = remainderCode.MaxEncodable;

            maxEncodable = divisor * (63 - remainderCode.GetCode(r).Length) + r;
        }
        private IEnumerable <Tuple <int, int> > FindBreaks(IBitEncoding e)
        {
            Code min = e.GetCode(e.MinEncodable);

            yield return(Tuple.Create(e.MinEncodable, min.Length));

            Code max = e.GetCode(e.MaxEncodable);

            foreach (var k in FindBreaks(e, e.MinEncodable, min, e.MaxEncodable, max))
            {
                yield return(k);
            }
        }
Beispiel #4
0
        internal BaseN(int n)
        {
            this.n = n;

            if (Bits.IsPowerOf2(n))
            {
                encoding = Codes.FixedWidth(Bits.FloorLog2(n) + 1);
            }
            else
            {
                encoding = Codes.TruncatedBinary(n);
            }
        }
Beispiel #5
0
        private void EncodingTest(IBitEncoding encoding)
        {
            var writer = new BitWriter(1000);
            var values = new List <int>();
            var g      = new Generator(encoding.GetType().Name);

            for (int i = 0; i < 1000; i++)
            {
                int v = g.Int32() >> g.Int32(32);
                if (v < encoding.MinEncodable || v > encoding.MaxEncodable)
                {
                    continue;
                }
                values.Add(v);

                if (i < 500)
                {
                    writer.Write(encoding.GetCode(v));
                }
                else
                {
                    encoding.Write(writer, v);
                }
            }

            var buffer = writer.ToArray();
            var reader = new BitReader(buffer);

            for (int i = 0; i < values.Count; i++)
            {
                int v = encoding.Read(reader);
                Assert.AreEqual(values[i], v);
            }

            try
            {
                encoding.GetCode(encoding.MinEncodable - 1);
                Assert.Fail("Expected ArgumentOutOfRangeException");
            }
            catch (Exception e)
            {
                Assert.AreEqual("ArgumentOutOfRangeException", e.GetType().Name);
            }
        }
Beispiel #6
0
 /// <summary>
 /// Reads a <see cref="System.Int32"/> value using the specified <see cref="IBitEncoding"/>.
 /// </summary>
 public int Read(IBitEncoding encoding)
 {
     return(encoding.Read(this));
 }
Beispiel #7
0
 /// <summary>
 /// Use this <see cref="IBitEncoding"/> to write a <see cref="System.Int32"/> value
 /// to the specified <see cref="BitWriter"/>.
 /// </summary>
 public static void Write(this IBitEncoding encoding, BitWriter writer, int value)
 {
     writer.Write(encoding, value);
 }
Beispiel #8
0
 /// <summary>
 /// Write a <see cref="System.Int32"/> using the specified <see cref="IBitEncoding"/>.
 /// </summary>
 public void Write(IBitEncoding encoding, int value)
 {
     Write(encoding.GetCode(value));
 }