Initial Commit

This commit is contained in:
2023-06-21 12:46:23 -04:00
commit c70248a520
1352 changed files with 336780 additions and 0 deletions

View File

@@ -0,0 +1,158 @@
using System;
namespace System.Drawing {
/// <summary>
/// The <code>Dimension</code> class encapsulates the width and
/// height of a component (in int precision) in a single object.
/// </summary>
/// <remarks>
/// The class is
/// associated with certain properties of components. Several methods
/// defined by the <code>Component</code> class and the
/// <code>LayoutManager</code> interface return a <code>Dimension</code> object.
/// <p/>
/// Normally the values of <code>width</code>
/// and <code>height</code> are non-negative ints.
/// The constructors that allow you to create a dimension do
/// not prevent you from setting a negative value for these properties.
/// If the value of <code>width</code> or <code>height</code> is
/// negative, the behavior of some methods defined by other objects is
/// undefined.
/// </remarks>
public class Dimension : Dimension2D {
/// <summary>
/// The width dimension. Negative values can be used.
/// </summary>
public int width;
/// <summary>
/// The height dimension. Negative values can be used.
/// </summary>
public int height;
/// <summary>
/// Creates an instance of <code>Dimension</code> with a width
/// of zero and a height of zero.
/// </summary>
public Dimension() : this(0, 0) {}
/// <summary>
/// Creates an instance of <code>Dimension</code> whose width
/// and height are the same as for the specified dimension.
/// </summary>
/// <param name="d">
/// the specified dimension for the
/// <code>width</code> and
/// <code>height</code> values.
/// </param>
public Dimension(Dimension d) : this(d.width, d.height) {}
/// <summary>
/// Constructs a Dimension and initializes it to the specified width and
/// specified height.
/// </summary>
/// <param name="width">the specified width dimension</param>
/// <param name="height">the specified height dimension</param>
public Dimension(int width, int height) {
this.width = width;
this.height = height;
}
/// <summary>
/// Returns the width of this dimension in double precision.
/// </summary>
/// <value>the width</value>
public override double Width {
get {
return width;
}
}
/// <summary>
/// Returns the height of this dimension in double precision.
/// </summary>
/// <value>the height</value>
public override double Height {
get {
return height;
}
}
/// <summary>
/// Set the size of this Dimension object to the specified width
/// and height in double precision.
/// </summary>
/// <param name="width">the new width for the Dimension object</param>
/// <param name="height">the new height for the Dimension object</param>
public override void SetSize(double width, double height) {
width = (int) Math.Ceiling(width);
height = (int) Math.Ceiling(height);
}
/// <summary>
/// Get/set the size of this <code>Dimension</code> object.
/// </summary>
/// <value>the size</value>
public new Dimension Size {
get {
return new Dimension(width, height);
}
set {
SetSize(value.width, value.height);
}
}
/// <summary>
/// Set the size of this <code>Dimension</code> object
/// to the specified width and height.
/// </summary>
/// <param name="width">the new width for this <code>Dimension</code> object.</param>
/// <param name="height">the new height for this <code>Dimension</code> object.</param>
public void SetSize(int width, int height) {
this.width = width;
this.height = height;
}
/// <summary>
/// Checks whether two dimension objects have equal values.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(Object obj) {
if (obj is Dimension) {
Dimension d = (Dimension)obj;
return (width == d.width) && (height == d.height);
}
return false;
}
/// <summary>
/// Returns the hash code for this Dimension.
/// </summary>
/// <returns>a hash code</returns>
public override int GetHashCode() {
int sum = width + height;
return sum * (sum + 1)/2 + width;
}
/// <summary>
/// Returns a string representation of the values of this
/// <code>Dimension</code> object's <code>height</code> and
/// <code>width</code> fields.
/// </summary>
/// <remarks>
/// This method is intended to be used only
/// for debugging purposes, and the content and format of the returned
/// string may vary between implementations. The returned string may be
/// empty but may not be <code>null</code>.
/// </remarks>
/// <returns>a string representation of this <code>Dimension</code>
/// object.
/// </returns>
public override string ToString() {
return this.GetType().Name + "[width=" + width + ",height=" + height + "]";
}
}
}

View File

@@ -0,0 +1,69 @@
using System;
namespace System.Drawing {
/// <summary>
/// The <code>Dimension2D</code> class is to encapsulate a width
/// and a height dimension.
/// </summary>
/// <remarks>
/// This class is only the abstract baseclass for all objects that
/// store a 2D dimension.
/// The actual storage representation of the sizes is left to
/// the subclass.
/// </remarks>
public abstract class Dimension2D : ICloneable {
/// <summary>
/// This is an abstract class that cannot be instantiated directly.
/// Type-specific implementation subclasses are available for
/// instantiation and provide a number of formats for storing
/// the information necessary to satisfy the various accessor
/// methods below.
/// </summary>
/// <seealso cref="T:System.Drawing.Dimension"/>
protected Dimension2D() {
}
/// <summary>
/// Returns the width of this <code>Dimension</code> in double
/// precision.
/// </summary>
/// <value>the width</value>
public abstract double Width {get;}
/// <summary>
/// Returns the height of this <code>Dimension</code> in double
/// precision.
/// </summary>
/// <value>the height</value>
public abstract double Height {get;}
/// <summary>
/// Sets the size of this <code>Dimension</code> object to the
/// specified width and height.
/// </summary>
/// <param name="width">the new width for the <code>Dimension</code>
/// object</param>
/// <param name="height">the new height for the <code>Dimension</code>
/// object</param>
public abstract void SetSize(double width, double height);
/// <summary>
/// Sets the size of this <code>Dimension2D</code> object to
/// match the specified size.
/// </summary>
/// <value>the size</value>
public Dimension2D Size {
set {
SetSize(value.Width, value.Height);
}
}
/// <summary>
/// Creates a new object of the same class as this object.
/// </summary>
/// <returns>a clone of this instance</returns>
public Object Clone() {
throw new Exception("not implemented");
}
}
}

View File

@@ -0,0 +1,43 @@
using System;
using System.Collections;
namespace System.util {
/// <summary>
/// Summary description for ListIterator.
/// </summary>
public class ListIterator {
ArrayList col;
int cursor = 0;
int lastRet = -1;
public ListIterator(ArrayList col) {
this.col = col;
}
public bool HasNext() {
return cursor != col.Count;
}
public object Next() {
Object next = col[cursor];
lastRet = cursor++;
return next;
}
public object Previous() {
int i = cursor - 1;
Object previous = col[i];
lastRet = cursor = i;
return previous;
}
public void Remove() {
if (lastRet == -1)
throw new InvalidOperationException();
col.RemoveAt(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
}
}
}

View File

@@ -0,0 +1,209 @@
using System;
using System.Text;
using System.IO;
using System.Collections;
namespace System.util
{
/// <summary>
/// Summary description for Properties.
/// </summary>
public class Properties
{
private Hashtable _col;
private const string whiteSpaceChars = " \t\r\n\f";
private const string keyValueSeparators = "=: \t\r\n\f";
private const string strictKeyValueSeparators = "=:";
public Properties()
{
_col = new Hashtable();
}
public string Remove(string key) {
string retval = (string)_col[key];
_col.Remove(key);
return retval;
}
public IEnumerator GetEnumerator() {
return _col.GetEnumerator();
}
public bool ContainsKey(string key) {
return _col.ContainsKey(key);
}
public virtual void Add(string key, string value) {
_col[key] = value;
}
public void AddAll(Properties col) {
foreach (string itm in col.Keys) {
_col[itm] = col[itm];
}
}
public int Count {
get {
return _col.Count;
}
}
public virtual string this[string key] {
get {
return (string)_col[key];
}
set {
_col[key] = value;
}
}
public ICollection Keys {
get {
return _col.Keys;
}
}
public void Clear() {
_col.Clear();
}
public void Load(Stream inStream) {
StreamReader inp = new StreamReader(inStream, Encoding.GetEncoding(1252));
while (true) {
// Get next line
String line = inp.ReadLine();
if (line == null)
return;
if (line.Length > 0) {
// Find start of key
int len = line.Length;
int keyStart;
for (keyStart=0; keyStart<len; keyStart++)
if (whiteSpaceChars.IndexOf(line[keyStart]) == -1)
break;
// Blank lines are ignored
if (keyStart == len)
continue;
// Continue lines that end in slashes if they are not comments
char firstChar = line[keyStart];
if ((firstChar != '#') && (firstChar != '!')) {
while (ContinueLine(line)) {
String nextLine = inp.ReadLine();
if (nextLine == null)
nextLine = "";
String loppedLine = line.Substring(0, len-1);
// Advance beyond whitespace on new line
int startIndex;
for (startIndex=0; startIndex<nextLine.Length; startIndex++)
if (whiteSpaceChars.IndexOf(nextLine[startIndex]) == -1)
break;
nextLine = nextLine.Substring(startIndex,nextLine.Length - startIndex);
line = loppedLine+nextLine;
len = line.Length;
}
// Find separation between key and value
int separatorIndex;
for (separatorIndex=keyStart; separatorIndex<len; separatorIndex++) {
char currentChar = line[separatorIndex];
if (currentChar == '\\')
separatorIndex++;
else if (keyValueSeparators.IndexOf(currentChar) != -1)
break;
}
// Skip over whitespace after key if any
int valueIndex;
for (valueIndex=separatorIndex; valueIndex<len; valueIndex++)
if (whiteSpaceChars.IndexOf(line[valueIndex]) == -1)
break;
// Skip over one non whitespace key value separators if any
if (valueIndex < len)
if (strictKeyValueSeparators.IndexOf(line[valueIndex]) != -1)
valueIndex++;
// Skip over white space after other separators if any
while (valueIndex < len) {
if (whiteSpaceChars.IndexOf(line[valueIndex]) == -1)
break;
valueIndex++;
}
String key = line.Substring(keyStart, separatorIndex - keyStart);
String value = (separatorIndex < len) ? line.Substring(valueIndex, len - valueIndex) : "";
// Convert then store key and value
key = LoadConvert(key);
value = LoadConvert(value);
Add(key, value);
}
}
}
}
/*
* Converts encoded &#92;uxxxx to unicode chars
* and changes special saved chars to their original forms
*/
private String LoadConvert(String theString) {
char aChar;
int len = theString.Length;
StringBuilder outBuffer = new StringBuilder(len);
for (int x=0; x<len; ) {
aChar = theString[x++];
if (aChar == '\\') {
aChar = theString[x++];
if (aChar == 'u') {
// Read the xxxx
int value=0;
for (int i=0; i<4; i++) {
aChar = theString[x++];
switch (aChar) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
value = (value << 4) + aChar - '0';
break;
case 'a': case 'b': case 'c':
case 'd': case 'e': case 'f':
value = (value << 4) + 10 + aChar - 'a';
break;
case 'A': case 'B': case 'C':
case 'D': case 'E': case 'F':
value = (value << 4) + 10 + aChar - 'A';
break;
default:
throw new ArgumentException(
"Malformed \\uxxxx encoding.");
}
}
outBuffer.Append((char)value);
} else {
if (aChar == 't') aChar = '\t';
else if (aChar == 'r') aChar = '\r';
else if (aChar == 'n') aChar = '\n';
else if (aChar == 'f') aChar = '\f';
outBuffer.Append(aChar);
}
} else
outBuffer.Append(aChar);
}
return outBuffer.ToString();
}
private bool ContinueLine(String line) {
int slashCount = 0;
int index = line.Length - 1;
while ((index >= 0) && (line[index--] == '\\'))
slashCount++;
return (slashCount % 2 == 1);
}
}
}

View File

@@ -0,0 +1,130 @@
using System;
/*
* $Id: StringTokenizer.cs,v 1.4 2006/06/16 10:52:26 psoares33 Exp $
*
* Copyright 2006 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License isp distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code isp 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code isp Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code isp Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library isp free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library isp 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 Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace System.util {
// a replacement for the StringTokenizer java class
// it's more or less the same as the one in the GNU classpath
public class StringTokenizer {
private int pos;
private String str;
private int len;
private String delim;
private bool retDelims;
public StringTokenizer(String str) : this(str, " \t\n\r\f", false) {
}
public StringTokenizer(String str, String delim) : this(str, delim, false) {
}
public StringTokenizer(String str, String delim, bool retDelims) {
len = str.Length;
this.str = str;
this.delim = delim;
this.retDelims = retDelims;
this.pos = 0;
}
public bool HasMoreTokens() {
if (! retDelims) {
while (pos < len && delim.IndexOf(str[pos]) >= 0)
pos++;
}
return pos < len;
}
public String NextToken(String delim) {
this.delim = delim;
return NextToken();
}
public String NextToken() {
if (pos < len && delim.IndexOf(str[pos]) >= 0) {
if (retDelims)
return str.Substring(pos++, 1);
while (++pos < len && delim.IndexOf(str[pos]) >= 0);
}
if (pos < len) {
int start = pos;
while (++pos < len && delim.IndexOf(str[pos]) < 0);
return str.Substring(start, pos - start);
}
throw new IndexOutOfRangeException();
}
public int CountTokens() {
int count = 0;
int delimiterCount = 0;
bool tokenFound = false;
int tmpPos = pos;
while (tmpPos < len) {
if (delim.IndexOf(str[tmpPos++]) >= 0) {
if (tokenFound) {
count++;
tokenFound = false;
}
delimiterCount++;
}
else {
tokenFound = true;
while (tmpPos < len
&& delim.IndexOf(str[tmpPos]) < 0)
++tmpPos;
}
}
if (tokenFound)
count++;
return retDelims ? count + delimiterCount : count;
}
}
}

View File

@@ -0,0 +1,27 @@
using System;
using System.Globalization;
namespace System.util
{
/// <summary>
/// Summary description for Util.
/// </summary>
public class Util
{
public static int USR(int op1, int op2) {
if (op2 < 1) {
return op1;
} else {
return unchecked((int)((uint)op1 >> op2));
}
}
public static bool EqualsIgnoreCase(string s1, string s2) {
return CultureInfo.InvariantCulture.CompareInfo.Compare(s1, s2, CompareOptions.IgnoreCase) == 0;
}
public static int CompareToIgnoreCase(string s1, string s2) {
return CultureInfo.InvariantCulture.CompareInfo.Compare(s1, s2, CompareOptions.IgnoreCase);
}
}
}

View File

@@ -0,0 +1,49 @@
using System;
namespace System.util.collections
{
/// <summary>
/// Very basic algorithms tool class.
/// </summary>
public class k_Algorithm
{
public static k_Iterator Copy(k_Iterator ak_SrcFirst, k_Iterator ak_BehindSrcLast, k_Iterator ak_DstFirst)
{
k_Iterator lk_Src = ak_SrcFirst.Clone(), lk_Dst = ak_DstFirst.Clone();
while (lk_Src != ak_BehindSrcLast)
{
lk_Dst.Current = lk_Src.Current;
lk_Src.Next(); lk_Dst.Next();
}
return lk_Dst;
}
public static k_Iterator CopyBackward(k_Iterator ak_SrcFirst, k_Iterator ak_BehindSrcLast, k_Iterator ak_BehindDstLast)
{
k_Iterator lk_Src = ak_BehindSrcLast.Clone(), lk_Dst = ak_BehindDstLast.Clone();
while (lk_Src != ak_SrcFirst)
{
lk_Src.Prev(); lk_Dst.Prev();
lk_Dst.Current = lk_Src.Current;
}
return lk_Dst;
}
public static void Fill(k_Iterator ak_DstFirst, k_Iterator ak_BehindDstLast, object ak_Value)
{
for (k_Iterator lk_Iter = ak_DstFirst.Clone(); lk_Iter != ak_BehindDstLast; lk_Iter.Next())
lk_Iter.Current = ak_Value;
}
public static k_Iterator Find(k_Iterator ak_First, k_Iterator ak_Last, object ak_Value)
{
k_Iterator lk_Iter = ak_First.Clone();
for (; lk_Iter != ak_Last; lk_Iter.Next())
{
if (object.Equals(lk_Iter.Current, ak_Value))
break;
}
return lk_Iter;
}
}
}

View File

@@ -0,0 +1,64 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// Base interface for all containers
/// </summary>
public interface IContainer : ICollection, ICloneable
{
k_Iterator Begin { get; }
k_Iterator End { get; }
bool IsEmpty { get; }
k_Iterator Find(object ak_Value);
k_Iterator Erase(k_Iterator ak_Where);
k_Iterator Erase(k_Iterator ak_First, k_Iterator ak_Last);
}
/// <summary>
/// Interface for non-associative sequential containers (k_Vector, k_Deque, k_List)
/// </summary>
public interface ISequence : IContainer, IList
{
object Front { get; set; }
object Back { get; set; }
void PushFront(object ak_Value);
void PopFront();
void PushBack(object ak_Value);
void PopBack();
void Assign(k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd);
void Assign(object ak_Value, int ai_Count);
void Insert(k_Iterator ak_Where, object ak_Value);
void Insert(k_Iterator ak_Where, k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd);
}
/// <summary>
/// Interface for IDictionary derived containers which provide key to value mapping (k_HashTable)
/// </summary>
public interface IMap : IContainer, IDictionary
{
k_Iterator FindKey(object ak_Key);
void Add(DictionaryEntry ar_Item);
void Insert(k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd);
}
/// <summary>
/// Interface for sorted mapping containers (k_SkipList, k_Tree)
/// </summary>
public interface ISortedMap : IMap
{
IComparer Comparer { get; }
k_Iterator LowerBound(object ak_Key);
k_Iterator UpperBound(object ak_Key);
}
}

View File

@@ -0,0 +1,470 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// Circular buffer of arrays
/// </summary>
public class k_Deque : ISequence
{
#region k_BlockIterator Implementation
private class k_BlockIterator : k_Iterator
{
private readonly k_Deque mk_Deque;
private int mi_Index;
private int mi_BlockIndex;
private int mi_BlockOffset;
public k_BlockIterator(k_Deque ak_Deque, int ai_Index)
{
mk_Deque = ak_Deque;
mi_Index = ai_Index;
mi_BlockIndex = mk_Deque.CalcBlockAndPos(mi_Index, out mi_BlockOffset);
}
public override void Move(int ai_Count)
{
int li_Index = mi_Index + ai_Count;
if (li_Index > mk_Deque.Count)
throw new InvalidOperationException("Tried to move beyond end element.");
else if (li_Index < 0)
throw new InvalidOperationException("Tried to move before first element.");
mi_Index = li_Index;
mi_BlockOffset += ai_Count;
if (mi_BlockOffset >= k_Deque.mi_BlockSize || mi_BlockOffset < 0)
mi_BlockIndex = mk_Deque.CalcBlockAndPos(mi_Index, out mi_BlockOffset);
}
public override int Distance(k_Iterator ak_Iter)
{
return mi_Index - ((k_BlockIterator)ak_Iter).mi_Index;
}
public override object Collection
{
get { return mk_Deque; }
}
public override object Current
{
get
{
if (mi_Index < 0 || mi_Index >= mk_Deque.mi_Count)
throw new k_InvalidPositionException();
return mk_Deque.mk_Blocks[mi_BlockIndex][mi_BlockOffset];
}
set
{
if (mi_Index < 0 || mi_Index >= mk_Deque.mi_Count)
throw new k_InvalidPositionException();
mk_Deque.mk_Blocks[mi_BlockIndex][mi_BlockOffset] = value;
}
}
public override bool Equals(object ak_Obj)
{
k_BlockIterator lk_Iter = ak_Obj as k_BlockIterator;
if (lk_Iter == null)
return false;
return (mi_Index == lk_Iter.mi_Index) && object.ReferenceEquals(this.Collection, lk_Iter.Collection);
}
public override int GetHashCode()
{
return mk_Deque.GetHashCode() ^ mi_Index;
}
public override k_Iterator Clone()
{
return new k_BlockIterator(mk_Deque, mi_Index);
}
internal int Index
{
get { return mi_Index; }
}
}
private class k_PinnedBlockIterator : k_BlockIterator
{
public k_PinnedBlockIterator(k_Deque ak_Deque, int ai_Index)
: base(ak_Deque, ai_Index)
{
}
public override void Move(int ai_Count)
{
throw new k_IteratorPinnedException();
}
}
#endregion
private const int mi_BlockSize = 16;
private object[][] mk_Blocks;
private int mi_Offset;
private int mi_Count;
public k_Deque()
: this(mi_BlockSize)
{
}
public k_Deque(int ai_Capacity)
{
if (ai_Capacity < 0)
throw new ArgumentException("Capacity must be positive.", "ai_Capacity");
mk_Blocks = new object[(ai_Capacity+mi_BlockSize-1)/mi_BlockSize][];
for (int i=0; i<mk_Blocks.Length; ++i)
mk_Blocks[i] = new object[mi_BlockSize];
}
// IContainer Members
public k_Iterator Begin
{
get { return new k_PinnedBlockIterator(this, 0); }
}
public k_Iterator End
{
get { return new k_PinnedBlockIterator(this, mi_Count); }
}
public bool IsEmpty
{
get { return (this.Count == 0); }
}
public k_Iterator Find(object ak_Value)
{
return k_Algorithm.Find(this.Begin, this.End, ak_Value);
}
public k_Iterator Erase(k_Iterator ak_Where)
{
return Erase(ak_Where, ak_Where + 1);
}
public k_Iterator Erase(k_Iterator ak_First, k_Iterator ak_Last)
{
if (ak_First == ak_Last)
return ak_Last;
int li_FirstIndex = ((k_BlockIterator)ak_First).Index;
int li_Count = ak_Last - ak_First;
int li_LastCount = this.End - ak_Last;
if (li_FirstIndex < li_LastCount)
{
k_Algorithm.CopyBackward(this.Begin, ak_First, ak_Last);
k_Algorithm.Fill(this.Begin, ak_First, null);
mi_Offset += li_Count;
mi_Offset %= (mk_Blocks.Length * mi_BlockSize);
}
else
{
k_Algorithm.Copy(ak_Last, this.End, ak_First);
k_Algorithm.Fill(ak_Last, this.End, null);
}
mi_Count -= li_Count;
return new k_BlockIterator(this, li_FirstIndex);
}
// ISequence Members
public object Front
{
get { return this.Begin.Current; }
set { this.Begin.Current = value; }
}
public object Back
{
get { return (this.End-1).Current; }
set { (this.End-1).Current = value; }
}
public void PushFront(object ak_Value)
{
if (mi_Offset % mi_BlockSize == 0 // currently on block boundary
&& mk_Blocks.Length * mi_BlockSize - mi_Count < mi_BlockSize)
{
AllocateBlock(mi_BlockSize);
}
if (mi_Offset == 0)
mi_Offset = mk_Blocks.Length * mi_BlockSize;
--mi_Offset;
mk_Blocks[mi_Offset/mi_BlockSize][mi_Offset%mi_BlockSize] = ak_Value;
++mi_Count;
}
public void PopFront()
{
Erase(this.Begin);
}
public void PushBack(object ak_Value)
{
if ((mi_Offset+mi_Count) % mi_BlockSize == 0 // currently on block boundary
&& mk_Blocks.Length * mi_BlockSize - mi_Count < mi_BlockSize)
{
AllocateBlock(mi_BlockSize);
}
int li_Pos = mi_Offset + mi_Count;
int li_Block = li_Pos/mi_BlockSize;
if (li_Block >= mk_Blocks.Length)
li_Block -= mk_Blocks.Length;
mk_Blocks[li_Block][li_Pos%mi_BlockSize] = ak_Value;
++mi_Count;
}
public void PopBack()
{
Erase(this.End-1);
}
public void Assign(k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
Clear();
Insert(this.End, ak_SrcBegin, ak_SrcEnd);
}
public void Assign(object ak_Value, int ai_Count)
{
Clear();
for (int i = 0; i < ai_Count; ++i)
Insert(this.End, ak_Value);
}
public void Insert(k_Iterator ak_Where, object ak_Value)
{
if (ak_Where == this.Begin)
PushFront(ak_Value);
else if (ak_Where == this.End)
PushBack(ak_Value);
else
{
int li_Index = ((k_BlockIterator)ak_Where).Index;
if (mk_Blocks.Length * mi_BlockSize - mi_Count < mi_BlockSize)
AllocateBlock(mi_BlockSize);
++mi_Count;
if (li_Index < mi_Count/2)
{
if (mi_Offset == 0)
mi_Offset = mk_Blocks.Length * mi_BlockSize;
--mi_Offset;
k_Iterator lk_Dest = k_Algorithm.Copy(this.Begin+1, this.Begin+li_Index+1, this.Begin);
lk_Dest.Current = ak_Value;
}
else
{
// count has been incremented - there is a free element at the end
k_Iterator lk_Dest = this.Begin + li_Index;
k_Algorithm.CopyBackward(lk_Dest, this.End - 1, this.End);
lk_Dest.Current = ak_Value;
}
}
}
public void Insert(k_Iterator ak_Where, k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
int li_FirstIndex = ((k_BlockIterator)ak_Where).Index;
int li_Count = ak_SrcEnd - ak_SrcBegin;
if (mk_Blocks.Length * mi_BlockSize <= mi_Count + li_Count + mi_BlockSize)
AllocateBlock(li_Count);
mi_Count += li_Count;
k_Iterator lk_Dest;
if (li_FirstIndex < li_Count/2)
{
if (mi_Offset == 0)
mi_Offset = mk_Blocks.Length * mi_BlockSize;
mi_Offset -= li_Count;
lk_Dest = k_Algorithm.Copy(this.Begin+li_Count, this.Begin+li_FirstIndex+li_Count, this.Begin);
}
else
{
// count has been incremented - there are li_Count free elements at the end
lk_Dest = this.Begin + li_FirstIndex;
k_Algorithm.CopyBackward(lk_Dest, this.End - li_Count, this.End);
}
k_Algorithm.Copy(ak_SrcBegin, ak_SrcEnd, lk_Dest);
}
#region IList Members
public int Add(object ak_Value)
{
PushBack(ak_Value);
return mi_Count;
}
public void Clear()
{
for (int i=0; i<mk_Blocks.Length; ++i)
mk_Blocks[i] = new object[mi_BlockSize];
mi_Count = 0;
mi_Offset = 0;
}
public bool Contains(object ak_Value)
{
return (Find(ak_Value) != this.End);
}
public int IndexOf(object ak_Value)
{
k_Iterator lk_Found = Find(ak_Value);
if (lk_Found == this.End)
return -1;
return ((k_BlockIterator)lk_Found).Index;
}
public void Insert(int ai_Index, object ak_Value)
{
Insert(this.Begin + ai_Index, ak_Value);
}
public void Remove(object ak_Value)
{
Erase(Find(ak_Value));
}
public void RemoveAt(int ai_Index)
{
Erase(this.Begin + ai_Index);
}
public bool IsReadOnly
{
get { return false; }
}
public bool IsFixedSize
{
get { return false; }
}
public object this[int ai_Index]
{
get
{
if (ai_Index >= mi_Count || ai_Index < 0)
throw new ArgumentOutOfRangeException("Position out of boundary");
int li_Pos, li_Block = CalcBlockAndPos(ai_Index, out li_Pos);
return mk_Blocks[li_Block][li_Pos];
}
set
{
if (ai_Index >= mi_Count || ai_Index < 0)
throw new ArgumentOutOfRangeException("Position out of boundary");
int li_Pos, li_Block = CalcBlockAndPos(ai_Index, out li_Pos);
mk_Blocks[li_Block][li_Pos] = value;
}
}
#endregion
#region ICollection Members
public void CopyTo(Array ak_Array, int ai_Index)
{
for (k_Iterator lk_Iter = this.Begin.Clone(); lk_Iter != this.End; lk_Iter.Next())
ak_Array.SetValue(lk_Iter.Current, ai_Index++);
}
public int Count
{
get { return mi_Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get { return this; }
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return new k_IteratorEnumerator(this.Begin, this.End);
}
#endregion
#region ICloneable Members
public object Clone()
{
k_Deque lk_Clone = new k_Deque(this.Count);
lk_Clone.Insert(lk_Clone.End, this.Begin, this.End);
return lk_Clone;
}
#endregion
private void AllocateBlock(int ai_MinElements)
{
// number of new blocks - grow by half block count (150%)
int li_Increment = mk_Blocks.Length / 2;
if (ai_MinElements > li_Increment*mi_BlockSize)
li_Increment = (ai_MinElements + mi_BlockSize - 1)/mi_BlockSize;
object[][] lk_NewBlocks = new object[mk_Blocks.Length + li_Increment][];
// first move all blocks after offset to front
int li_StartBlock = mi_Offset / mi_BlockSize;
int li_BackCount = mk_Blocks.Length - li_StartBlock;
Array.Copy(mk_Blocks, li_StartBlock, lk_NewBlocks, 0, li_BackCount);
int li_TotalOld = li_BackCount;
// second move all blocks before offset to end
int li_FrontCount = (mi_Offset + mi_Count + mi_BlockSize - 1) / mi_BlockSize - mk_Blocks.Length;
if (li_FrontCount > 0)
{
Array.Copy(mk_Blocks, 0, lk_NewBlocks, li_BackCount, li_FrontCount);
li_TotalOld += li_FrontCount;
}
// actually create new empty blocks
for (int i=li_TotalOld; i < li_TotalOld+li_Increment; ++i)
lk_NewBlocks[i] = new object[mi_BlockSize];
mk_Blocks = lk_NewBlocks;
mi_Offset %= mi_BlockSize;
}
private int CalcBlockAndPos(int ai_Index, out int ai_Pos)
{
ai_Pos = mi_Offset + ai_Index;
int li_BlockIndex = ai_Pos / mi_BlockSize;
if (li_BlockIndex >= mk_Blocks.Length)
li_BlockIndex -= mk_Blocks.Length;
ai_Pos %= mi_BlockSize;
return li_BlockIndex;
}
}
}

View File

@@ -0,0 +1,658 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// A HashTable with iterators
/// </summary>
public class k_HashTable : IMap
{
#region static helper functions
private readonly static int[] mk_Primes =
{
11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521, 631, 761, 919,
1103, 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591,
17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431, 90523, 108631, 130363,
156437, 187751, 225307, 270371, 324449, 389357, 467237, 560689, 672827, 807403,
968897, 1162687, 1395263, 1674319, 2009191, 2411033, 2893249, 3471899, 4166287,
4999559, 5999471, 7199369
};
private static bool IsPrime(int ai_Number)
{
if ((ai_Number & 1) == 0)
return (ai_Number == 2);
int li_Max = (int)Math.Sqrt(ai_Number);
for (int li_Div=3; li_Div < li_Max; li_Div+=2)
{
if ((ai_Number % li_Div) == 0)
return false;
}
return true;
}
private static int FindPrimeGreater(int ai_Min)
{
if (ai_Min < 0)
throw new ArgumentException("k_HashTable capacity overflow.");
// do binary search lookup in primes array
int li_Pos = Array.BinarySearch(mk_Primes, ai_Min);
if (li_Pos >= 0)
return mk_Primes[li_Pos];
li_Pos = ~li_Pos;
if (li_Pos < mk_Primes.Length)
return mk_Primes[li_Pos];
// ai_Min is greater than highest number in mk_Primes
for (int i = (ai_Min|1); i <= Int32.MaxValue; i+=2)
{
if (IsPrime(i))
return i;
}
return ai_Min;
}
#endregion
#region Bucket Structure
private struct r_Bucket
{
public object mk_Key;
public object mk_Value;
public int mi_HashCode; // MSB (sign bit) indicates a collision.
}
#endregion
#region k_BucketIterator Implementation
private class k_BucketIterator : k_Iterator
{
private readonly k_HashTable mk_Table;
private int mi_Index;
public k_BucketIterator(k_HashTable ak_Table, int ai_Index)
{
mk_Table = ak_Table;
mi_Index = -1;
if (ai_Index >= 0)
mi_Index = FindNext(ai_Index-1);
}
public override object Current
{
get
{
if (mi_Index < 0 || mk_Table.mk_Buckets[mi_Index].mk_Key == null)
throw new k_InvalidPositionException();
r_Bucket lr_Bucket = mk_Table.mk_Buckets[mi_Index];
return new DictionaryEntry(lr_Bucket.mk_Key, lr_Bucket.mk_Value);
}
set
{
if (mi_Index < 0 || mk_Table.mk_Buckets[mi_Index].mk_Key == null)
throw new k_InvalidPositionException();
DictionaryEntry lr_Entry = (DictionaryEntry)value;
r_Bucket lr_Bucket = mk_Table.mk_Buckets[mi_Index];
if (mk_Table.mk_Comparer.Compare(lr_Entry.Key, lr_Bucket.mk_Key) != 0)
throw new ArgumentException("Key values must not be changed.");
mk_Table.mk_Buckets[mi_Index].mk_Value = lr_Entry.Value;
}
}
public override void Move(int ai_Count)
{
int li_NewIndex = mi_Index;
if (ai_Count > 0)
{
while (ai_Count-- > 0)
{
if (li_NewIndex < 0)
throw new InvalidOperationException("Tried to moved beyond end element.");
li_NewIndex = FindNext(li_NewIndex);
}
}
else
{
while (ai_Count++ < 0)
{
if (li_NewIndex < 0)
li_NewIndex = FindPrev(mk_Table.mk_Buckets.Length);
else
li_NewIndex = FindPrev(li_NewIndex);
if (li_NewIndex < 0)
throw new InvalidOperationException("Tried to move before first element.");
}
}
mi_Index = li_NewIndex;
}
public override int Distance(k_Iterator ak_Iter)
{
k_BucketIterator lk_Iter = ak_Iter as k_BucketIterator;
if (lk_Iter == null || !object.ReferenceEquals(lk_Iter.Collection, this.Collection))
throw new ArgumentException("Cannot determine distance of iterators belonging to different collections.");
k_Iterator lk_End = mk_Table.End;
int li_IndexDiff;
if (this != lk_End && ak_Iter != lk_End)
li_IndexDiff = mi_Index - lk_Iter.mi_Index;
else
li_IndexDiff = (this == lk_End) ? 1 : -1; // 1 is also fine when both are End
if (li_IndexDiff < 0)
{
int li_Diff = 0;
k_Iterator lk_Bck = this.Clone();
for (; lk_Bck != ak_Iter && lk_Bck != lk_End; lk_Bck.Next())
--li_Diff;
if (lk_Bck == ak_Iter)
return li_Diff;
}
else
{
int li_Diff = 0;
k_Iterator lk_Fwd = ak_Iter.Clone();
for (; lk_Fwd != this && lk_Fwd != lk_End; lk_Fwd.Next())
++li_Diff;
if (lk_Fwd == this)
return li_Diff;
}
throw new Exception("Inconsistent state. Concurrency?");
}
public override object Collection
{
get { return mk_Table; }
}
public override bool Equals(object ak_Obj)
{
k_BucketIterator lk_Iter = ak_Obj as k_BucketIterator;
if (lk_Iter == null)
return false;
return (mi_Index == lk_Iter.mi_Index && object.ReferenceEquals(mk_Table, lk_Iter.mk_Table));
}
public override int GetHashCode()
{
return mk_Table.GetHashCode() ^ mi_Index;
}
public override k_Iterator Clone()
{
return new k_BucketIterator(mk_Table, mi_Index);
}
private int FindPrev(int ai_Index)
{
--ai_Index;
r_Bucket[] lk_Buckets = mk_Table.mk_Buckets;
while (ai_Index >= 0 && lk_Buckets[ai_Index].mk_Key == null)
--ai_Index;
if (ai_Index < -1)
return -1;
return ai_Index;
}
private int FindNext(int ai_Index)
{
++ai_Index;
r_Bucket[] lk_Buckets = mk_Table.mk_Buckets;
while (ai_Index < lk_Buckets.Length && lk_Buckets[ai_Index].mk_Key == null)
++ai_Index;
if (ai_Index >= lk_Buckets.Length)
return -1;
return ai_Index;
}
internal int Index
{
get { return mi_Index; }
}
}
private class k_PinnedBucketIterator : k_BucketIterator
{
public k_PinnedBucketIterator(k_HashTable ak_Table, int ai_Index)
: base(ak_Table, ai_Index)
{
}
public override void Move(int ai_Count)
{
throw new k_IteratorPinnedException();
}
}
#endregion
private IHashCodeProvider mk_HashProvider;
private IComparer mk_Comparer;
private double md_LoadFactor;
private int mi_GrowSize;
private r_Bucket[] mk_Buckets;
private int mi_Count;
private readonly k_Iterator mk_End;
public k_HashTable()
: this(0, 0.72)
{
}
public k_HashTable(int ai_Capacity, double ad_LoadFactor)
: this(ai_Capacity, ad_LoadFactor, null, null)
{
}
public k_HashTable(int ai_Capacity, double ad_LoadFactor, IHashCodeProvider ak_HashProvider, IComparer ak_Comparer)
{
if (ad_LoadFactor <= .0 || ad_LoadFactor > 1.0)
throw new ArgumentException("Load factor must be greater than .0 and smaller or equal to 1.0", "ad_LoadFactor");
md_LoadFactor = ad_LoadFactor;
double ld_Size = ai_Capacity/ad_LoadFactor;
if (ld_Size > int.MaxValue)
throw new ArgumentException("k_HashTable overflow");
int li_TableSize = FindPrimeGreater((int)ld_Size);
mk_Buckets = new r_Bucket[li_TableSize];
mi_GrowSize = (md_LoadFactor < 1.0) ? (int)(md_LoadFactor * li_TableSize) : li_TableSize-1;
mk_HashProvider = ak_HashProvider;
mk_Comparer = ak_Comparer;
mk_End = new k_PinnedBucketIterator(this, -1);
}
// IContainer Members
public k_Iterator Begin
{
get
{
if (mi_Count == 0)
return mk_End;
return new k_PinnedBucketIterator(this, 0);
}
}
public k_Iterator End
{
get { return mk_End; }
}
public bool IsEmpty
{
get { return (mi_Count == 0); }
}
public k_Iterator Find(object ak_Value)
{
DictionaryEntry lr_Item = (DictionaryEntry)ak_Value;
int li_Index = FindBucket(lr_Item.Key);
if (li_Index < 0 || !object.Equals(mk_Buckets[li_Index].mk_Value, lr_Item.Value))
return this.End;
return new k_BucketIterator(this, li_Index);
}
public k_Iterator Erase(k_Iterator ak_Where)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this collection.");
k_Iterator lk_Successor = ak_Where + 1;
EmptyBucket(((k_BucketIterator)ak_Where).Index);
return lk_Successor;
}
public k_Iterator Erase(k_Iterator ak_First, k_Iterator ak_Last)
{
if (ak_First == this.Begin && ak_Last == this.End)
{
Clear();
return ak_Last.Clone();
}
k_Iterator lk_Current = ak_First;
while (lk_Current != ak_Last)
lk_Current = Erase(lk_Current);
return lk_Current;
}
// IMap Members
public k_Iterator FindKey(object ak_Key)
{
return new k_BucketIterator(this, FindBucket(ak_Key));
}
public void Add(DictionaryEntry ar_Item)
{
Add(ar_Item.Key, ar_Item.Value);
}
public void Insert(k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
for (k_Iterator lk_Iter = ak_SrcBegin.Clone(); lk_Iter != ak_SrcEnd; lk_Iter.Next())
Add((DictionaryEntry)lk_Iter.Current);
}
#region IDictionary Members
public void Add(object ak_Key, object ak_Value)
{
SetValue(ak_Key, ak_Value, true);
}
public void Clear()
{
if (mi_Count == 0)
return;
for (int i=0; i < mk_Buckets.Length; ++i)
mk_Buckets[i] = new r_Bucket();
mi_Count = 0;
}
public bool Contains(object ak_Key)
{
return (FindBucket(ak_Key) >= 0);
}
public void Remove(object ak_Key)
{
EmptyBucket(FindBucket(ak_Key));
}
public IDictionaryEnumerator GetEnumerator()
{
return new k_IteratorDictEnumerator(this.Begin, this.End);
}
public bool IsReadOnly
{
get { return false; }
}
public bool IsFixedSize
{
get { return false; }
}
public object this[object ak_Key]
{
get
{
int li_Index = FindBucket(ak_Key);
if (li_Index < 0)
return null;
return mk_Buckets[li_Index].mk_Value;
}
set
{
SetValue(ak_Key, value, false);
}
}
public ICollection Keys
{
get
{
int i = 0;
object[] lk_Keys = new object[mi_Count];
foreach (DictionaryEntry lr_Entry in this)
lk_Keys[i++] = lr_Entry.Key;
return lk_Keys;
}
}
public ICollection Values
{
get
{
int i=0;
object[] lk_Values = new object[mi_Count];
foreach (DictionaryEntry lr_Entry in this)
lk_Values[i++] = lr_Entry.Value;
return lk_Values;
}
}
#endregion
#region ICollection Members
public void CopyTo(Array ak_Array, int ai_Index)
{
foreach (DictionaryEntry lr_Entry in this)
ak_Array.SetValue(lr_Entry, ai_Index++);
}
public int Count
{
get { return mi_Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get { return this; }
}
#endregion
#region IEnumerable Members
IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return new k_IteratorEnumerator(this.Begin, this.End);
}
#endregion
#region ICloneable Members
public object Clone()
{
k_HashTable lk_Clone = new k_HashTable(this.Count, md_LoadFactor, mk_HashProvider, mk_Comparer);
int i = mk_Buckets.Length;
while (i-- > 0)
{
object lk_Key = mk_Buckets[i].mk_Key;
if (lk_Key != null)
lk_Clone[lk_Key] = mk_Buckets[i].mk_Value;
}
return lk_Clone;
}
#endregion
private void EmptyBucket(int ai_Index)
{
if (ai_Index < 0 || ai_Index >= mk_Buckets.Length)
return;
if (mk_Buckets[ai_Index].mk_Key == null)
throw new InvalidOperationException("Key was removed earlier.");
mk_Buckets[ai_Index].mi_HashCode &= unchecked((int)0x80000000);
mk_Buckets[ai_Index].mk_Key = null;
mk_Buckets[ai_Index].mk_Value = null;
--mi_Count;
}
private int FindBucket(object ak_Key)
{
if (ak_Key == null)
throw new ArgumentException("Key must not be null.", "ak_Key");
uint lui_BucketCount = (uint)mk_Buckets.Length;
uint lui_Increment;
uint lui_HashCode = ComputeHashAndStep(ak_Key, out lui_Increment);
uint lui_Walker = lui_HashCode % lui_BucketCount;
r_Bucket lr_Bucket;
do
{
int li_Index = (int)lui_Walker;
lr_Bucket = mk_Buckets[li_Index];
if (lr_Bucket.mk_Key == null && lr_Bucket.mi_HashCode >= 0)
break; // stop on empty non-duplicate
if ((lr_Bucket.mi_HashCode & 0x7fffffff) == lui_HashCode
&& EqualsHelper(lr_Bucket.mk_Key, ak_Key))
return li_Index;
lui_Walker += lui_Increment;
lui_Walker %= lui_BucketCount;
}
while (lr_Bucket.mi_HashCode < 0 && lui_Walker != lui_HashCode);
return -1; // not found
}
private void SetValue(object ak_Key, object ak_Value, bool ab_Add)
{
if (mi_Count >= mi_GrowSize)
ExpandBucketsArray();
uint lui_BucketCount = (uint)mk_Buckets.Length;
uint lui_Increment;
uint lui_HashCode = ComputeHashAndStep(ak_Key, out lui_Increment);
r_Bucket lr_Bucket;
int li_Free = -1;
uint lui_Walker = lui_HashCode % lui_BucketCount;
do
{
int li_Index = (int)lui_Walker;
lr_Bucket = mk_Buckets[li_Index];
if (li_Free < 0 && lr_Bucket.mk_Key == null && lr_Bucket.mi_HashCode < 0)
li_Free = li_Index;
if (lr_Bucket.mk_Key == null && (lr_Bucket.mi_HashCode & unchecked(0x80000000)) == 0)
{
if (li_Free >= 0)
li_Index = li_Free;
mk_Buckets[li_Index].mk_Key = ak_Key;
mk_Buckets[li_Index].mk_Value = ak_Value;
mk_Buckets[li_Index].mi_HashCode |= (int)lui_HashCode;
++mi_Count;
return;
}
if ((lr_Bucket.mi_HashCode & 0x7fffffff) == lui_HashCode
&& EqualsHelper(lr_Bucket.mk_Key, ak_Key))
{
if (ab_Add)
throw new ArgumentException("duplicate key");
mk_Buckets[li_Index].mk_Value = ak_Value;
return;
}
// mark all as dupes as long as we have not found a free bucket
if (li_Free == -1)
mk_Buckets[li_Index].mi_HashCode |= unchecked((int)0x80000000);
lui_Walker += lui_Increment;
lui_Walker %= lui_BucketCount;
}
while (lui_Walker != lui_HashCode);
if (li_Free == -1)
throw new InvalidOperationException("Corrupted hash table. Insert failed.");
mk_Buckets[li_Free].mk_Key = ak_Key;
mk_Buckets[li_Free].mk_Value = ak_Value;
mk_Buckets[li_Free].mi_HashCode |= (int)lui_HashCode;
++mi_Count;
}
private static void InternalExpandInsert(r_Bucket[] ak_Buckets, r_Bucket ar_Bucket)
{
ar_Bucket.mi_HashCode &= 0x7fffffff;
uint lui_BucketCount = (uint)ak_Buckets.Length;
uint lui_Increment = (uint)(1 + ((((uint)ar_Bucket.mi_HashCode >> 5) + 1) % (lui_BucketCount - 1)));
uint lui_Walker = (uint)ar_Bucket.mi_HashCode % lui_BucketCount;
for (;;)
{
int li_Index = (int)lui_Walker;
if (ak_Buckets[li_Index].mk_Key == null)
{
ak_Buckets[li_Index] = ar_Bucket;
return;
}
// since current bucket is occupied mark it as duplicate
ak_Buckets[li_Index].mi_HashCode |= unchecked((int)0x80000000);
lui_Walker += lui_Increment;
lui_Walker %= lui_BucketCount;
}
}
private void ExpandBucketsArray()
{
int li_NewSize = FindPrimeGreater(mk_Buckets.Length * 2);
r_Bucket[] lk_Buckets = new r_Bucket[li_NewSize];
foreach (r_Bucket lr_Bucket in mk_Buckets)
{
if (lr_Bucket.mk_Key == null)
continue;
InternalExpandInsert(lk_Buckets, lr_Bucket);
}
mk_Buckets = lk_Buckets;
mi_GrowSize = (md_LoadFactor < 1.0) ? (int)(md_LoadFactor * li_NewSize) : li_NewSize-1;
}
private uint ComputeHashAndStep(object ak_Key, out uint aui_Increment)
{
// mask the sign bit (our collision indicator)
uint lui_HashCode = (uint)GetHashHelper(ak_Key) & 0x7fffffff;
// calc increment value relatively prime to mk_Buckets.Length
aui_Increment = (uint)(1 + (((lui_HashCode >> 5) + 1) % ((uint)mk_Buckets.Length - 1)));
return lui_HashCode;
}
private int GetHashHelper(object ak_Key)
{
if (mk_HashProvider != null)
return mk_HashProvider.GetHashCode(ak_Key);
return ak_Key.GetHashCode();
}
private bool EqualsHelper(object ak_ObjA, object ak_ObjB)
{
if (mk_Comparer != null)
return (mk_Comparer.Compare(ak_ObjA, ak_ObjB) == 0);
return Object.Equals(ak_ObjA, ak_ObjB);
}
}
}

View File

@@ -0,0 +1,323 @@
using System;
using System.Collections;
namespace System.util.collections
{
public abstract class k_Iterator : IComparable, ICloneable
{
public static k_Iterator operator ++(k_Iterator ak_Iter)
{
return ak_Iter + 1;
}
public static k_Iterator operator --(k_Iterator ak_Iter)
{
return ak_Iter - 1;
}
public static k_Iterator operator +(k_Iterator ak_Iter, int ai_Distance)
{
k_Iterator lk_Clone = ak_Iter.Clone();
lk_Clone.Move(ai_Distance);
return lk_Clone;
}
public static k_Iterator operator -(k_Iterator ak_Iter, int ai_Distance)
{
k_Iterator lk_Clone = ak_Iter.Clone();
lk_Clone.Move(-ai_Distance);
return lk_Clone;
}
public static int operator -(k_Iterator ak_Left, k_Iterator ak_Right)
{
return ak_Left.Distance(ak_Right);
}
public static bool operator ==(k_Iterator ak_Left, k_Iterator ak_Right)
{
return object.Equals(ak_Left, ak_Right);
}
public static bool operator !=(k_Iterator ak_Left, k_Iterator ak_Right)
{
return !object.Equals(ak_Left, ak_Right);
}
public override bool Equals(object ak_Obj)
{
return base.Equals(ak_Obj); // implemented to avoid warning
}
public override int GetHashCode()
{
return base.GetHashCode(); // implemented to avoid warning
}
public int CompareTo(object ak_Obj)
{
k_Iterator lk_Iter = ak_Obj as k_Iterator;
if (lk_Iter == null || !object.ReferenceEquals(lk_Iter.Collection, this.Collection))
throw new ArgumentException("Cannot compare iterators of different origin.");
return Distance(lk_Iter);
}
object ICloneable.Clone()
{
return this.Clone();
}
public void Next()
{
Move(1);
}
public void Prev()
{
Move(-1);
}
public abstract object Current { get; set; }
public abstract object Collection { get; }
public abstract k_Iterator Clone();
public abstract void Move(int ai_Count);
public abstract int Distance(k_Iterator ak_Iter);
}
public class k_IteratorEnumerator : IEnumerator
{
protected k_Iterator mk_Current;
protected k_Iterator mk_Begin, mk_End;
protected bool mb_Fresh;
public k_IteratorEnumerator(k_Iterator ak_Begin, k_Iterator ak_End)
{
mk_Begin = ak_Begin;
mk_End = ak_End;
mb_Fresh = true;
}
#region IEnumerator Members
public bool MoveNext()
{
if (mb_Fresh)
{
mk_Current = mk_Begin.Clone();
mb_Fresh = false;
}
else if (mk_Current != mk_End)
mk_Current.Next();
return (mk_Current != mk_End);
}
public void Reset()
{
mb_Fresh = true;
mk_Current = null;
}
public object Current
{
get
{
if (mb_Fresh || mk_Current == mk_End)
throw new InvalidOperationException("The enumerator is positioned before the first element of the collection or after the last element.");
return mk_Current.Current;
}
}
#endregion
}
public class k_IteratorDictEnumerator : k_IteratorEnumerator, IDictionaryEnumerator
{
public k_IteratorDictEnumerator(k_Iterator ak_Begin, k_Iterator ak_End)
: base(ak_Begin, ak_End)
{
}
#region IDictionaryEnumerator Members
public object Key
{
get { return this.Entry.Key; }
}
public object Value
{
get { return this.Entry.Value; }
}
public DictionaryEntry Entry
{
get { return (DictionaryEntry)this.Current; }
}
#endregion
}
public class k_IListIterator : k_Iterator
{
private readonly IList mk_List;
private int mi_Index;
public static k_IListIterator CreateBegin(IList ak_List)
{
return new k_PinnedIListIterator(ak_List, 0);
}
public static k_IListIterator CreateEnd(IList ak_List)
{
return new k_PinnedIListIterator(ak_List, ak_List.Count);
}
public k_IListIterator(IList ak_List, int ai_Index)
{
mk_List = ak_List;
mi_Index = ai_Index;
}
public override void Move(int ai_Count)
{
int li_Index = mi_Index + ai_Count;
if (li_Index < 0)
throw new InvalidOperationException("Tried to move before first element.");
else if (li_Index > mk_List.Count)
throw new InvalidOperationException("Tried to moved beyond end element.");
mi_Index = li_Index;
}
public override int Distance(k_Iterator ak_Iter)
{
return mi_Index - ((k_IListIterator)ak_Iter).mi_Index;
}
public override object Collection
{
get { return mk_List; }
}
public override object Current
{
get
{
if (mi_Index < 0 || mi_Index >= mk_List.Count)
throw new k_InvalidPositionException();
return mk_List[mi_Index];
}
set
{
if (mi_Index < 0 || mi_Index >= mk_List.Count)
throw new k_InvalidPositionException();
mk_List[mi_Index] = value;
}
}
public override bool Equals(object ak_Obj)
{
k_IListIterator lk_Iter = ak_Obj as k_IListIterator;
if (lk_Iter == null)
return false;
return (mi_Index == lk_Iter.mi_Index) && object.ReferenceEquals(this.Collection, lk_Iter.Collection);
}
public override int GetHashCode()
{
return mk_List.GetHashCode() ^ mi_Index;
}
public override k_Iterator Clone()
{
return new k_IListIterator(mk_List, mi_Index);
}
internal int Index
{
get { return mi_Index; }
}
}
internal class k_PinnedIListIterator : k_IListIterator
{
public k_PinnedIListIterator(IList ak_List, int ai_Index)
: base(ak_List, ai_Index)
{
}
public override void Move(int ai_Count)
{
throw new k_IteratorPinnedException();
}
}
public class k_CollectionOnIterators : ICollection
{
private k_Iterator mk_Begin, mk_End;
private int mi_Count;
public k_CollectionOnIterators(k_Iterator ak_Begin, k_Iterator ak_End)
{
mk_Begin = ak_Begin;
mk_End = ak_End;
mi_Count = mk_End - mk_Begin;
}
#region ICollection Members
public int Count
{
get { return mi_Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get { return mk_Begin.Collection; }
}
public void CopyTo(Array ak_Array, int ai_Index)
{
foreach (object lk_Obj in this)
ak_Array.SetValue(lk_Obj, ai_Index++);
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return new k_IteratorEnumerator(mk_Begin, mk_End);
}
#endregion
}
[Serializable]
public class k_IteratorPinnedException : InvalidOperationException
{
public k_IteratorPinnedException()
: base("Cannot move pinned iterator. Use Clone() to create a movable copy.")
{
}
}
[Serializable]
public class k_InvalidPositionException : InvalidOperationException
{
public k_InvalidPositionException()
: base("Iterator positioned on End or invalid element.")
{
}
}
}

View File

@@ -0,0 +1,537 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// A doubly linked list
/// </summary>
public class k_List : ISequence
{
#region k_Node Implementation
private class k_Node
{
private object mk_Value;
public k_Node mk_Prev, mk_Next;
public k_Node(object ak_Value)
{
mk_Value = ak_Value;
}
public object Value
{
get { return mk_Value; }
set { mk_Value = value; }
}
}
#endregion
#region k_NodeIterator Implementation
private class k_NodeIterator : k_Iterator, ICloneable
{
private readonly k_List mk_List;
private k_Node mk_Current;
public k_NodeIterator(k_List ak_List, k_Node ak_Node)
{
mk_List = ak_List;
mk_Current = ak_Node;
}
public override object Current
{
get
{
if (mk_Current == null)
throw new k_InvalidPositionException();
return mk_Current.Value;
}
set
{
if (mk_Current == null)
throw new k_InvalidPositionException();
mk_Current.Value = value;
}
}
public override object Collection
{
get { return mk_List; }
}
public override void Move(int ai_Count)
{
k_Node lk_NewPos = mk_Current;
int li_Count = ai_Count;
if (li_Count > 0)
{
while (li_Count-- > 0)
{
if (lk_NewPos == null)
throw new InvalidOperationException("Tried to moved beyond end element.");
lk_NewPos = lk_NewPos.mk_Next;
}
}
else
{
while (li_Count++ < 0)
{
if (lk_NewPos == null)
lk_NewPos = mk_List.mk_Tail;
else
lk_NewPos = lk_NewPos.mk_Prev;
if (lk_NewPos == null)
throw new InvalidOperationException("Tried to move before first element.");
}
}
#if (DEBUG)
if (ai_Count != 0 && object.ReferenceEquals(mk_Current, lk_NewPos))
throw new IndexOutOfRangeException("Iterator is positioned on invalid node.");
#endif
mk_Current = lk_NewPos;
}
public override int Distance(k_Iterator ak_Iter)
{
k_NodeIterator lk_Iter = (k_NodeIterator)ak_Iter;
if (!this.IsValid || !lk_Iter.IsValid)
throw new ArgumentException("Iterator is invalid.");
int li_Diff = 0;
k_Iterator lk_End = mk_List.End;
k_Iterator lk_Fwd = lk_Iter.Clone();
for (; lk_Fwd != this && lk_Fwd != lk_End; lk_Fwd.Next())
++li_Diff;
if (lk_Fwd == this)
return li_Diff;
li_Diff = 0;
k_Iterator lk_Bck = this.Clone();
for (; lk_Bck != lk_Iter && lk_Bck != lk_End; lk_Bck.Next())
--li_Diff;
if (lk_Bck == lk_Iter)
return li_Diff;
throw new Exception("Inconsistent state. Concurrency?");
}
public override bool Equals(object ak_Obj)
{
k_NodeIterator lk_Iter = ak_Obj as k_NodeIterator;
if (lk_Iter == null)
return false;
return object.ReferenceEquals(mk_Current, lk_Iter.mk_Current);
}
public override int GetHashCode()
{
if (mk_Current == null)
return mk_List.GetHashCode();
return mk_Current.GetHashCode();
}
public override k_Iterator Clone()
{
return new k_NodeIterator(mk_List, mk_Current);
}
internal k_Node Node
{
get { return mk_Current; }
}
internal bool IsValid
{
get { return (mk_Current == null || (!object.ReferenceEquals(mk_Current.mk_Next, mk_Current) && !object.ReferenceEquals(mk_Current.mk_Prev, mk_Current))); }
}
}
private class k_PinnedNodeIterator : k_NodeIterator
{
public k_PinnedNodeIterator(k_List ak_List, k_Node ak_Node)
: base(ak_List, ak_Node)
{
}
public override void Move(int ai_Count)
{
throw new k_IteratorPinnedException();
}
}
#endregion
private int mi_Count;
private k_Node mk_Head, mk_Tail;
private k_Iterator mk_Begin, mk_End;
public k_List()
{
mk_End = new k_PinnedNodeIterator(this, null);
mk_Begin = mk_End;
}
// IContainer Members
public k_Iterator Begin
{
get
{
if (mi_Count == 0)
return mk_End;
if (mk_Begin == null)
mk_Begin = new k_PinnedNodeIterator(this, mk_Head);
return mk_Begin;
}
}
public k_Iterator End
{
get { return mk_End; }
}
public bool IsEmpty
{
get { return (mi_Count == 0); }
}
public k_Iterator Find(object ak_Value)
{
return k_Algorithm.Find(this.Begin, this.End, ak_Value);
}
public k_Iterator Erase(k_Iterator ak_Where)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this list.");
if (ak_Where == this.End)
return this.End;
return Erase(ak_Where, ak_Where + 1);
}
public k_Iterator Erase(k_Iterator ak_First, k_Iterator ak_Last)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_First.Collection) && object.ReferenceEquals(this, ak_Last.Collection), "Iterators do not belong to this collection.");
int li_Distance = ak_Last - ak_First;
if (li_Distance == 0)
return ak_Last;
k_Node lk_First = ((k_NodeIterator)ak_First).Node;
k_Node lk_Prev = lk_First.mk_Prev;
k_Node lk_Next = (ak_Last != this.End) ? ((k_NodeIterator)ak_Last).Node : null;
if (lk_Prev != null)
lk_Prev.mk_Next = lk_Next;
else
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(mk_Head, lk_First), "Inconsistent list state");
mk_Head = lk_Next;
mk_Begin = null;
}
if (lk_Next != null)
lk_Next.mk_Prev = lk_Prev;
else
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(mk_Tail, ((k_NodeIterator)(ak_Last-1)).Node), "Inconsistent list state");
mk_Tail = lk_Prev;
}
mi_Count -= li_Distance;
#if (DEBUG)
// create invalid nodes linking to itself
k_Node lk_Node = lk_First;
while (lk_Node != null && lk_Node != lk_Next)
{
k_Node lk_Tmp = lk_Node.mk_Next;
lk_Node.mk_Next = lk_Node;
lk_Node.mk_Prev = lk_Node;
lk_Node = lk_Tmp;
}
#endif
return ak_Last;
}
// ISequence Members
public object Front
{
get
{
if (this.IsEmpty)
throw new InvalidOperationException("Empty list");
return mk_Head.Value;
}
set
{
if (this.IsEmpty)
throw new InvalidOperationException("Empty list");
mk_Head.Value = value;
}
}
public object Back
{
get
{
if (this.IsEmpty)
throw new InvalidOperationException("Empty list");
return mk_Tail.Value;
}
set
{
if (this.IsEmpty)
throw new InvalidOperationException("Empty list");
mk_Tail.Value = value;
}
}
public void PushFront(object ak_Value)
{
Insert(this.Begin, ak_Value);
}
public void PopFront()
{
Erase(this.Begin);
}
public void PushBack(object ak_Value)
{
Insert(this.End, ak_Value);
}
public void PopBack()
{
Erase(this.End-1);
}
public void Assign(k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
Clear();
Insert(this.End, ak_SrcBegin, ak_SrcEnd);
}
public void Assign(object ak_Value, int ai_Count)
{
Clear();
Insert(this.End, ak_Value, ai_Count);
}
public void Insert(k_Iterator ak_Where, object ak_Value)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this collection.");
k_Node lk_New = new k_Node(ak_Value);
PasteNodeRange((k_NodeIterator)ak_Where, lk_New, lk_New);
++mi_Count;
}
public void Insert(k_Iterator ak_Where, k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this collection.");
k_Node lk_Start = new k_Node(null), lk_End = lk_Start;
int li_Count = 0;
for (k_Iterator lk_Iter = ak_SrcBegin.Clone(); lk_Iter != ak_SrcEnd; lk_Iter.Next(), ++li_Count)
{
k_Node lk_New = new k_Node(lk_Iter.Current);
lk_End.mk_Next = lk_New;
lk_New.mk_Prev = lk_End;
lk_End = lk_New;
}
if (li_Count > 0)
{
PasteNodeRange((k_NodeIterator)ak_Where, lk_Start.mk_Next, lk_End);
mi_Count += li_Count;
}
}
public void Insert(k_Iterator ak_Where, object ak_Value, int ai_Count)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this collection.");
k_Node lk_Start = new k_Node(null), lk_End = lk_Start;
for (int i=0; i<ai_Count; ++i)
{
k_Node lk_New = new k_Node(ak_Value);
lk_End.mk_Next = lk_New;
lk_New.mk_Prev = lk_End;
lk_End = lk_New;
}
if (ai_Count > 0)
{
PasteNodeRange((k_NodeIterator)ak_Where, lk_Start.mk_Next, lk_End);
mi_Count += ai_Count;
}
}
#region IList Members
public int Add(object ak_Value)
{
Insert(this.End, ak_Value);
return mi_Count;
}
public void Clear()
{
mk_Head = mk_Tail = null;
mk_Begin = mk_End;
mi_Count = 0;
}
public bool Contains(object ak_Value)
{
return (this.Find(ak_Value) != this.End);
}
public int IndexOf(object ak_Value)
{
int li_Index = 0;
foreach (object lk_Val in this)
{
if (object.Equals(lk_Val, ak_Value))
return li_Index;
++li_Index;
}
return -1;
}
void IList.Insert(int ai_Index, object ak_Value)
{
this.Insert(this.Begin + ai_Index, ak_Value);
}
void IList.Remove(object ak_Value)
{
k_NodeIterator lk_Found = (k_NodeIterator)this.Find(ak_Value);
if (lk_Found != this.End)
Erase(lk_Found);
}
public void RemoveAt(int ai_Index)
{
Erase(this.Begin + ai_Index);
}
public bool IsFixedSize
{
get { return false; }
}
bool IList.IsReadOnly
{
get { return false; }
}
public object this[int index]
{
get
{
k_Iterator lk_Iter = this.Begin + index;
return lk_Iter.Current;
}
set
{
k_Iterator lk_Iter = this.Begin + index;
lk_Iter.Current = value;
}
}
#endregion
#region ICollection Members
public void CopyTo(Array ak_Array, int ai_Index)
{
foreach (object lk_Obj in this)
ak_Array.SetValue(lk_Obj, ai_Index++);
}
public int Count
{
get { return mi_Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get { return this; }
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return new k_IteratorEnumerator(this.Begin, this.End);
}
#endregion
#region ICloneable Members
public object Clone()
{
k_List lk_Clone = new k_List();
for (k_Iterator lk_Iter = this.Begin.Clone(); lk_Iter != this.End; lk_Iter.Next())
lk_Clone.Add(lk_Iter.Current);
return lk_Clone;
}
#endregion
private void PasteNodeRange(k_NodeIterator ak_Where, k_Node ak_First, k_Node ak_Last)
{
if (ak_Where != this.End)
{
k_Node lk_Next = ak_Where.Node;
k_Node lk_Prev = lk_Next.mk_Prev;
ak_Last.mk_Next = lk_Next;
ak_First.mk_Prev = lk_Prev;
if (lk_Next != null)
lk_Next.mk_Prev = ak_Last;
if (lk_Prev != null)
lk_Prev.mk_Next = ak_First;
}
else
{
if (mk_Tail != null)
{
mk_Tail.mk_Next = ak_First;
ak_First.mk_Prev = mk_Tail;
}
mk_Tail = ak_Last;
}
if (ak_Where == this.Begin)
{
mk_Head = ak_First;
mk_Begin = null; // recalc on next get
}
}
}
}

View File

@@ -0,0 +1,108 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// k_Queue is a first-in, first-out (FIFO) data structure.
/// It hides functionality of the underlying container (e.g. k_List, k_Deque)
/// and provides a a basic queue class.
/// </summary>
public class k_Queue : ICollection
{
private ISequence mk_Container;
public k_Queue()
: this(typeof(k_Deque))
{
}
public k_Queue(Type ak_ContainerType)
{
mk_Container = Activator.CreateInstance(ak_ContainerType) as ISequence;
if (mk_Container == null)
throw new ArgumentException("Container type must implement ISequence.", "ak_ContainerType");
}
public k_Iterator Begin
{
get { return mk_Container.Begin; }
}
public k_Iterator End
{
get { return mk_Container.End; }
}
public object Front
{
get { return mk_Container.Front; }
}
public object Back
{
get { return mk_Container.Back; }
}
public bool IsEmpty
{
get { return mk_Container.IsEmpty; }
}
public k_Iterator Erase(k_Iterator ak_Where)
{
return mk_Container.Erase(ak_Where);
}
public void Push(object ak_Value)
{
mk_Container.PushBack(ak_Value);
}
public object Pop()
{
object lk_Obj = mk_Container.Front;
mk_Container.PopFront();
return lk_Obj;
}
public IContainer UnderlyingContainer
{
get { return mk_Container; }
}
#region ICollection Members
public void CopyTo(Array ak_Array, int ai_Index)
{
foreach (object lk_Obj in this)
ak_Array.SetValue(lk_Obj, ai_Index++);
}
public int Count
{
get { return mk_Container.Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get { return this; }
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return new k_IteratorEnumerator(mk_Container.Begin, mk_Container.End);
}
#endregion
}
}

View File

@@ -0,0 +1,699 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// A Skip List
/// </summary>
public class k_SkipList : ISortedMap
{
#region k_Node Implementation
private class k_Node
{
private object mk_Key;
private object mk_Value;
private k_Node[] mk_Next;
public k_Node(object ak_Key, object ak_Value, int ai_Height)
{
mk_Next = new k_Node[ai_Height];
mk_Key = ak_Key;
mk_Value = ak_Value;
}
public object Key
{
get { return mk_Key; }
}
public object Value
{
get { return mk_Value; }
set { mk_Value = Value; }
}
public DictionaryEntry Item
{
get { return new DictionaryEntry(mk_Key, mk_Value); }
}
public k_Node[] Next
{
get { return mk_Next; }
}
public int Height
{
get { return mk_Next.Length; }
}
}
#endregion
#region k_NodeIterator Implementation
private class k_NodeIterator : k_Iterator
{
private readonly k_SkipList mk_List;
private k_Node mk_Current;
public k_NodeIterator(k_SkipList ak_List, k_Node ak_Node)
{
mk_List = ak_List;
mk_Current = ak_Node;
}
public override object Current
{
get
{
if (mk_Current == null)
throw new k_InvalidPositionException();
return mk_Current.Item;
}
set
{
DictionaryEntry lr_Entry = (DictionaryEntry)value;
if (mk_List.mk_Comparer.Compare(lr_Entry.Key, mk_Current.Key) != 0)
throw new ArgumentException("Key values must not be changed.");
mk_Current.Value = lr_Entry.Value;
}
}
public override object Collection
{
get { return mk_List; }
}
public override void Move(int ai_Count)
{
k_Node lk_NewPos = mk_Current;
if (ai_Count > 0)
{
while (ai_Count-- > 0)
{
if (lk_NewPos == null)
throw new InvalidOperationException("Tried to moved beyond end element.");
lk_NewPos = mk_List.Next(lk_NewPos);
}
}
else
{
while (ai_Count++ < 0)
{
if (lk_NewPos == null)
lk_NewPos = mk_List.RightMost();
else
lk_NewPos = mk_List.Previous(lk_NewPos);
if (lk_NewPos == null)
throw new InvalidOperationException("Tried to move before first element.");
}
}
mk_Current = lk_NewPos;
}
public override int Distance(k_Iterator ak_Iter)
{
k_NodeIterator lk_Iter = (k_NodeIterator)ak_Iter;
k_Iterator lk_End = mk_List.End;
int li_KeyDiff;
if (this == lk_End || ak_Iter == lk_End)
li_KeyDiff = (this == lk_End && this != ak_Iter) ? 1 : 0;
else
li_KeyDiff = mk_List.mk_Comparer.Compare(mk_Current.Key, lk_Iter.mk_Current.Key);
if (li_KeyDiff <= 0)
{
int li_Diff = 0;
k_Iterator lk_Bck = this.Clone();
for (; lk_Bck != lk_Iter && lk_Bck != lk_End; lk_Bck.Next())
--li_Diff;
if (lk_Bck == lk_Iter)
return li_Diff;
}
if (li_KeyDiff >= 0)
{
int li_Diff = 0;
k_Iterator lk_Fwd = lk_Iter.Clone();
for (; lk_Fwd != this && lk_Fwd != lk_End; lk_Fwd.Next())
++li_Diff;
if (lk_Fwd == this)
return li_Diff;
}
throw new Exception("Inconsistent state. Concurrency?");
}
public override bool Equals(object ak_Obj)
{
k_NodeIterator lk_Iter = ak_Obj as k_NodeIterator;
if (lk_Iter == null)
return false;
return object.ReferenceEquals(mk_Current, lk_Iter.mk_Current);
}
public override int GetHashCode()
{
if (mk_Current == null)
return mk_List.GetHashCode();
return mk_Current.GetHashCode();
}
public override k_Iterator Clone()
{
return new k_NodeIterator(mk_List, mk_Current);
}
internal k_Node Node
{
get { return mk_Current; }
}
}
private class k_PinnedNodeIterator : k_NodeIterator
{
public k_PinnedNodeIterator(k_SkipList ak_List, k_Node ak_Node)
: base(ak_List, ak_Node)
{
}
public override void Move(int ai_Count)
{
throw new k_IteratorPinnedException();
}
}
#endregion
private static Random mk_Rand = new Random();
private IComparer mk_Comparer;
private double md_Prob;
private int mi_MaxLevel;
private int mi_HighestNode;
private k_Node mk_Head;
private int mi_Count;
private k_Iterator mk_End;
public k_SkipList()
: this(System.Collections.Comparer.Default)
{
}
public k_SkipList(IComparer ak_Comparer)
: this(ak_Comparer, 1.0/Math.E, 16)
{
}
public k_SkipList(IComparer ak_Comparer, double ad_Prob, int ai_MaxLevel)
{
if (ad_Prob >= 1.0 || ad_Prob <= 0)
throw new ArgumentException("Invalid probability. Must be (0-1).", "ad_Prob");
md_Prob = ad_Prob;
mi_MaxLevel = ai_MaxLevel;
mk_Comparer = ak_Comparer;
mk_Head = new k_Node(null, null, ai_MaxLevel);
mk_End = new k_PinnedNodeIterator(this, null);
}
// IContainer Members
public k_Iterator Begin
{
get
{
if (mi_Count == 0)
return this.End;
return new k_NodeIterator(this, this.LeftMost());
}
}
public k_Iterator End
{
get { return mk_End; }
}
public bool IsEmpty
{
get { return (mi_Count == 0); }
}
public k_Iterator Find(object ak_Value)
{
DictionaryEntry lr_Item = (DictionaryEntry)ak_Value;
k_NodeIterator lk_Found = (k_NodeIterator)LowerBound(lr_Item.Key);
if (lk_Found != this.End
&& mk_Comparer.Compare(lr_Item.Key, lk_Found.Node.Key) == 0 && mk_Comparer.Compare(lr_Item.Value, lk_Found.Node.Value) == 0)
return lk_Found;
return this.End;
}
public k_Iterator Erase(k_Iterator ak_Where)
{
return Erase(ak_Where, ak_Where+1);
}
public k_Iterator Erase(k_Iterator ak_First, k_Iterator ak_Last)
{
if (ak_First == ak_Last)
return ak_Last.Clone();
int li_Count = ak_Last - ak_First;
k_Node lk_First = ((k_NodeIterator)ak_First).Node;
k_Node lk_Last = (ak_Last != this.End) ? ((k_NodeIterator)ak_Last).Node : null;
k_Node lk_Node = new k_Node(null, null, mi_HighestNode);
k_Node lk_Current = mk_Head;
for (int li_Level = mi_HighestNode-1; li_Level >= 0; --li_Level)
{
while (lk_Current.Next[li_Level] != null)
{
if (ComparePos(lk_Current.Next[li_Level], lk_First) >= 0)
break;
lk_Current = lk_Current.Next[li_Level];
}
lk_Node.Next[li_Level] = lk_Current;
}
if (lk_Last == null)
{
for (int i=0; i<lk_Node.Height; ++i)
{
k_Node lk_Left = lk_Node.Next[i];
lk_Left.Next[i] = null;
}
}
else
{
for (int i=0; i<lk_Node.Height; ++i)
{
k_Node lk_Left = lk_Node.Next[i];
// for each level skip over erased range
lk_Current = lk_Left.Next[i];
while (lk_Current != null)
{
if (ComparePos(lk_Current, lk_Last) >= 0)
break;
lk_Current = lk_Current.Next[i];
}
lk_Left.Next[i] = lk_Current;
}
}
mi_Count -= li_Count;
while (mi_HighestNode > 0 && mk_Head.Next[mi_HighestNode-1] == null)
--mi_HighestNode;
return ak_Last;
}
// IMap Members
public k_Iterator FindKey(object ak_Key)
{
k_NodeIterator lk_Found = (k_NodeIterator)LowerBound(ak_Key);
if (lk_Found != this.End && mk_Comparer.Compare(ak_Key, lk_Found.Node.Key) == 0)
return lk_Found;
return this.End;
}
public void Add(DictionaryEntry ar_Entry)
{
Add(ar_Entry.Key, ar_Entry.Value);
}
public void Insert(k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
for (k_Iterator lk_Iter = ak_SrcBegin.Clone(); lk_Iter != ak_SrcEnd; lk_Iter.Next())
Add((DictionaryEntry)lk_Iter.Current);
}
// ISortedMap Members
public IComparer Comparer
{
get { return mk_Comparer; }
}
/// <summary>
/// Returns an iterator to the first element in a list with a key value
/// that is equal to or greater than that of a specified key.
/// </summary>
/// <param name="ak_Key">
/// The argument key value to be compared with the sort key of an element
/// from the list being searched.
/// </param>
/// <returns>
/// Location of an element in a list that with a key that is equal to
/// or greater than the argument key, or this.End if no match is found for the key.
/// </returns>
public k_Iterator LowerBound(object ak_Key)
{
k_Node lk_Found = null;
k_Node lk_Current = mk_Head;
for (int li_Level = mi_HighestNode-1; li_Level >= 0; --li_Level)
{
k_Node lk_Next = lk_Current.Next[li_Level];
while (lk_Next != null)
{
int li_Diff = mk_Comparer.Compare(lk_Next.Key, ak_Key);
if (li_Diff >= 0)
{
lk_Found = lk_Next;
break;
}
lk_Current = lk_Next;
lk_Next = lk_Next.Next[li_Level];
}
}
return new k_NodeIterator(this, lk_Found);
}
/// <summary>
/// Returns an iterator to the first element in a list with a key value
/// that is greater than that of a specified key.
/// </summary>
/// <param name="ak_Key">
/// The argument key value to be compared with the sort key of an element
/// from the list being searched.
/// </param>
/// <returns>
/// Location of an element in a list that with a key that is greater
/// than the argument key, or this.End if no match is found for the key.
/// </returns>
public k_Iterator UpperBound(object ak_Key)
{
k_Node lk_Found = null;
k_Node lk_Current = mk_Head;
for (int li_Level = mi_HighestNode-1; li_Level >= 0; --li_Level)
{
k_Node lk_Next = lk_Current.Next[li_Level];
while (lk_Next != null)
{
int li_Diff = mk_Comparer.Compare(lk_Next.Key, ak_Key);
if (li_Diff > 0)
{
lk_Found = lk_Next;
break;
}
lk_Current = lk_Next;
lk_Next = lk_Next.Next[li_Level];
}
}
return new k_NodeIterator(this, lk_Found);
}
#region IDictionary Members
public void Add(object ak_Key, object ak_Value)
{
k_Node lk_Node = new k_Node(ak_Key, ak_Value, CalcNewNodeHeight());
if (lk_Node.Height > mi_HighestNode)
mi_HighestNode = lk_Node.Height;
FindInsertPos(lk_Node);
for (int i=0; i<lk_Node.Height; ++i)
{
k_Node lk_Left = lk_Node.Next[i];
k_Node lk_Tmp = lk_Left.Next[i];
lk_Left.Next[i] = lk_Node;
lk_Node.Next[i] = lk_Tmp;
}
++mi_Count;
}
public void Clear()
{
Array.Clear(mk_Head.Next, 0, mk_Head.Next.Length);
mi_HighestNode = 0;
mi_Count = 0;
}
public bool Contains(object ak_Key)
{
return (FindKey(ak_Key) != this.End);
}
public IDictionaryEnumerator GetEnumerator()
{
return new k_IteratorDictEnumerator(this.Begin, this.End);
}
public void Remove(object ak_Key)
{
Erase(LowerBound(ak_Key), UpperBound(ak_Key));
}
public bool IsFixedSize
{
get { return false; }
}
public bool IsReadOnly
{
get { return false; }
}
public object this[object ak_Key]
{
get
{
k_NodeIterator lk_Iter = (k_NodeIterator)FindKey(ak_Key);
if (lk_Iter == this.End)
return null;
return lk_Iter.Node.Value;
}
set
{
k_NodeIterator lk_Iter = (k_NodeIterator)FindKey(ak_Key);
if (lk_Iter == this.End)
throw new ArgumentException("No element for key was found.", "ak_Key");
lk_Iter.Node.Value = value;
}
}
public ICollection Keys
{
get
{
object[] lk_Keys = new object[mi_Count];
int i = 0;
for (k_Iterator lk_Iter = this.Begin.Clone(); lk_Iter != this.End; lk_Iter.Next())
lk_Keys[i++] = ((k_NodeIterator)lk_Iter).Node.Key;
return lk_Keys;
}
}
public ICollection Values
{
get
{
object[] lk_Values = new object[mi_Count];
int i=0;
for (k_Iterator lk_Iter = this.Begin.Clone(); lk_Iter != this.End; lk_Iter.Next())
lk_Values[i++] = ((k_NodeIterator)lk_Iter).Node.Value;
return lk_Values;
}
}
#endregion
#region ICollection Members
public void CopyTo(Array ak_Array, int ai_Index)
{
foreach (object lk_Obj in this)
ak_Array.SetValue(lk_Obj, ai_Index++);
}
public int Count
{
get { return mi_Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get { return this; }
}
#endregion
#region IEnumerable Members
IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return new k_IteratorEnumerator(this.Begin, this.End);
}
#endregion
#region ICloneable Members
public object Clone()
{
k_SkipList lk_Clone = new k_SkipList(mk_Comparer, md_Prob, mi_MaxLevel);
lk_Clone.mi_Count = mi_Count;
lk_Clone.mi_HighestNode = mi_HighestNode;
lk_Clone.mk_Head = CloneR(mk_Head, null);
return lk_Clone;
}
#endregion
private k_Node Previous(k_Node ak_Node)
{
k_Node lk_Current = mk_Head;
for (int li_Level = mi_HighestNode-1; li_Level >= 0; --li_Level)
{
while (lk_Current.Next[li_Level] != null)
{
int li_Diff = mk_Comparer.Compare(lk_Current.Next[li_Level].Key, ak_Node.Key);
if (li_Diff > 0)
break;
if (li_Diff == 0)
{
k_Node lk_Next = lk_Current;
while (lk_Next != null && !object.ReferenceEquals(lk_Next.Next[0], ak_Node))
{
if (mk_Comparer.Compare(lk_Next.Key, ak_Node.Key) > 0)
lk_Next = null;
else
lk_Next = lk_Next.Next[0];
}
if (lk_Next == null)
break;
return lk_Next; // found previous node during right-scan of nodes with equal key value
}
lk_Current = lk_Current.Next[li_Level];
}
}
if (object.ReferenceEquals(mk_Head, lk_Current))
return null;
return lk_Current;
}
private k_Node Next(k_Node ak_Node)
{
return ak_Node.Next[0];
}
/// <summary>
/// Return leftmost node in list.
/// </summary>
/// <returns>Found node</returns>
private k_Node LeftMost()
{
return mk_Head.Next[0];
}
/// <summary>
/// Return rightmost node in list.
/// </summary>
/// <returns>Found node</returns>
private k_Node RightMost()
{
k_Node lk_Current = mk_Head.Next[mi_HighestNode-1];
for (int li_Level = mi_HighestNode-1; li_Level >= 0; --li_Level)
{
while (lk_Current.Next[li_Level] != null)
lk_Current = lk_Current.Next[li_Level];
}
return lk_Current;
}
private void FindInsertPos(k_Node ak_Node)
{
k_Node lk_Current = mk_Head;
for (int li_Level = mi_HighestNode-1; li_Level >= 0; --li_Level)
{
while (lk_Current.Next[li_Level] != null && mk_Comparer.Compare(lk_Current.Next[li_Level].Key, ak_Node.Key) < 0)
lk_Current = lk_Current.Next[li_Level];
if (li_Level < ak_Node.Height)
ak_Node.Next[li_Level] = lk_Current;
}
}
private int CalcNewNodeHeight()
{
double ld_Rnd = mk_Rand.NextDouble();
int li_Level = 1;
for (double ld_Pow = md_Prob; li_Level < mi_MaxLevel; ++li_Level, ld_Pow*=md_Prob)
{
if (ld_Pow < ld_Rnd)
break;
}
return li_Level;
}
private int ComparePos(k_Node ak_Left, k_Node ak_Right)
{
if (object.ReferenceEquals(ak_Left, ak_Right))
return 0;
int li_Diff = mk_Comparer.Compare(ak_Left.Key, ak_Right.Key);
if (li_Diff != 0)
return li_Diff;
k_Node lk_Current = ak_Left;
for (;;)
{
if (lk_Current == null || mk_Comparer.Compare(lk_Current.Key, ak_Right.Key) > 0)
return 1;
else if (object.ReferenceEquals(lk_Current, ak_Right))
return -1;
lk_Current = lk_Current.Next[0];
}
}
private k_Node CloneR(k_Node ak_Node, k_Node ak_NextHigher)
{
k_Node lk_New = new k_Node(ak_Node.Key, ak_Node.Value, ak_Node.Height);
for (int i=ak_Node.Height-1; i>=0; --i)
{
// simply copy two links with equal target next to each other
if (i < ak_Node.Height-1 && object.ReferenceEquals(ak_Node.Next[i], ak_Node.Next[i+1]))
{
lk_New.Next[i] = lk_New.Next[i+1];
continue;
}
k_Node lk_Next = ak_Node.Next[i];
if (lk_Next != null && lk_Next.Height-1 <= i)
{
k_Node lk_Higher = (i < ak_Node.Height-1) ? ak_Node.Next[i+1] : ak_NextHigher;
lk_New.Next[i] = CloneR(lk_Next, lk_Higher);
}
else
lk_New.Next[i] = ak_NextHigher;
}
return lk_New;
}
}
}

View File

@@ -0,0 +1,101 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// A push-down stack using an underlying k_Vector.
/// Last in first out (LIFO).
/// </summary>
public class k_Stack : ICollection
{
private ISequence mk_Items; // stack container
private int mi_MaxSize;
public k_Stack()
{
mk_Items = new k_Vector();
mi_MaxSize = int.MaxValue;
}
public k_Stack(int ai_Capacity, bool ab_FixedSize)
{
mk_Items = new k_Vector(ai_Capacity);
mi_MaxSize = (ab_FixedSize) ? ai_Capacity : int.MaxValue;
}
public object Top
{
get { return mk_Items.Back; }
set { mk_Items.Back = value; }
}
public object this[int ai_Index]
{
get { return (mk_Items.Begin+ai_Index).Current; }
set { (mk_Items.Begin+ai_Index).Current = value; }
}
public void Push(object ak_Value)
{
if (mk_Items.Count >= mi_MaxSize)
throw new StackOverflowException("Stack overflow");
mk_Items.PushBack(ak_Value);
}
public object Pop()
{
if (mk_Items.Count == 0)
throw new StackOverflowException("Stack underflow");
object lk_Obj = mk_Items.Back;
mk_Items.PopBack();
return lk_Obj;
}
public bool IsEmpty
{
get { return mk_Items.IsEmpty; }
}
public void Clear()
{
mk_Items.Clear();
}
#region ICollection Members
public void CopyTo(Array ak_Array, int ai_Index)
{
for (k_Iterator lk_Iter = mk_Items.Begin.Clone(); lk_Iter != mk_Items.End; lk_Iter.Next())
ak_Array.SetValue(lk_Iter.Current, ai_Index++);
}
public int Count
{
get { return mk_Items.Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get { return this; }
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return new k_IteratorEnumerator(mk_Items.Begin, mk_Items.End);
}
#endregion
}
}

View File

@@ -0,0 +1,830 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// k_Tree is a red-black balanced search tree (BST) implementation.
/// Complexity of find, insert and erase operations is near O(lg n).
/// </summary>
public class k_Tree : ISortedMap
{
#region k_Node Implementation
private class k_Node
{
private object mk_Key;
private object mk_Value;
private bool mb_Red;
public k_Node mk_Left, mk_Right, mk_Parent; // public to simplify fixup & clone (passing by ref)
public k_Node(object ak_Key, object ak_Value, k_Node ak_Parent)
{
mk_Key = ak_Key;
mk_Value = ak_Value;
mk_Parent = ak_Parent;
mb_Red = true;
}
public object Key
{
get { return mk_Key; }
}
public object Value
{
get { return mk_Value; }
set { mk_Value = value; }
}
public DictionaryEntry Item
{
get { return new DictionaryEntry(mk_Key, mk_Value); }
}
public bool Red
{
get { return mb_Red; }
set { mb_Red = value; }
}
public static void SwapItems(k_Node ak_A, k_Node ak_B)
{
object lk_Tmp = ak_A.mk_Key;
ak_A.mk_Key = ak_B.mk_Key;
ak_B.mk_Key = lk_Tmp;
lk_Tmp = ak_A.mk_Value;
ak_A.mk_Value = ak_B.mk_Value;
ak_B.mk_Value = lk_Tmp;
}
}
#endregion
#region k_NodeIterator Implementation
private class k_NodeIterator : k_Iterator
{
private readonly k_Tree mk_Tree;
private k_Node mk_Current;
public k_NodeIterator(k_Tree ak_Tree, k_Node ak_Node)
{
mk_Tree = ak_Tree;
mk_Current = ak_Node;
}
public override object Current
{
get
{
if (mk_Current == null)
throw new k_InvalidPositionException();
return mk_Current.Item;
}
set
{
DictionaryEntry lr_Entry = (DictionaryEntry)value;
if (mk_Tree.mk_Comparer.Compare(lr_Entry.Key, mk_Current.Key) != 0)
throw new ArgumentException("Key values must not be changed.");
mk_Current.Value = lr_Entry.Value;
}
}
public override object Collection
{
get { return mk_Tree; }
}
public override void Move(int ai_Count)
{
k_Node lk_NewPos = mk_Current;
if (ai_Count > 0)
{
while (ai_Count-- > 0)
{
if (lk_NewPos == null)
throw new InvalidOperationException("Tried to moved beyond end element.");
lk_NewPos = k_Tree.Next(lk_NewPos);
}
}
else
{
while (ai_Count++ < 0)
{
if (lk_NewPos == null)
lk_NewPos = mk_Tree.mk_Right;
else
lk_NewPos = k_Tree.Previous(lk_NewPos);
if (lk_NewPos == null)
throw new InvalidOperationException("Tried to move before first element.");
}
}
mk_Current = lk_NewPos;
}
public override int Distance(k_Iterator ak_Iter)
{
k_NodeIterator lk_Iter = ak_Iter as k_NodeIterator;
if (lk_Iter == null || !object.ReferenceEquals(lk_Iter.Collection, this.Collection))
throw new ArgumentException("Cannot determine distance of iterators belonging to different collections.");
k_Iterator lk_End = mk_Tree.End;
int li_KeyDiff;
if (this == lk_End || ak_Iter == lk_End)
li_KeyDiff = (this == lk_End && this != ak_Iter) ? 1 : 0;
else
li_KeyDiff = mk_Tree.mk_Comparer.Compare(mk_Current.Key, lk_Iter.mk_Current.Key);
if (li_KeyDiff <= 0)
{
int li_Diff = 0;
k_Iterator lk_Bck = this.Clone();
for (; lk_Bck != ak_Iter && lk_Bck != lk_End; lk_Bck.Next())
--li_Diff;
if (lk_Bck == ak_Iter)
return li_Diff;
}
if (li_KeyDiff >= 0)
{
int li_Diff = 0;
k_Iterator lk_Fwd = ak_Iter.Clone();
for (; lk_Fwd != this && lk_Fwd != lk_End; lk_Fwd.Next())
++li_Diff;
if (lk_Fwd == this)
return li_Diff;
}
throw new Exception("Inconsistent state. Concurrency?");
}
public override bool Equals(object ak_Obj)
{
k_NodeIterator lk_Iter = ak_Obj as k_NodeIterator;
if (lk_Iter == null)
return false;
return object.ReferenceEquals(mk_Current, lk_Iter.mk_Current);
}
public override int GetHashCode()
{
if (mk_Current == null)
return mk_Tree.GetHashCode();
return mk_Current.GetHashCode();
}
public override k_Iterator Clone()
{
return new k_NodeIterator(mk_Tree, mk_Current);
}
internal k_Node Node
{
get { return mk_Current; }
}
}
private class k_PinnedNodeIterator : k_NodeIterator
{
public k_PinnedNodeIterator(k_Tree ak_Tree, k_Node ak_Node)
: base(ak_Tree, ak_Node)
{
}
public override void Move(int ai_Count)
{
throw new k_IteratorPinnedException();
}
}
#endregion
private k_Node mk_Head, mk_Left, mk_Right;
private k_Iterator mk_End;
private int mi_Count;
private IComparer mk_Comparer;
private bool mb_AllowDuplicateKeys;
public k_Tree()
: this(false)
{
}
public k_Tree(bool ab_AllowDuplicateKeys)
: this(ab_AllowDuplicateKeys, System.Collections.Comparer.Default)
{
}
public k_Tree(bool ab_AllowDuplicateKeys, IComparer ak_Comparer)
{
mb_AllowDuplicateKeys = ab_AllowDuplicateKeys;
mk_Comparer = ak_Comparer;
mk_End = new k_PinnedNodeIterator(this, null);
}
// IContainer Members
public k_Iterator Begin
{
get
{
if (mi_Count == 0)
return this.End;
return new k_NodeIterator(this, mk_Left);
}
}
public k_Iterator End
{
get { return mk_End; }
}
public bool IsEmpty
{
get { return (mi_Count == 0); }
}
public k_Iterator Find(object ak_Value)
{
DictionaryEntry lr_Item = (DictionaryEntry)ak_Value;
k_Node lk_Found = FindInternal(mk_Head, lr_Item.Key);
if (lk_Found != null && mk_Comparer.Compare(lk_Found.Value, lr_Item.Value) == 0)
return new k_NodeIterator(this, lk_Found);
return this.End;
}
public k_Iterator Erase(k_Iterator ak_Where)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this tree.");
k_Iterator lk_Successor = ak_Where + 1;
RemoveNode(((k_NodeIterator)ak_Where).Node);
return lk_Successor;
}
public k_Iterator Erase(k_Iterator ak_First, k_Iterator ak_Last)
{
if (ak_First == this.Begin && ak_Last == this.End)
{
Clear();
return ak_Last.Clone();
}
k_Iterator lk_Current = ak_First;
while (lk_Current != ak_Last)
lk_Current = Erase(lk_Current);
return lk_Current;
}
// IMap Members
public void Add(DictionaryEntry ar_Item)
{
Add(ar_Item.Key, ar_Item.Value);
}
public k_Iterator FindKey(object ak_Key)
{
return new k_NodeIterator(this, FindInternal(mk_Head, ak_Key));
}
public void Insert(k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
for (k_Iterator lk_Iter = ak_SrcBegin.Clone(); lk_Iter != ak_SrcEnd; lk_Iter.Next())
Add((DictionaryEntry)lk_Iter.Current);
}
// ISortedMap Members
public IComparer Comparer
{
get { return mk_Comparer; }
}
public k_Iterator LowerBound(object ak_Key)
{
k_Node lk_Node = mk_Head;
k_Node lk_Found = null;
while (lk_Node != null)
{
if (mk_Comparer.Compare(lk_Node.Key, ak_Key) < 0)
lk_Node = lk_Node.mk_Right;
else
{
lk_Found = lk_Node;
lk_Node = lk_Node.mk_Left;
}
}
return new k_NodeIterator(this, lk_Found);
}
public k_Iterator UpperBound(object ak_Key)
{
k_Node lk_Node = mk_Head;
k_Node lk_Found = null;
while (lk_Node != null)
{
if (mk_Comparer.Compare(lk_Node.Key, ak_Key) > 0)
{
lk_Found = lk_Node;
lk_Node = lk_Node.mk_Left;
}
else
lk_Node = lk_Node.mk_Right;
}
return new k_NodeIterator(this, lk_Found);
}
#region IDictionary Members
public void Add(object ak_Key, object ak_Value)
{
Insert(ref mk_Head, null, ak_Key, ak_Value, false);
mk_Head.Red = false;
++mi_Count;
}
public void Clear()
{
mi_Count = 0;
mk_Head = null;
mk_Left = null;
mk_Right = null;
}
public bool Contains(object ak_Key)
{
return (FindInternal(mk_Head, ak_Key) != null);
}
public IDictionaryEnumerator GetEnumerator()
{
return new k_IteratorDictEnumerator(this.Begin, this.End);
}
public void Remove(object ak_Key)
{
RemoveNode(FindInternal(mk_Head, ak_Key));
}
public bool IsFixedSize
{
get { return false; }
}
public bool IsReadOnly
{
get { return false; }
}
public object this[object ak_Key]
{
get
{
k_Node lk_Node = FindInternal(mk_Head, ak_Key);
if (lk_Node == null)
return null;
return lk_Node.Value;
}
set
{
k_Node lk_Node = FindInternal(mk_Head, ak_Key);
if (lk_Node == null)
Add(new DictionaryEntry(ak_Key, value));
else
lk_Node.Value = value;
}
}
public ICollection Keys
{
get
{
int i=0;
object[] lk_Keys = new object[mi_Count];
foreach (DictionaryEntry lr_Entry in this)
lk_Keys[i++] = lr_Entry.Key;
return lk_Keys;
}
}
public ICollection Values
{
get
{
int i=0;
object[] lk_Values = new object[mi_Count];
foreach (DictionaryEntry lr_Entry in this)
lk_Values[i++] = lr_Entry.Value;
return lk_Values;
}
}
#endregion
#region ICollection Members
public void CopyTo(Array ak_Array, int ai_Index)
{
foreach (DictionaryEntry lr_Entry in this)
ak_Array.SetValue(lr_Entry, ai_Index++);
}
public int Count
{
get { return mi_Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get { return this; }
}
#endregion
#region IEnumerable Members
IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return new k_IteratorEnumerator(this.Begin, this.End);
}
#endregion
#region ICloneable Members
public object Clone()
{
k_Tree lk_Clone = new k_Tree(mb_AllowDuplicateKeys, mk_Comparer);
lk_Clone.mi_Count = mi_Count;
CloneRecursive(mk_Head, null, ref lk_Clone.mk_Head);
lk_Clone.mk_Left = k_Tree.LeftMost(lk_Clone.mk_Head);
lk_Clone.mk_Right = k_Tree.RightMost(lk_Clone.mk_Head);
return lk_Clone;
}
#endregion
private void CloneRecursive(k_Node ak_Node, k_Node ak_Parent, ref k_Node ak_Link)
{
if (ak_Node == null)
return;
ak_Link = new k_Node(ak_Node.Key, ak_Node.Value, ak_Parent);
ak_Link.Red = ak_Node.Red;
CloneRecursive(ak_Node.mk_Left, ak_Link, ref ak_Link.mk_Left);
CloneRecursive(ak_Node.mk_Right, ak_Link, ref ak_Link.mk_Right);
}
private bool IsRed(k_Node ak_Node)
{
return (ak_Node != null && ak_Node.Red);
}
private k_Node FindInternal(k_Node ak_Node, object ak_Key)
{
while (ak_Node != null)
{
int li_Diff = mk_Comparer.Compare(ak_Key, ak_Node.Key);
if (li_Diff == 0)
return ak_Node;
ak_Node = (li_Diff < 0) ? ak_Node.mk_Left : ak_Node.mk_Right;
}
return null;
}
/// <summary>
/// Return leftmost node in subtree.
/// </summary>
/// <param name="ak_Node">Node where to start search</param>
/// <returns>Found node</returns>
private static k_Node LeftMost(k_Node ak_Node)
{
if (ak_Node == null)
return null;
while (ak_Node.mk_Left != null)
ak_Node = ak_Node.mk_Left;
return ak_Node;
}
/// <summary>
/// Return rightmost node in subtree.
/// </summary>
/// <param name="ak_Node">Node where to start search</param>
/// <returns>Found node</returns>
private static k_Node RightMost(k_Node ak_Node)
{
if (ak_Node == null)
return null;
while (ak_Node.mk_Right != null)
ak_Node = ak_Node.mk_Right;
return ak_Node;
}
private static k_Node Previous(k_Node ak_Node) // the next smaller
{
if (ak_Node.mk_Left != null)
return RightMost(ak_Node.mk_Left);
k_Node lk_Parent = ak_Node.mk_Parent;
while (lk_Parent != null && lk_Parent.mk_Left == ak_Node)
{
ak_Node = lk_Parent;
lk_Parent = lk_Parent.mk_Parent;
}
return lk_Parent;
}
private static k_Node Next(k_Node ak_Node)
{
if (ak_Node.mk_Right != null)
return LeftMost(ak_Node.mk_Right);
k_Node lk_Parent = ak_Node.mk_Parent;
while (lk_Parent != null && lk_Parent.mk_Right == ak_Node)
{
ak_Node = lk_Parent;
lk_Parent = lk_Parent.mk_Parent;
}
return lk_Parent;
}
private void RemoveNode(k_Node ak_Node)
{
if (ak_Node == null)
return;
if (ak_Node == mk_Head)
UnlinkNode(ref mk_Head);
else if (ak_Node == ak_Node.mk_Parent.mk_Right)
UnlinkNode(ref ak_Node.mk_Parent.mk_Right);
else
UnlinkNode(ref ak_Node.mk_Parent.mk_Left);
}
private void UnlinkNode(ref k_Node ak_Node)
{
bool lb_Red = ak_Node.Red;
k_Node lk_Erased = ak_Node;
k_Node lk_PatchNode = null;
if (ak_Node.mk_Right == null)
lk_PatchNode = ak_Node.mk_Left;
else if (ak_Node.mk_Left == null)
lk_PatchNode = ak_Node.mk_Right;
else
lk_PatchNode = ak_Node;
k_Node lk_PatchParent = null, lk_FixNode = null;
if (lk_PatchNode == null)
{
lk_PatchParent = ak_Node.mk_Parent;
ak_Node = null;
}
else if (lk_PatchNode != ak_Node)
{
lk_PatchNode.mk_Parent = ak_Node.mk_Parent;
ak_Node = lk_PatchNode;
lk_PatchParent = lk_PatchNode.mk_Parent;
}
else
{
// two subtrees
lk_PatchNode = RightMost(ak_Node.mk_Left);
if (lk_PatchNode.mk_Parent.mk_Right == lk_PatchNode)
lk_PatchNode.mk_Parent.mk_Right = lk_PatchNode.mk_Left;
else
lk_PatchNode.mk_Parent.mk_Left = lk_PatchNode.mk_Left;
lb_Red = lk_PatchNode.Red;
if (lk_PatchNode.mk_Left != null)
lk_PatchNode.mk_Left.mk_Parent = lk_PatchNode.mk_Parent;
lk_PatchParent = lk_PatchNode.mk_Parent;
lk_FixNode = lk_PatchNode.mk_Left;
k_Node.SwapItems(ak_Node, lk_PatchNode);
// ensure that mk_Left and/or mk_Right are corrected after unlink
lk_Erased = lk_PatchNode;
}
if (!lb_Red && lk_PatchParent != null)
{
// erased node was black link - rebalance the tree
while (!IsRed(lk_FixNode) && lk_FixNode != mk_Head)
{
if (lk_PatchParent.mk_Left != null || lk_PatchParent.mk_Right != null)
{
if (lk_PatchParent.mk_Left == lk_FixNode)
{
// fixup right subtree
k_Node lk_Node = lk_PatchParent.mk_Right;
if (IsRed(lk_Node))
{
lk_Node.Red = false;
lk_PatchParent.Red = true;
RotateLeft(lk_PatchParent);
lk_Node = lk_PatchParent.mk_Right;
}
if (lk_Node != null)
{
if (!IsRed(lk_Node.mk_Left) && !IsRed(lk_Node.mk_Right))
lk_Node.Red = true;
else
{
if (!IsRed(lk_Node.mk_Right))
{
lk_Node.Red = true;
lk_Node.mk_Left.Red = false;
RotateRight(lk_Node);
lk_Node = lk_PatchParent.mk_Right;
}
lk_Node.Red = lk_PatchParent.Red;
lk_PatchParent.Red = false;
lk_Node.mk_Right.Red = false;
RotateLeft(lk_PatchParent);
break;
}
}
}
else
{
// fixup leftsubtree
k_Node lk_Node = lk_PatchParent.mk_Left;
if (IsRed(lk_Node))
{
lk_Node.Red = false;
lk_PatchParent.Red = true;
RotateRight(lk_PatchParent);
lk_Node = lk_PatchParent.mk_Left;
}
if (lk_Node != null)
{
if (!IsRed(lk_Node.mk_Left) && !IsRed(lk_Node.mk_Right))
lk_Node.Red = true;
else
{
if (!IsRed(lk_Node.mk_Left))
{
lk_Node.Red = true;
lk_Node.mk_Right.Red = false;
RotateLeft(lk_Node);
lk_Node = lk_PatchParent.mk_Left;
}
lk_Node.Red = lk_PatchParent.Red;
lk_PatchParent.Red = false;
lk_Node.mk_Left.Red = false;
RotateRight(lk_PatchParent);
break;
}
}
}
}
lk_FixNode = lk_PatchParent;
lk_PatchParent = lk_PatchParent.mk_Parent;
}
if (lk_FixNode != null)
lk_FixNode.Red = false;
}
--mi_Count;
if (object.ReferenceEquals(lk_Erased, mk_Right))
mk_Right = k_Tree.RightMost(mk_Head);
if (object.ReferenceEquals(lk_Erased, mk_Left))
mk_Left = k_Tree.LeftMost(mk_Head);
}
private void Insert(ref k_Node ak_Node, k_Node ak_Parent, object ak_Key, object ak_Value, bool ab_RightMove)
{
if (ak_Node == null)
{
ak_Node = new k_Node(ak_Key, ak_Value, ak_Parent);
if (object.ReferenceEquals(ak_Parent, mk_Right) && (ak_Parent == null || ab_RightMove))
mk_Right = ak_Node;
if (object.ReferenceEquals(ak_Parent, mk_Left) && (ak_Parent == null || !ab_RightMove))
mk_Left = ak_Node;
return;
}
if (IsRed(ak_Node.mk_Left) && IsRed(ak_Node.mk_Right))
{
ak_Node.Red = true;
ak_Node.mk_Left.Red = false;
ak_Node.mk_Right.Red = false;
}
int li_Diff = mk_Comparer.Compare(ak_Key, ak_Node.Key);
if (!mb_AllowDuplicateKeys && li_Diff == 0)
throw new ArgumentException("An element with the same key already exists in the tree.");
if (li_Diff < 0)
{
Insert(ref ak_Node.mk_Left, ak_Node, ak_Key, ak_Value, false);
if (IsRed(ak_Node) && IsRed(ak_Node.mk_Left) && ab_RightMove)
ak_Node = RotateRight(ak_Node);
if (IsRed(ak_Node.mk_Left) && IsRed(ak_Node.mk_Left.mk_Left))
{
ak_Node = RotateRight(ak_Node);
ak_Node.Red = false;
ak_Node.mk_Right.Red = true;
}
}
else
{
Insert(ref ak_Node.mk_Right, ak_Node, ak_Key, ak_Value, true);
if (IsRed(ak_Node) && IsRed(ak_Node.mk_Right) && !ab_RightMove)
ak_Node = RotateLeft(ak_Node);
if (IsRed(ak_Node.mk_Right) && IsRed(ak_Node.mk_Right.mk_Right))
{
ak_Node = RotateLeft(ak_Node);
ak_Node.Red = false;
ak_Node.mk_Left.Red = true;
}
}
}
/*
A right rotation: ak_Node.Left takes old position of ak_Node.
Makes the old root the right subtree of the new root.
5 2
2 7 -> 1 5
1 3 6 8 3 7
6 8
*/
private k_Node RotateRight(k_Node ak_Node)
{
k_Node lk_Tmp = ak_Node.mk_Left;
lk_Tmp.mk_Parent = ak_Node.mk_Parent;
ak_Node.mk_Parent = lk_Tmp;
ak_Node.mk_Left = lk_Tmp.mk_Right;
if (ak_Node.mk_Left != null)
ak_Node.mk_Left.mk_Parent = ak_Node;
lk_Tmp.mk_Right = ak_Node;
// correct parent
if (lk_Tmp.mk_Parent == null)
mk_Head = lk_Tmp;
else if (lk_Tmp.mk_Parent.mk_Right == ak_Node)
lk_Tmp.mk_Parent.mk_Right = lk_Tmp;
else
lk_Tmp.mk_Parent.mk_Left = lk_Tmp;
return lk_Tmp;
}
/*
A left rotation: ak_Node.Right takes old position of ak_Node.
Makes the old root the left subtree of the new root.
5 7
2 7 -> 5 8
1 3 6 8 2 6
1 3
*/
private k_Node RotateLeft(k_Node ak_Node)
{
k_Node lk_Tmp = ak_Node.mk_Right;
lk_Tmp.mk_Parent = ak_Node.mk_Parent;
ak_Node.mk_Parent = lk_Tmp;
ak_Node.mk_Right = lk_Tmp.mk_Left;
if (ak_Node.mk_Right != null)
ak_Node.mk_Right.mk_Parent = ak_Node;
lk_Tmp.mk_Left = ak_Node;
// correct parent
if (lk_Tmp.mk_Parent == null)
mk_Head = lk_Tmp;
else if (lk_Tmp.mk_Parent.mk_Right == ak_Node)
lk_Tmp.mk_Parent.mk_Right = lk_Tmp;
else
lk_Tmp.mk_Parent.mk_Left = lk_Tmp;
return lk_Tmp;
}
}
}

View File

@@ -0,0 +1,143 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// One dimensional array of variable size
/// </summary>
public class k_Vector : ArrayList, ISequence
{
public k_Vector()
: base()
{
}
public k_Vector(int ai_Capacity)
: base(ai_Capacity)
{
}
public k_Vector(ICollection ak_Collection)
: base(ak_Collection)
{
}
// IContainer Members
public k_Iterator Begin
{
get { return k_IListIterator.CreateBegin(this); }
}
public k_Iterator End
{
get { return k_IListIterator.CreateEnd(this); }
}
public bool IsEmpty
{
get { return (this.Count == 0); }
}
public k_Iterator Find(object ak_Value)
{
int li_Index = this.IndexOf(ak_Value);
if (li_Index < 0)
return this.End;
return new k_IListIterator(this, li_Index);
}
public k_Iterator Erase(k_Iterator ak_Where)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this collection.");
int li_Index = ((k_IListIterator)ak_Where).Index;
if (li_Index < this.Count)
base.RemoveAt(li_Index);
return new k_IListIterator(this, li_Index);
}
public k_Iterator Erase(k_Iterator ak_First, k_Iterator ak_Last)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_First.Collection) && object.ReferenceEquals(this, ak_Last.Collection), "Iterators do not belong to this collection.");
int li_FirstIndex = ((k_IListIterator)ak_First).Index;
int li_Count = ak_Last - ak_First;
base.RemoveRange(li_FirstIndex, li_Count);
return new k_IListIterator(this, li_FirstIndex);
}
// ISequence Members
public object Front
{
get { return this.Begin.Current; }
set { this.Begin.Current = value; }
}
public object Back
{
get { return (this.End-1).Current; }
set { (this.End-1).Current = value; }
}
public void PushFront(object ak_Value)
{
Insert(this.Begin, ak_Value);
}
public void PopFront()
{
Erase(this.Begin);
}
public void PushBack(object ak_Value)
{
Insert(this.End, ak_Value);
}
public void PopBack()
{
Erase(this.End-1);
}
public void Assign(k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
Clear();
Insert(this.End, ak_SrcBegin, ak_SrcEnd);
}
public void Assign(object ak_Value, int ai_Count)
{
Clear();
Insert(this.End, ak_Value, ai_Count);
}
public void Insert(k_Iterator ak_Where, object ak_Value)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this collection.");
this.Insert(((k_IListIterator)ak_Where).Index, ak_Value);
}
public void Insert(k_Iterator ak_Where, k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this collection.");
InsertRange(((k_IListIterator)ak_Where).Index, new k_CollectionOnIterators(ak_SrcBegin, ak_SrcEnd));
}
public void Insert(k_Iterator ak_Where, object ak_Value, int ai_Count)
{
int li_Pos = ((k_IListIterator)ak_Where).Index;
for (int i=0; i<ai_Count; ++i)
base.Insert(li_Pos+i, ak_Value);
}
#region ICloneable Members
public override object Clone()
{
return new k_Vector(this);
}
#endregion
}
}

View File

@@ -0,0 +1,88 @@
using System;
/*
* $Id: Adler32.cs,v 1.1 2006/06/16 10:56:20 psoares33 Exp $
*
Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This program is based on zlib-1.1.3, so all credit should go authors
* Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
* and contributors of zlib.
*/
namespace System.util.zlib {
internal sealed class Adler32{
// largest prime smaller than 65536
private const int BASE=65521;
// NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
private const int NMAX=5552;
internal long adler32(long adler, byte[] buf, int index, int len){
if(buf == null){ return 1L; }
long s1=adler&0xffff;
long s2=(adler>>16)&0xffff;
int k;
while(len > 0) {
k=len<NMAX?len:NMAX;
len-=k;
while(k>=16){
s1+=buf[index++]&0xff; s2+=s1;
s1+=buf[index++]&0xff; s2+=s1;
s1+=buf[index++]&0xff; s2+=s1;
s1+=buf[index++]&0xff; s2+=s1;
s1+=buf[index++]&0xff; s2+=s1;
s1+=buf[index++]&0xff; s2+=s1;
s1+=buf[index++]&0xff; s2+=s1;
s1+=buf[index++]&0xff; s2+=s1;
s1+=buf[index++]&0xff; s2+=s1;
s1+=buf[index++]&0xff; s2+=s1;
s1+=buf[index++]&0xff; s2+=s1;
s1+=buf[index++]&0xff; s2+=s1;
s1+=buf[index++]&0xff; s2+=s1;
s1+=buf[index++]&0xff; s2+=s1;
s1+=buf[index++]&0xff; s2+=s1;
s1+=buf[index++]&0xff; s2+=s1;
k-=16;
}
if(k!=0){
do{
s1+=buf[index++]&0xff; s2+=s1;
}
while(--k!=0);
}
s1%=BASE;
s2%=BASE;
}
return (s2<<16)|s1;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,618 @@
using System;
/*
* $Id: InfBlocks.cs,v 1.1 2006/06/16 10:56:20 psoares33 Exp $
*
Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This program is based on zlib-1.1.3, so all credit should go authors
* Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
* and contributors of zlib.
*/
namespace System.util.zlib {
internal sealed class InfBlocks{
private const int MANY=1440;
// And'ing with mask[n] masks the lower n bits
static private int[] inflate_mask = {
0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff,
0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff,
0x00007fff, 0x0000ffff
};
// Table for deflate from PKZIP's appnote.txt.
static int[] border = { // Order of the bit length code lengths
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
};
private const int Z_OK=0;
private const int Z_STREAM_END=1;
private const int Z_NEED_DICT=2;
private const int Z_ERRNO=-1;
private const int Z_STREAM_ERROR=-2;
private const int Z_DATA_ERROR=-3;
private const int Z_MEM_ERROR=-4;
private const int Z_BUF_ERROR=-5;
private const int Z_VERSION_ERROR=-6;
private const int TYPE=0; // get type bits (3, including end bit)
private const int LENS=1; // get lengths for stored
private const int STORED=2;// processing stored block
private const int TABLE=3; // get table lengths
private const int BTREE=4; // get bit lengths tree for a dynamic block
private const int DTREE=5; // get length, distance trees for a dynamic block
private const int CODES=6; // processing fixed or dynamic block
private const int DRY=7; // output remaining window bytes
private const int DONE=8; // finished last block, done
private const int BAD=9; // ot a data error--stuck here
internal int mode; // current inflate_block mode
internal int left; // if STORED, bytes left to copy
internal int table; // table lengths (14 bits)
internal int index; // index into blens (or border)
internal int[] blens; // bit lengths of codes
internal int[] bb=new int[1]; // bit length tree depth
internal int[] tb=new int[1]; // bit length decoding tree
internal InfCodes codes=new InfCodes(); // if CODES, current state
int last; // true if this block is the last block
// mode independent information
internal int bitk; // bits in bit buffer
internal int bitb; // bit buffer
internal int[] hufts; // single malloc for tree space
internal byte[] window; // sliding window
internal int end; // one byte after sliding window
internal int read; // window read pointer
internal int write; // window write pointer
internal Object checkfn; // check function
internal long check; // check on output
internal InfTree inftree=new InfTree();
internal InfBlocks(ZStream z, Object checkfn, int w){
hufts=new int[MANY*3];
window=new byte[w];
end=w;
this.checkfn = checkfn;
mode = TYPE;
reset(z, null);
}
internal void reset(ZStream z, long[] c){
if(c!=null) c[0]=check;
if(mode==BTREE || mode==DTREE){
}
if(mode==CODES){
codes.free(z);
}
mode=TYPE;
bitk=0;
bitb=0;
read=write=0;
if(checkfn != null)
z.adler=check=z._adler.adler32(0L, null, 0, 0);
}
internal int proc(ZStream z, int r){
int t; // temporary storage
int b; // bit buffer
int k; // bits in bit buffer
int p; // input data pointer
int n; // bytes available there
int q; // output window write pointer
int m; { // bytes to end of window or read pointer
// copy input/output information to locals (UPDATE macro restores)
p=z.next_in_index;n=z.avail_in;b=bitb;k=bitk;} {
q=write;m=(int)(q<read?read-q-1:end-q);}
// process input based on current state
while(true){
switch (mode){
case TYPE:
while(k<(3)){
if(n!=0){
r=Z_OK;
}
else{
bitb=b; bitk=k;
z.avail_in=n;
z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z,r);
};
n--;
b|=(z.next_in[p++]&0xff)<<k;
k+=8;
}
t = (int)(b & 7);
last = t & 1;
switch (t >> 1){
case 0: { // stored
b>>=(3);k-=(3);}
t = k & 7; { // go to byte boundary
b>>=(t);k-=(t);}
mode = LENS; // get length of stored block
break;
case 1: { // fixed
int[] bl=new int[1];
int[] bd=new int[1];
int[][] tl=new int[1][];
int[][] td=new int[1][];
InfTree.inflate_trees_fixed(bl, bd, tl, td, z);
codes.init(bl[0], bd[0], tl[0], 0, td[0], 0, z);
} {
b>>=(3);k-=(3);}
mode = CODES;
break;
case 2: { // dynamic
b>>=(3);k-=(3);}
mode = TABLE;
break;
case 3: { // illegal
b>>=(3);k-=(3);}
mode = BAD;
z.msg = "invalid block type";
r = Z_DATA_ERROR;
bitb=b; bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z,r);
}
break;
case LENS:
while(k<(32)){
if(n!=0){
r=Z_OK;
}
else{
bitb=b; bitk=k;
z.avail_in=n;
z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z,r);
};
n--;
b|=(z.next_in[p++]&0xff)<<k;
k+=8;
}
if ((((~b) >> 16) & 0xffff) != (b & 0xffff)){
mode = BAD;
z.msg = "invalid stored block lengths";
r = Z_DATA_ERROR;
bitb=b; bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z,r);
}
left = (b & 0xffff);
b = k = 0; // dump bits
mode = left!=0 ? STORED : (last!=0 ? DRY : TYPE);
break;
case STORED:
if (n == 0){
bitb=b; bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z,r);
}
if(m==0){
if(q==end&&read!=0){
q=0; m=(int)(q<read?read-q-1:end-q);
}
if(m==0){
write=q;
r=inflate_flush(z,r);
q=write;m=(int)(q<read?read-q-1:end-q);
if(q==end&&read!=0){
q=0; m=(int)(q<read?read-q-1:end-q);
}
if(m==0){
bitb=b; bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z,r);
}
}
}
r=Z_OK;
t = left;
if(t>n) t = n;
if(t>m) t = m;
System.Array.Copy(z.next_in, p, window, q, t);
p += t; n -= t;
q += t; m -= t;
if ((left -= t) != 0)
break;
mode = last!=0 ? DRY : TYPE;
break;
case TABLE:
while(k<(14)){
if(n!=0){
r=Z_OK;
}
else{
bitb=b; bitk=k;
z.avail_in=n;
z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z,r);
};
n--;
b|=(z.next_in[p++]&0xff)<<k;
k+=8;
}
table = t = (b & 0x3fff);
if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29) {
mode = BAD;
z.msg = "too many length or distance symbols";
r = Z_DATA_ERROR;
bitb=b; bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z,r);
}
t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
if(blens==null || blens.Length<t){
blens=new int[t];
}
else{
for(int i=0; i<t; i++){blens[i]=0;}
} {
b>>=(14);k-=(14);}
index = 0;
mode = BTREE;
goto case BTREE;
case BTREE:
while (index < 4 + (table >> 10)){
while(k<(3)){
if(n!=0){
r=Z_OK;
}
else{
bitb=b; bitk=k;
z.avail_in=n;
z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z,r);
};
n--;
b|=(z.next_in[p++]&0xff)<<k;
k+=8;
}
blens[border[index++]] = b&7; {
b>>=(3);k-=(3);}
}
while(index < 19){
blens[border[index++]] = 0;
}
bb[0] = 7;
t = inftree.inflate_trees_bits(blens, bb, tb, hufts, z);
if (t != Z_OK){
r = t;
if (r == Z_DATA_ERROR){
blens=null;
mode = BAD;
}
bitb=b; bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z,r);
}
index = 0;
mode = DTREE;
goto case DTREE;
case DTREE:
while (true){
t = table;
if(!(index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))){
break;
}
int i, j, c;
t = bb[0];
while(k<(t)){
if(n!=0){
r=Z_OK;
}
else{
bitb=b; bitk=k;
z.avail_in=n;
z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z,r);
};
n--;
b|=(z.next_in[p++]&0xff)<<k;
k+=8;
}
if(tb[0]==-1){
//System.err.println("null...");
}
t=hufts[(tb[0]+(b&inflate_mask[t]))*3+1];
c=hufts[(tb[0]+(b&inflate_mask[t]))*3+2];
if (c < 16){
b>>=(t);k-=(t);
blens[index++] = c;
}
else { // c == 16..18
i = c == 18 ? 7 : c - 14;
j = c == 18 ? 11 : 3;
while(k<(t+i)){
if(n!=0){
r=Z_OK;
}
else{
bitb=b; bitk=k;
z.avail_in=n;
z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z,r);
};
n--;
b|=(z.next_in[p++]&0xff)<<k;
k+=8;
}
b>>=(t);k-=(t);
j += (b & inflate_mask[i]);
b>>=(i);k-=(i);
i = index;
t = table;
if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
(c == 16 && i < 1)){
blens=null;
mode = BAD;
z.msg = "invalid bit length repeat";
r = Z_DATA_ERROR;
bitb=b; bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z,r);
}
c = c == 16 ? blens[i-1] : 0;
do{
blens[i++] = c;
}
while (--j!=0);
index = i;
}
}
tb[0]=-1; {
int[] bl=new int[1];
int[] bd=new int[1];
int[] tl=new int[1];
int[] td=new int[1];
bl[0] = 9; // must be <= 9 for lookahead assumptions
bd[0] = 6; // must be <= 9 for lookahead assumptions
t = table;
t = inftree.inflate_trees_dynamic(257 + (t & 0x1f),
1 + ((t >> 5) & 0x1f),
blens, bl, bd, tl, td, hufts, z);
if (t != Z_OK){
if (t == Z_DATA_ERROR){
blens=null;
mode = BAD;
}
r = t;
bitb=b; bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z,r);
}
codes.init(bl[0], bd[0], hufts, tl[0], hufts, td[0], z);
}
mode = CODES;
goto case CODES;
case CODES:
bitb=b; bitk=k;
z.avail_in=n; z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
if ((r = codes.proc(this, z, r)) != Z_STREAM_END){
return inflate_flush(z, r);
}
r = Z_OK;
codes.free(z);
p=z.next_in_index; n=z.avail_in;b=bitb;k=bitk;
q=write;m=(int)(q<read?read-q-1:end-q);
if (last==0){
mode = TYPE;
break;
}
mode = DRY;
goto case DRY;
case DRY:
write=q;
r=inflate_flush(z, r);
q=write; m=(int)(q<read?read-q-1:end-q);
if (read != write){
bitb=b; bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z, r);
}
mode = DONE;
goto case DONE;
case DONE:
r = Z_STREAM_END;
bitb=b; bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z, r);
case BAD:
r = Z_DATA_ERROR;
bitb=b; bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z, r);
default:
r = Z_STREAM_ERROR;
bitb=b; bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
write=q;
return inflate_flush(z, r);
}
}
}
internal void free(ZStream z){
reset(z, null);
window=null;
hufts=null;
//ZFREE(z, s);
}
internal void set_dictionary(byte[] d, int start, int n){
System.Array.Copy(d, start, window, 0, n);
read = write = n;
}
// Returns true if inflate is currently at the end of a block generated
// by Z_SYNC_FLUSH or Z_FULL_FLUSH.
internal int sync_point(){
return mode == LENS ? 1 : 0;
}
// copy as much as possible from the sliding window to the output area
internal int inflate_flush(ZStream z, int r){
int n;
int p;
int q;
// local copies of source and destination pointers
p = z.next_out_index;
q = read;
// compute number of bytes to copy as far as end of window
n = (int)((q <= write ? write : end) - q);
if (n > z.avail_out) n = z.avail_out;
if (n!=0 && r == Z_BUF_ERROR) r = Z_OK;
// update counters
z.avail_out -= n;
z.total_out += n;
// update check information
if(checkfn != null)
z.adler=check=z._adler.adler32(check, window, q, n);
// copy as far as end of window
System.Array.Copy(window, q, z.next_out, p, n);
p += n;
q += n;
// see if more to copy at beginning of window
if (q == end){
// wrap pointers
q = 0;
if (write == end)
write = 0;
// compute bytes to copy
n = write - q;
if (n > z.avail_out) n = z.avail_out;
if (n!=0 && r == Z_BUF_ERROR) r = Z_OK;
// update counters
z.avail_out -= n;
z.total_out += n;
// update check information
if(checkfn != null)
z.adler=check=z._adler.adler32(check, window, q, n);
// copy
System.Array.Copy(window, q, z.next_out, p, n);
p += n;
q += n;
}
// update pointers
z.next_out_index = p;
read = q;
// done
return r;
}
}
}

View File

@@ -0,0 +1,611 @@
using System;
/*
* $Id: InfCodes.cs,v 1.1 2006/06/16 10:56:20 psoares33 Exp $
*
Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This program is based on zlib-1.1.3, so all credit should go authors
* Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
* and contributors of zlib.
*/
namespace System.util.zlib {
internal sealed class InfCodes{
static private int[] inflate_mask = {
0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff,
0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff,
0x00007fff, 0x0000ffff
};
private const int Z_OK=0;
private const int Z_STREAM_END=1;
private const int Z_NEED_DICT=2;
private const int Z_ERRNO=-1;
private const int Z_STREAM_ERROR=-2;
private const int Z_DATA_ERROR=-3;
private const int Z_MEM_ERROR=-4;
private const int Z_BUF_ERROR=-5;
private const int Z_VERSION_ERROR=-6;
// waiting for "i:"=input,
// "o:"=output,
// "x:"=nothing
private const int START=0; // x: set up for LEN
private const int LEN=1; // i: get length/literal/eob next
private const int LENEXT=2; // i: getting length extra (have base)
private const int DIST=3; // i: get distance next
private const int DISTEXT=4;// i: getting distance extra
private const int COPY=5; // o: copying bytes in window, waiting for space
private const int LIT=6; // o: got literal, waiting for output space
private const int WASH=7; // o: got eob, possibly still output waiting
private const int END=8; // x: got eob and all data flushed
private const int BADCODE=9;// x: got error
int mode; // current inflate_codes mode
// mode dependent information
int len;
int[] tree; // pointer into tree
int tree_index=0;
int need; // bits needed
int lit;
// if EXT or COPY, where and how much
int get; // bits to get for extra
int dist; // distance back to copy from
byte lbits; // ltree bits decoded per branch
byte dbits; // dtree bits decoder per branch
int[] ltree; // literal/length/eob tree
int ltree_index; // literal/length/eob tree
int[] dtree; // distance tree
int dtree_index; // distance tree
internal InfCodes(){
}
internal void init(int bl, int bd,
int[] tl, int tl_index,
int[] td, int td_index, ZStream z){
mode=START;
lbits=(byte)bl;
dbits=(byte)bd;
ltree=tl;
ltree_index=tl_index;
dtree = td;
dtree_index=td_index;
tree=null;
}
internal int proc(InfBlocks s, ZStream z, int r){
int j; // temporary storage
int tindex; // temporary pointer
int e; // extra bits or operation
int b=0; // bit buffer
int k=0; // bits in bit buffer
int p=0; // input data pointer
int n; // bytes available there
int q; // output window write pointer
int m; // bytes to end of window or read pointer
int f; // pointer to copy strings from
// copy input/output information to locals (UPDATE macro restores)
p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk;
q=s.write;m=q<s.read?s.read-q-1:s.end-q;
// process input and output based on current state
while (true){
switch (mode){
// waiting for "i:"=input, "o:"=output, "x:"=nothing
case START: // x: set up for LEN
if (m >= 258 && n >= 10){
s.bitb=b;s.bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
s.write=q;
r = inflate_fast(lbits, dbits,
ltree, ltree_index,
dtree, dtree_index,
s, z);
p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk;
q=s.write;m=q<s.read?s.read-q-1:s.end-q;
if (r != Z_OK){
mode = r == Z_STREAM_END ? WASH : BADCODE;
break;
}
}
need = lbits;
tree = ltree;
tree_index=ltree_index;
mode = LEN;
goto case LEN;
case LEN: // i: get length/literal/eob next
j = need;
while(k<(j)){
if(n!=0)r=Z_OK;
else{
s.bitb=b;s.bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
s.write=q;
return s.inflate_flush(z,r);
}
n--;
b|=(z.next_in[p++]&0xff)<<k;
k+=8;
}
tindex=(tree_index+(b&inflate_mask[j]))*3;
b>>=(tree[tindex+1]);
k-=(tree[tindex+1]);
e=tree[tindex];
if(e == 0){ // literal
lit = tree[tindex+2];
mode = LIT;
break;
}
if((e & 16)!=0 ){ // length
get = e & 15;
len = tree[tindex+2];
mode = LENEXT;
break;
}
if ((e & 64) == 0){ // next table
need = e;
tree_index = tindex/3+tree[tindex+2];
break;
}
if ((e & 32)!=0){ // end of block
mode = WASH;
break;
}
mode = BADCODE; // invalid code
z.msg = "invalid literal/length code";
r = Z_DATA_ERROR;
s.bitb=b;s.bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
s.write=q;
return s.inflate_flush(z,r);
case LENEXT: // i: getting length extra (have base)
j = get;
while(k<(j)){
if(n!=0)r=Z_OK;
else{
s.bitb=b;s.bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
s.write=q;
return s.inflate_flush(z,r);
}
n--; b|=(z.next_in[p++]&0xff)<<k;
k+=8;
}
len += (b & inflate_mask[j]);
b>>=j;
k-=j;
need = dbits;
tree = dtree;
tree_index=dtree_index;
mode = DIST;
goto case DIST;
case DIST: // i: get distance next
j = need;
while(k<(j)){
if(n!=0)r=Z_OK;
else{
s.bitb=b;s.bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
s.write=q;
return s.inflate_flush(z,r);
}
n--; b|=(z.next_in[p++]&0xff)<<k;
k+=8;
}
tindex=(tree_index+(b & inflate_mask[j]))*3;
b>>=tree[tindex+1];
k-=tree[tindex+1];
e = (tree[tindex]);
if((e & 16)!=0){ // distance
get = e & 15;
dist = tree[tindex+2];
mode = DISTEXT;
break;
}
if ((e & 64) == 0){ // next table
need = e;
tree_index = tindex/3 + tree[tindex+2];
break;
}
mode = BADCODE; // invalid code
z.msg = "invalid distance code";
r = Z_DATA_ERROR;
s.bitb=b;s.bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
s.write=q;
return s.inflate_flush(z,r);
case DISTEXT: // i: getting distance extra
j = get;
while(k<(j)){
if(n!=0)r=Z_OK;
else{
s.bitb=b;s.bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
s.write=q;
return s.inflate_flush(z,r);
}
n--; b|=(z.next_in[p++]&0xff)<<k;
k+=8;
}
dist += (b & inflate_mask[j]);
b>>=j;
k-=j;
mode = COPY;
goto case COPY;
case COPY: // o: copying bytes in window, waiting for space
f = q - dist;
while(f < 0){ // modulo window size-"while" instead
f += s.end; // of "if" handles invalid distances
}
while (len!=0){
if(m==0){
if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
if(m==0){
s.write=q; r=s.inflate_flush(z,r);
q=s.write;m=q<s.read?s.read-q-1:s.end-q;
if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
if(m==0){
s.bitb=b;s.bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
s.write=q;
return s.inflate_flush(z,r);
}
}
}
s.window[q++]=s.window[f++]; m--;
if (f == s.end)
f = 0;
len--;
}
mode = START;
break;
case LIT: // o: got literal, waiting for output space
if(m==0){
if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
if(m==0){
s.write=q; r=s.inflate_flush(z,r);
q=s.write;m=q<s.read?s.read-q-1:s.end-q;
if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
if(m==0){
s.bitb=b;s.bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
s.write=q;
return s.inflate_flush(z,r);
}
}
}
r=Z_OK;
s.window[q++]=(byte)lit; m--;
mode = START;
break;
case WASH: // o: got eob, possibly more output
if (k > 7){ // return unused byte, if any
k -= 8;
n++;
p--; // can always return one
}
s.write=q; r=s.inflate_flush(z,r);
q=s.write;m=q<s.read?s.read-q-1:s.end-q;
if (s.read != s.write){
s.bitb=b;s.bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
s.write=q;
return s.inflate_flush(z,r);
}
mode = END;
goto case END;
case END:
r = Z_STREAM_END;
s.bitb=b;s.bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
s.write=q;
return s.inflate_flush(z,r);
case BADCODE: // x: got error
r = Z_DATA_ERROR;
s.bitb=b;s.bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
s.write=q;
return s.inflate_flush(z,r);
default:
r = Z_STREAM_ERROR;
s.bitb=b;s.bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
s.write=q;
return s.inflate_flush(z,r);
}
}
}
internal void free(ZStream z){
// ZFREE(z, c);
}
// Called with number of bytes left to write in window at least 258
// (the maximum string length) and number of input bytes available
// at least ten. The ten bytes are six bytes for the longest length/
// distance pair plus four bytes for overloading the bit buffer.
internal int inflate_fast(int bl, int bd,
int[] tl, int tl_index,
int[] td, int td_index,
InfBlocks s, ZStream z){
int t; // temporary pointer
int[] tp; // temporary pointer
int tp_index; // temporary pointer
int e; // extra bits or operation
int b; // bit buffer
int k; // bits in bit buffer
int p; // input data pointer
int n; // bytes available there
int q; // output window write pointer
int m; // bytes to end of window or read pointer
int ml; // mask for literal/length tree
int md; // mask for distance tree
int c; // bytes to copy
int d; // distance back to copy from
int r; // copy source pointer
int tp_index_t_3; // (tp_index+t)*3
// load input, output, bit values
p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk;
q=s.write;m=q<s.read?s.read-q-1:s.end-q;
// initialize masks
ml = inflate_mask[bl];
md = inflate_mask[bd];
// do until not enough input or output space for fast loop
do { // assume called with m >= 258 && n >= 10
// get literal/length code
while(k<(20)){ // max bits for literal/length code
n--;
b|=(z.next_in[p++]&0xff)<<k;k+=8;
}
t= b&ml;
tp=tl;
tp_index=tl_index;
tp_index_t_3=(tp_index+t)*3;
if ((e = tp[tp_index_t_3]) == 0){
b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
s.window[q++] = (byte)tp[tp_index_t_3+2];
m--;
continue;
}
do {
b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
if((e&16)!=0){
e &= 15;
c = tp[tp_index_t_3+2] + ((int)b & inflate_mask[e]);
b>>=e; k-=e;
// decode distance base of block to copy
while(k<(15)){ // max bits for distance code
n--;
b|=(z.next_in[p++]&0xff)<<k;k+=8;
}
t= b&md;
tp=td;
tp_index=td_index;
tp_index_t_3=(tp_index+t)*3;
e = tp[tp_index_t_3];
do {
b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
if((e&16)!=0){
// get extra bits to add to distance base
e &= 15;
while(k<(e)){ // get extra bits (up to 13)
n--;
b|=(z.next_in[p++]&0xff)<<k;k+=8;
}
d = tp[tp_index_t_3+2] + (b&inflate_mask[e]);
b>>=(e); k-=(e);
// do the copy
m -= c;
if (q >= d){ // offset before dest
// just copy
r=q-d;
if(q-r>0 && 2>(q-r)){
s.window[q++]=s.window[r++]; // minimum count is three,
s.window[q++]=s.window[r++]; // so unroll loop a little
c-=2;
}
else{
System.Array.Copy(s.window, r, s.window, q, 2);
q+=2; r+=2; c-=2;
}
}
else{ // else offset after destination
r=q-d;
do{
r+=s.end; // force pointer in window
}while(r<0); // covers invalid distances
e=s.end-r;
if(c>e){ // if source crosses,
c-=e; // wrapped copy
if(q-r>0 && e>(q-r)){
do{s.window[q++] = s.window[r++];}
while(--e!=0);
}
else{
System.Array.Copy(s.window, r, s.window, q, e);
q+=e; r+=e; e=0;
}
r = 0; // copy rest from start of window
}
}
// copy all or what's left
if(q-r>0 && c>(q-r)){
do{s.window[q++] = s.window[r++];}
while(--c!=0);
}
else{
System.Array.Copy(s.window, r, s.window, q, c);
q+=c; r+=c; c=0;
}
break;
}
else if((e&64)==0){
t+=tp[tp_index_t_3+2];
t+=(b&inflate_mask[e]);
tp_index_t_3=(tp_index+t)*3;
e=tp[tp_index_t_3];
}
else{
z.msg = "invalid distance code";
c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
s.bitb=b;s.bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
s.write=q;
return Z_DATA_ERROR;
}
}
while(true);
break;
}
if((e&64)==0){
t+=tp[tp_index_t_3+2];
t+=(b&inflate_mask[e]);
tp_index_t_3=(tp_index+t)*3;
if((e=tp[tp_index_t_3])==0){
b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
s.window[q++]=(byte)tp[tp_index_t_3+2];
m--;
break;
}
}
else if((e&32)!=0){
c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
s.bitb=b;s.bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
s.write=q;
return Z_STREAM_END;
}
else{
z.msg="invalid literal/length code";
c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
s.bitb=b;s.bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
s.write=q;
return Z_DATA_ERROR;
}
}
while(true);
}
while(m>=258 && n>= 10);
// not enough input or output--restore pointers and return
c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
s.bitb=b;s.bitk=k;
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
s.write=q;
return Z_OK;
}
}
}

View File

@@ -0,0 +1,523 @@
using System;
/*
* $Id: InfTree.cs,v 1.1 2006/06/16 10:56:20 psoares33 Exp $
*
Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This program is based on zlib-1.1.3, so all credit should go authors
* Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
* and contributors of zlib.
*/
namespace System.util.zlib {
internal sealed class InfTree{
private const int MANY=1440;
private const int Z_OK=0;
private const int Z_STREAM_END=1;
private const int Z_NEED_DICT=2;
private const int Z_ERRNO=-1;
private const int Z_STREAM_ERROR=-2;
private const int Z_DATA_ERROR=-3;
private const int Z_MEM_ERROR=-4;
private const int Z_BUF_ERROR=-5;
private const int Z_VERSION_ERROR=-6;
private const int fixed_bl = 9;
private const int fixed_bd = 5;
static int[] fixed_tl = {
96,7,256, 0,8,80, 0,8,16, 84,8,115,
82,7,31, 0,8,112, 0,8,48, 0,9,192,
80,7,10, 0,8,96, 0,8,32, 0,9,160,
0,8,0, 0,8,128, 0,8,64, 0,9,224,
80,7,6, 0,8,88, 0,8,24, 0,9,144,
83,7,59, 0,8,120, 0,8,56, 0,9,208,
81,7,17, 0,8,104, 0,8,40, 0,9,176,
0,8,8, 0,8,136, 0,8,72, 0,9,240,
80,7,4, 0,8,84, 0,8,20, 85,8,227,
83,7,43, 0,8,116, 0,8,52, 0,9,200,
81,7,13, 0,8,100, 0,8,36, 0,9,168,
0,8,4, 0,8,132, 0,8,68, 0,9,232,
80,7,8, 0,8,92, 0,8,28, 0,9,152,
84,7,83, 0,8,124, 0,8,60, 0,9,216,
82,7,23, 0,8,108, 0,8,44, 0,9,184,
0,8,12, 0,8,140, 0,8,76, 0,9,248,
80,7,3, 0,8,82, 0,8,18, 85,8,163,
83,7,35, 0,8,114, 0,8,50, 0,9,196,
81,7,11, 0,8,98, 0,8,34, 0,9,164,
0,8,2, 0,8,130, 0,8,66, 0,9,228,
80,7,7, 0,8,90, 0,8,26, 0,9,148,
84,7,67, 0,8,122, 0,8,58, 0,9,212,
82,7,19, 0,8,106, 0,8,42, 0,9,180,
0,8,10, 0,8,138, 0,8,74, 0,9,244,
80,7,5, 0,8,86, 0,8,22, 192,8,0,
83,7,51, 0,8,118, 0,8,54, 0,9,204,
81,7,15, 0,8,102, 0,8,38, 0,9,172,
0,8,6, 0,8,134, 0,8,70, 0,9,236,
80,7,9, 0,8,94, 0,8,30, 0,9,156,
84,7,99, 0,8,126, 0,8,62, 0,9,220,
82,7,27, 0,8,110, 0,8,46, 0,9,188,
0,8,14, 0,8,142, 0,8,78, 0,9,252,
96,7,256, 0,8,81, 0,8,17, 85,8,131,
82,7,31, 0,8,113, 0,8,49, 0,9,194,
80,7,10, 0,8,97, 0,8,33, 0,9,162,
0,8,1, 0,8,129, 0,8,65, 0,9,226,
80,7,6, 0,8,89, 0,8,25, 0,9,146,
83,7,59, 0,8,121, 0,8,57, 0,9,210,
81,7,17, 0,8,105, 0,8,41, 0,9,178,
0,8,9, 0,8,137, 0,8,73, 0,9,242,
80,7,4, 0,8,85, 0,8,21, 80,8,258,
83,7,43, 0,8,117, 0,8,53, 0,9,202,
81,7,13, 0,8,101, 0,8,37, 0,9,170,
0,8,5, 0,8,133, 0,8,69, 0,9,234,
80,7,8, 0,8,93, 0,8,29, 0,9,154,
84,7,83, 0,8,125, 0,8,61, 0,9,218,
82,7,23, 0,8,109, 0,8,45, 0,9,186,
0,8,13, 0,8,141, 0,8,77, 0,9,250,
80,7,3, 0,8,83, 0,8,19, 85,8,195,
83,7,35, 0,8,115, 0,8,51, 0,9,198,
81,7,11, 0,8,99, 0,8,35, 0,9,166,
0,8,3, 0,8,131, 0,8,67, 0,9,230,
80,7,7, 0,8,91, 0,8,27, 0,9,150,
84,7,67, 0,8,123, 0,8,59, 0,9,214,
82,7,19, 0,8,107, 0,8,43, 0,9,182,
0,8,11, 0,8,139, 0,8,75, 0,9,246,
80,7,5, 0,8,87, 0,8,23, 192,8,0,
83,7,51, 0,8,119, 0,8,55, 0,9,206,
81,7,15, 0,8,103, 0,8,39, 0,9,174,
0,8,7, 0,8,135, 0,8,71, 0,9,238,
80,7,9, 0,8,95, 0,8,31, 0,9,158,
84,7,99, 0,8,127, 0,8,63, 0,9,222,
82,7,27, 0,8,111, 0,8,47, 0,9,190,
0,8,15, 0,8,143, 0,8,79, 0,9,254,
96,7,256, 0,8,80, 0,8,16, 84,8,115,
82,7,31, 0,8,112, 0,8,48, 0,9,193,
80,7,10, 0,8,96, 0,8,32, 0,9,161,
0,8,0, 0,8,128, 0,8,64, 0,9,225,
80,7,6, 0,8,88, 0,8,24, 0,9,145,
83,7,59, 0,8,120, 0,8,56, 0,9,209,
81,7,17, 0,8,104, 0,8,40, 0,9,177,
0,8,8, 0,8,136, 0,8,72, 0,9,241,
80,7,4, 0,8,84, 0,8,20, 85,8,227,
83,7,43, 0,8,116, 0,8,52, 0,9,201,
81,7,13, 0,8,100, 0,8,36, 0,9,169,
0,8,4, 0,8,132, 0,8,68, 0,9,233,
80,7,8, 0,8,92, 0,8,28, 0,9,153,
84,7,83, 0,8,124, 0,8,60, 0,9,217,
82,7,23, 0,8,108, 0,8,44, 0,9,185,
0,8,12, 0,8,140, 0,8,76, 0,9,249,
80,7,3, 0,8,82, 0,8,18, 85,8,163,
83,7,35, 0,8,114, 0,8,50, 0,9,197,
81,7,11, 0,8,98, 0,8,34, 0,9,165,
0,8,2, 0,8,130, 0,8,66, 0,9,229,
80,7,7, 0,8,90, 0,8,26, 0,9,149,
84,7,67, 0,8,122, 0,8,58, 0,9,213,
82,7,19, 0,8,106, 0,8,42, 0,9,181,
0,8,10, 0,8,138, 0,8,74, 0,9,245,
80,7,5, 0,8,86, 0,8,22, 192,8,0,
83,7,51, 0,8,118, 0,8,54, 0,9,205,
81,7,15, 0,8,102, 0,8,38, 0,9,173,
0,8,6, 0,8,134, 0,8,70, 0,9,237,
80,7,9, 0,8,94, 0,8,30, 0,9,157,
84,7,99, 0,8,126, 0,8,62, 0,9,221,
82,7,27, 0,8,110, 0,8,46, 0,9,189,
0,8,14, 0,8,142, 0,8,78, 0,9,253,
96,7,256, 0,8,81, 0,8,17, 85,8,131,
82,7,31, 0,8,113, 0,8,49, 0,9,195,
80,7,10, 0,8,97, 0,8,33, 0,9,163,
0,8,1, 0,8,129, 0,8,65, 0,9,227,
80,7,6, 0,8,89, 0,8,25, 0,9,147,
83,7,59, 0,8,121, 0,8,57, 0,9,211,
81,7,17, 0,8,105, 0,8,41, 0,9,179,
0,8,9, 0,8,137, 0,8,73, 0,9,243,
80,7,4, 0,8,85, 0,8,21, 80,8,258,
83,7,43, 0,8,117, 0,8,53, 0,9,203,
81,7,13, 0,8,101, 0,8,37, 0,9,171,
0,8,5, 0,8,133, 0,8,69, 0,9,235,
80,7,8, 0,8,93, 0,8,29, 0,9,155,
84,7,83, 0,8,125, 0,8,61, 0,9,219,
82,7,23, 0,8,109, 0,8,45, 0,9,187,
0,8,13, 0,8,141, 0,8,77, 0,9,251,
80,7,3, 0,8,83, 0,8,19, 85,8,195,
83,7,35, 0,8,115, 0,8,51, 0,9,199,
81,7,11, 0,8,99, 0,8,35, 0,9,167,
0,8,3, 0,8,131, 0,8,67, 0,9,231,
80,7,7, 0,8,91, 0,8,27, 0,9,151,
84,7,67, 0,8,123, 0,8,59, 0,9,215,
82,7,19, 0,8,107, 0,8,43, 0,9,183,
0,8,11, 0,8,139, 0,8,75, 0,9,247,
80,7,5, 0,8,87, 0,8,23, 192,8,0,
83,7,51, 0,8,119, 0,8,55, 0,9,207,
81,7,15, 0,8,103, 0,8,39, 0,9,175,
0,8,7, 0,8,135, 0,8,71, 0,9,239,
80,7,9, 0,8,95, 0,8,31, 0,9,159,
84,7,99, 0,8,127, 0,8,63, 0,9,223,
82,7,27, 0,8,111, 0,8,47, 0,9,191,
0,8,15, 0,8,143, 0,8,79, 0,9,255
};
static int[] fixed_td = {
80,5,1, 87,5,257, 83,5,17, 91,5,4097,
81,5,5, 89,5,1025, 85,5,65, 93,5,16385,
80,5,3, 88,5,513, 84,5,33, 92,5,8193,
82,5,9, 90,5,2049, 86,5,129, 192,5,24577,
80,5,2, 87,5,385, 83,5,25, 91,5,6145,
81,5,7, 89,5,1537, 85,5,97, 93,5,24577,
80,5,4, 88,5,769, 84,5,49, 92,5,12289,
82,5,13, 90,5,3073, 86,5,193, 192,5,24577
};
// Tables for deflate from PKZIP's appnote.txt.
static int[] cplens = { // Copy lengths for literal codes 257..285
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
};
// see note #13 above about 258
static int[] cplext = { // Extra bits for literal codes 257..285
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112 // 112==invalid
};
static int[] cpdist = { // Copy offsets for distance codes 0..29
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
8193, 12289, 16385, 24577
};
static int[] cpdext = { // Extra bits for distance codes
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
12, 12, 13, 13};
// If BMAX needs to be larger than 16, then h and x[] should be uLong.
const int BMAX=15; // maximum bit length of any code
int[] hn = null; // hufts used in space
int[] v = null; // work area for huft_build
int[] c = null; // bit length count table
int[] r = null; // table entry for structure assignment
int[] u = null; // table stack
int[] x = null; // bit offsets, then code stack
private int huft_build(int[] b, // code lengths in bits (all assumed <= BMAX)
int bindex,
int n, // number of codes (assumed <= 288)
int s, // number of simple-valued codes (0..s-1)
int[] d, // list of base values for non-simple codes
int[] e, // list of extra bits for non-simple codes
int[] t, // result: starting table
int[] m, // maximum lookup bits, returns actual
int[] hp,// space for trees
int[] hn,// hufts used in space
int[] v // working area: values in order of bit length
){
// Given a list of code lengths and a maximum table size, make a set of
// tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
// if the given code set is incomplete (the tables are still built in this
// case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
// lengths), or Z_MEM_ERROR if not enough memory.
int a; // counter for codes of length k
int f; // i repeats in table every f entries
int g; // maximum code length
int h; // table level
int i; // counter, current code
int j; // counter
int k; // number of bits in current code
int l; // bits per table (returned in m)
int mask; // (1 << w) - 1, to avoid cc -O bug on HP
int p; // pointer into c[], b[], or v[]
int q; // points to current table
int w; // bits before this table == (l * h)
int xp; // pointer into x
int y; // number of dummy codes added
int z; // number of entries in current table
// Generate counts for each bit length
p = 0; i = n;
do {
c[b[bindex+p]]++; p++; i--; // assume all entries <= BMAX
}while(i!=0);
if(c[0] == n){ // null input--all zero length codes
t[0] = -1;
m[0] = 0;
return Z_OK;
}
// Find minimum and maximum length, bound *m by those
l = m[0];
for (j = 1; j <= BMAX; j++)
if(c[j]!=0) break;
k = j; // minimum code length
if(l < j){
l = j;
}
for (i = BMAX; i!=0; i--){
if(c[i]!=0) break;
}
g = i; // maximum code length
if(l > i){
l = i;
}
m[0] = l;
// Adjust last length count to fill out codes, if needed
for (y = 1 << j; j < i; j++, y <<= 1){
if ((y -= c[j]) < 0){
return Z_DATA_ERROR;
}
}
if ((y -= c[i]) < 0){
return Z_DATA_ERROR;
}
c[i] += y;
// Generate starting offsets into the value table for each length
x[1] = j = 0;
p = 1; xp = 2;
while (--i!=0) { // note that i == g from above
x[xp] = (j += c[p]);
xp++;
p++;
}
// Make a table of values in order of bit lengths
i = 0; p = 0;
do {
if ((j = b[bindex+p]) != 0){
v[x[j]++] = i;
}
p++;
}
while (++i < n);
n = x[g]; // set n to length of v
// Generate the Huffman codes and for each, make the table entries
x[0] = i = 0; // first Huffman code is zero
p = 0; // grab values in bit order
h = -1; // no tables yet--level -1
w = -l; // bits decoded == (l * h)
u[0] = 0; // just to keep compilers happy
q = 0; // ditto
z = 0; // ditto
// go through the bit lengths (k already is bits in shortest code)
for (; k <= g; k++){
a = c[k];
while (a--!=0){
// here i is the Huffman code of length k bits for value *p
// make tables up to required level
while (k > w + l){
h++;
w += l; // previous table always l bits
// compute minimum size table less than or equal to l bits
z = g - w;
z = (z > l) ? l : z; // table size upper limit
if((f=1<<(j=k-w))>a+1){ // try a k-w bit table
// too few codes for k-w bit table
f -= a + 1; // deduct codes from patterns left
xp = k;
if(j < z){
while (++j < z){ // try smaller tables up to z bits
if((f <<= 1) <= c[++xp])
break; // enough codes to use up j bits
f -= c[xp]; // else deduct codes from patterns
}
}
}
z = 1 << j; // table entries for j-bit table
// allocate new table
if (hn[0] + z > MANY){ // (note: doesn't matter for fixed)
return Z_DATA_ERROR; // overflow of MANY
}
u[h] = q = /*hp+*/ hn[0]; // DEBUG
hn[0] += z;
// connect to last table, if there is one
if(h!=0){
x[h]=i; // save pattern for backing up
r[0]=(byte)j; // bits in this table
r[1]=(byte)l; // bits to dump before this table
j=i>>(w - l);
r[2] = (int)(q - u[h-1] - j); // offset to this table
System.Array.Copy(r, 0, hp, (u[h-1]+j)*3, 3); // connect to last table
}
else{
t[0] = q; // first table is returned result
}
}
// set up table entry in r
r[1] = (byte)(k - w);
if (p >= n){
r[0] = 128 + 64; // out of values--invalid code
}
else if (v[p] < s){
r[0] = (byte)(v[p] < 256 ? 0 : 32 + 64); // 256 is end-of-block
r[2] = v[p++]; // simple code is just the value
}
else{
r[0]=(byte)(e[v[p]-s]+16+64); // non-simple--look up in lists
r[2]=d[v[p++] - s];
}
// fill code-like entries with r
f=1<<(k-w);
for (j=i>>w;j<z;j+=f){
System.Array.Copy(r, 0, hp, (q+j)*3, 3);
}
// backwards increment the k-bit code i
for (j = 1 << (k - 1); (i & j)!=0; j >>= 1){
i ^= j;
}
i ^= j;
// backup over finished tables
mask = (1 << w) - 1; // needed on HP, cc -O bug
while ((i & mask) != x[h]){
h--; // don't need to update q
w -= l;
mask = (1 << w) - 1;
}
}
}
// Return Z_BUF_ERROR if we were given an incomplete table
return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
}
internal int inflate_trees_bits(int[] c, // 19 code lengths
int[] bb, // bits tree desired/actual depth
int[] tb, // bits tree result
int[] hp, // space for trees
ZStream z // for messages
){
int result;
initWorkArea(19);
hn[0]=0;
result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);
if(result == Z_DATA_ERROR){
z.msg = "oversubscribed dynamic bit lengths tree";
}
else if(result == Z_BUF_ERROR || bb[0] == 0){
z.msg = "incomplete dynamic bit lengths tree";
result = Z_DATA_ERROR;
}
return result;
}
internal int inflate_trees_dynamic(int nl, // number of literal/length codes
int nd, // number of distance codes
int[] c, // that many (total) code lengths
int[] bl, // literal desired/actual bit depth
int[] bd, // distance desired/actual bit depth
int[] tl, // literal/length tree result
int[] td, // distance tree result
int[] hp, // space for trees
ZStream z // for messages
){
int result;
// build literal/length tree
initWorkArea(288);
hn[0]=0;
result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
if (result != Z_OK || bl[0] == 0){
if(result == Z_DATA_ERROR){
z.msg = "oversubscribed literal/length tree";
}
else if (result != Z_MEM_ERROR){
z.msg = "incomplete literal/length tree";
result = Z_DATA_ERROR;
}
return result;
}
// build distance tree
initWorkArea(288);
result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);
if (result != Z_OK || (bd[0] == 0 && nl > 257)){
if (result == Z_DATA_ERROR){
z.msg = "oversubscribed distance tree";
}
else if (result == Z_BUF_ERROR) {
z.msg = "incomplete distance tree";
result = Z_DATA_ERROR;
}
else if (result != Z_MEM_ERROR){
z.msg = "empty distance tree with lengths";
result = Z_DATA_ERROR;
}
return result;
}
return Z_OK;
}
internal static int inflate_trees_fixed(int[] bl, //literal desired/actual bit depth
int[] bd, //distance desired/actual bit depth
int[][] tl,//literal/length tree result
int[][] td,//distance tree result
ZStream z //for memory allocation
){
bl[0]=fixed_bl;
bd[0]=fixed_bd;
tl[0]=fixed_tl;
td[0]=fixed_td;
return Z_OK;
}
private void initWorkArea(int vsize){
if(hn==null){
hn=new int[1];
v=new int[vsize];
c=new int[BMAX+1];
r=new int[3];
u=new int[BMAX];
x=new int[BMAX+1];
}
if(v.Length<vsize){ v=new int[vsize]; }
for(int i=0; i<vsize; i++){v[i]=0;}
for(int i=0; i<BMAX+1; i++){c[i]=0;}
for(int i=0; i<3; i++){r[i]=0;}
// for(int i=0; i<BMAX; i++){u[i]=0;}
System.Array.Copy(c, 0, u, 0, BMAX);
// for(int i=0; i<BMAX+1; i++){x[i]=0;}
System.Array.Copy(c, 0, x, 0, BMAX+1);
}
}
}

View File

@@ -0,0 +1,387 @@
using System;
/*
* $Id: Inflate.cs,v 1.1 2006/06/16 10:56:20 psoares33 Exp $
*
Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This program is based on zlib-1.1.3, so all credit should go authors
* Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
* and contributors of zlib.
*/
namespace System.util.zlib {
internal sealed class Inflate{
private const int MAX_WBITS=15; // 32K LZ77 window
// preset dictionary flag in zlib header
private const int PRESET_DICT=0x20;
internal const int Z_NO_FLUSH=0;
internal const int Z_PARTIAL_FLUSH=1;
internal const int Z_SYNC_FLUSH=2;
internal const int Z_FULL_FLUSH=3;
internal const int Z_FINISH=4;
private const int Z_DEFLATED=8;
private const int Z_OK=0;
private const int Z_STREAM_END=1;
private const int Z_NEED_DICT=2;
private const int Z_ERRNO=-1;
private const int Z_STREAM_ERROR=-2;
private const int Z_DATA_ERROR=-3;
private const int Z_MEM_ERROR=-4;
private const int Z_BUF_ERROR=-5;
private const int Z_VERSION_ERROR=-6;
private const int METHOD=0; // waiting for method byte
private const int FLAG=1; // waiting for flag byte
private const int DICT4=2; // four dictionary check bytes to go
private const int DICT3=3; // three dictionary check bytes to go
private const int DICT2=4; // two dictionary check bytes to go
private const int DICT1=5; // one dictionary check byte to go
private const int DICT0=6; // waiting for inflateSetDictionary
private const int BLOCKS=7; // decompressing blocks
private const int CHECK4=8; // four check bytes to go
private const int CHECK3=9; // three check bytes to go
private const int CHECK2=10; // two check bytes to go
private const int CHECK1=11; // one check byte to go
private const int DONE=12; // finished check, done
private const int BAD=13; // got an error--stay here
internal int mode; // current inflate mode
// mode dependent information
internal int method; // if FLAGS, method byte
// if CHECK, check values to compare
internal long[] was=new long[1] ; // computed check value
internal long need; // stream check value
// if BAD, inflateSync's marker bytes count
internal int marker;
// mode independent information
internal int nowrap; // flag for no wrapper
internal int wbits; // log2(window size) (8..15, defaults to 15)
internal InfBlocks blocks; // current inflate_blocks state
internal int inflateReset(ZStream z){
if(z == null || z.istate == null) return Z_STREAM_ERROR;
z.total_in = z.total_out = 0;
z.msg = null;
z.istate.mode = z.istate.nowrap!=0 ? BLOCKS : METHOD;
z.istate.blocks.reset(z, null);
return Z_OK;
}
internal int inflateEnd(ZStream z){
if(blocks != null)
blocks.free(z);
blocks=null;
// ZFREE(z, z->state);
return Z_OK;
}
internal int inflateInit(ZStream z, int w){
z.msg = null;
blocks = null;
// handle undocumented nowrap option (no zlib header or check)
nowrap = 0;
if(w < 0){
w = - w;
nowrap = 1;
}
// set window size
if(w<8 ||w>15){
inflateEnd(z);
return Z_STREAM_ERROR;
}
wbits=w;
z.istate.blocks=new InfBlocks(z,
z.istate.nowrap!=0 ? null : this,
1<<w);
// reset state
inflateReset(z);
return Z_OK;
}
internal int inflate(ZStream z, int f){
int r;
int b;
if(z == null || z.istate == null || z.next_in == null)
return Z_STREAM_ERROR;
f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
r = Z_BUF_ERROR;
while (true){
//System.out.println("mode: "+z.istate.mode);
switch (z.istate.mode){
case METHOD:
if(z.avail_in==0)return r;r=f;
z.avail_in--; z.total_in++;
if(((z.istate.method = z.next_in[z.next_in_index++])&0xf)!=Z_DEFLATED){
z.istate.mode = BAD;
z.msg="unknown compression method";
z.istate.marker = 5; // can't try inflateSync
break;
}
if((z.istate.method>>4)+8>z.istate.wbits){
z.istate.mode = BAD;
z.msg="invalid window size";
z.istate.marker = 5; // can't try inflateSync
break;
}
z.istate.mode=FLAG;
goto case FLAG;
case FLAG:
if(z.avail_in==0)return r;r=f;
z.avail_in--; z.total_in++;
b = (z.next_in[z.next_in_index++])&0xff;
if((((z.istate.method << 8)+b) % 31)!=0){
z.istate.mode = BAD;
z.msg = "incorrect header check";
z.istate.marker = 5; // can't try inflateSync
break;
}
if((b&PRESET_DICT)==0){
z.istate.mode = BLOCKS;
break;
}
z.istate.mode = DICT4;
goto case DICT4;
case DICT4:
if(z.avail_in==0)return r;r=f;
z.avail_in--; z.total_in++;
z.istate.need=((z.next_in[z.next_in_index++]&0xff)<<24)&0xff000000L;
z.istate.mode=DICT3;
goto case DICT3;
case DICT3:
if(z.avail_in==0)return r;r=f;
z.avail_in--; z.total_in++;
z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<16)&0xff0000L;
z.istate.mode=DICT2;
goto case DICT2;
case DICT2:
if(z.avail_in==0)return r;r=f;
z.avail_in--; z.total_in++;
z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<8)&0xff00L;
z.istate.mode=DICT1;
goto case DICT1;
case DICT1:
if(z.avail_in==0)return r;r=f;
z.avail_in--; z.total_in++;
z.istate.need += (z.next_in[z.next_in_index++]&0xffL);
z.adler = z.istate.need;
z.istate.mode = DICT0;
return Z_NEED_DICT;
case DICT0:
z.istate.mode = BAD;
z.msg = "need dictionary";
z.istate.marker = 0; // can try inflateSync
return Z_STREAM_ERROR;
case BLOCKS:
r = z.istate.blocks.proc(z, r);
if(r == Z_DATA_ERROR){
z.istate.mode = BAD;
z.istate.marker = 0; // can try inflateSync
break;
}
if(r == Z_OK){
r = f;
}
if(r != Z_STREAM_END){
return r;
}
r = f;
z.istate.blocks.reset(z, z.istate.was);
if(z.istate.nowrap!=0){
z.istate.mode=DONE;
break;
}
z.istate.mode=CHECK4;
goto case CHECK4;
case CHECK4:
if(z.avail_in==0)return r;r=f;
z.avail_in--; z.total_in++;
z.istate.need=((z.next_in[z.next_in_index++]&0xff)<<24)&0xff000000L;
z.istate.mode=CHECK3;
goto case CHECK3;
case CHECK3:
if(z.avail_in==0)return r;r=f;
z.avail_in--; z.total_in++;
z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<16)&0xff0000L;
z.istate.mode = CHECK2;
goto case CHECK2;
case CHECK2:
if(z.avail_in==0)return r;r=f;
z.avail_in--; z.total_in++;
z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<8)&0xff00L;
z.istate.mode = CHECK1;
goto case CHECK1;
case CHECK1:
if(z.avail_in==0)return r;r=f;
z.avail_in--; z.total_in++;
z.istate.need+=(z.next_in[z.next_in_index++]&0xffL);
if(((int)(z.istate.was[0])) != ((int)(z.istate.need))){
z.istate.mode = BAD;
z.msg = "incorrect data check";
z.istate.marker = 5; // can't try inflateSync
break;
}
z.istate.mode = DONE;
goto case DONE;
case DONE:
return Z_STREAM_END;
case BAD:
return Z_DATA_ERROR;
default:
return Z_STREAM_ERROR;
}
}
}
internal int inflateSetDictionary(ZStream z, byte[] dictionary, int dictLength){
int index=0;
int length = dictLength;
if(z==null || z.istate == null|| z.istate.mode != DICT0)
return Z_STREAM_ERROR;
if(z._adler.adler32(1L, dictionary, 0, dictLength)!=z.adler){
return Z_DATA_ERROR;
}
z.adler = z._adler.adler32(0, null, 0, 0);
if(length >= (1<<z.istate.wbits)){
length = (1<<z.istate.wbits)-1;
index=dictLength - length;
}
z.istate.blocks.set_dictionary(dictionary, index, length);
z.istate.mode = BLOCKS;
return Z_OK;
}
static private byte[] mark = {(byte)0, (byte)0, (byte)0xff, (byte)0xff};
internal int inflateSync(ZStream z){
int n; // number of bytes to look at
int p; // pointer to bytes
int m; // number of marker bytes found in a row
long r, w; // temporaries to save total_in and total_out
// set up
if(z == null || z.istate == null)
return Z_STREAM_ERROR;
if(z.istate.mode != BAD){
z.istate.mode = BAD;
z.istate.marker = 0;
}
if((n=z.avail_in)==0)
return Z_BUF_ERROR;
p=z.next_in_index;
m=z.istate.marker;
// search
while (n!=0 && m < 4){
if(z.next_in[p] == mark[m]){
m++;
}
else if(z.next_in[p]!=0){
m = 0;
}
else{
m = 4 - m;
}
p++; n--;
}
// restore
z.total_in += p-z.next_in_index;
z.next_in_index = p;
z.avail_in = n;
z.istate.marker = m;
// return no joy or set up to restart on a new block
if(m != 4){
return Z_DATA_ERROR;
}
r=z.total_in; w=z.total_out;
inflateReset(z);
z.total_in=r; z.total_out = w;
z.istate.mode = BLOCKS;
return Z_OK;
}
// Returns true if inflate is currently at the end of a block generated
// by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
// implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
// but removes the length bytes of the resulting empty stored block. When
// decompressing, PPP checks that at the end of input packet, inflate is
// waiting for these length bytes.
internal int inflateSyncPoint(ZStream z){
if(z == null || z.istate == null || z.istate.blocks == null)
return Z_STREAM_ERROR;
return z.istate.blocks.sync_point();
}
}
}

View File

@@ -0,0 +1,70 @@
using System;
/*
* $Id: JZlib.cs,v 1.1 2006/06/16 10:56:20 psoares33 Exp $
*
Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This program is based on zlib-1.1.3, so all credit should go authors
* Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
* and contributors of zlib.
*/
namespace System.util.zlib {
public sealed class JZlib{
private const String version_="1.0.2";
public static String version(){return version_;}
// compression levels
public const int Z_NO_COMPRESSION=0;
public const int Z_BEST_SPEED=1;
public const int Z_BEST_COMPRESSION=9;
public const int Z_DEFAULT_COMPRESSION=(-1);
// compression strategy
public const int Z_FILTERED=1;
public const int Z_HUFFMAN_ONLY=2;
public const int Z_DEFAULT_STRATEGY=0;
public const int Z_NO_FLUSH=0;
public const int Z_PARTIAL_FLUSH=1;
public const int Z_SYNC_FLUSH=2;
public const int Z_FULL_FLUSH=3;
public const int Z_FINISH=4;
public const int Z_OK=0;
public const int Z_STREAM_END=1;
public const int Z_NEED_DICT=2;
public const int Z_ERRNO=-1;
public const int Z_STREAM_ERROR=-2;
public const int Z_DATA_ERROR=-3;
public const int Z_MEM_ERROR=-4;
public const int Z_BUF_ERROR=-5;
public const int Z_VERSION_ERROR=-6;
}
}

View File

@@ -0,0 +1,152 @@
using System;
/*
* $Id: StaticTree.cs,v 1.1 2006/06/16 10:56:20 psoares33 Exp $
*
Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This program is based on zlib-1.1.3, so all credit should go authors
* Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
* and contributors of zlib.
*/
namespace System.util.zlib {
internal sealed class StaticTree{
private const int MAX_BITS=15;
private const int BL_CODES=19;
private const int D_CODES=30;
private const int LITERALS=256;
private const int LENGTH_CODES=29;
private const int L_CODES=(LITERALS+1+LENGTH_CODES);
// Bit length codes must not exceed MAX_BL_BITS bits
internal const int MAX_BL_BITS=7;
internal static short[] static_ltree = {
12, 8, 140, 8, 76, 8, 204, 8, 44, 8,
172, 8, 108, 8, 236, 8, 28, 8, 156, 8,
92, 8, 220, 8, 60, 8, 188, 8, 124, 8,
252, 8, 2, 8, 130, 8, 66, 8, 194, 8,
34, 8, 162, 8, 98, 8, 226, 8, 18, 8,
146, 8, 82, 8, 210, 8, 50, 8, 178, 8,
114, 8, 242, 8, 10, 8, 138, 8, 74, 8,
202, 8, 42, 8, 170, 8, 106, 8, 234, 8,
26, 8, 154, 8, 90, 8, 218, 8, 58, 8,
186, 8, 122, 8, 250, 8, 6, 8, 134, 8,
70, 8, 198, 8, 38, 8, 166, 8, 102, 8,
230, 8, 22, 8, 150, 8, 86, 8, 214, 8,
54, 8, 182, 8, 118, 8, 246, 8, 14, 8,
142, 8, 78, 8, 206, 8, 46, 8, 174, 8,
110, 8, 238, 8, 30, 8, 158, 8, 94, 8,
222, 8, 62, 8, 190, 8, 126, 8, 254, 8,
1, 8, 129, 8, 65, 8, 193, 8, 33, 8,
161, 8, 97, 8, 225, 8, 17, 8, 145, 8,
81, 8, 209, 8, 49, 8, 177, 8, 113, 8,
241, 8, 9, 8, 137, 8, 73, 8, 201, 8,
41, 8, 169, 8, 105, 8, 233, 8, 25, 8,
153, 8, 89, 8, 217, 8, 57, 8, 185, 8,
121, 8, 249, 8, 5, 8, 133, 8, 69, 8,
197, 8, 37, 8, 165, 8, 101, 8, 229, 8,
21, 8, 149, 8, 85, 8, 213, 8, 53, 8,
181, 8, 117, 8, 245, 8, 13, 8, 141, 8,
77, 8, 205, 8, 45, 8, 173, 8, 109, 8,
237, 8, 29, 8, 157, 8, 93, 8, 221, 8,
61, 8, 189, 8, 125, 8, 253, 8, 19, 9,
275, 9, 147, 9, 403, 9, 83, 9, 339, 9,
211, 9, 467, 9, 51, 9, 307, 9, 179, 9,
435, 9, 115, 9, 371, 9, 243, 9, 499, 9,
11, 9, 267, 9, 139, 9, 395, 9, 75, 9,
331, 9, 203, 9, 459, 9, 43, 9, 299, 9,
171, 9, 427, 9, 107, 9, 363, 9, 235, 9,
491, 9, 27, 9, 283, 9, 155, 9, 411, 9,
91, 9, 347, 9, 219, 9, 475, 9, 59, 9,
315, 9, 187, 9, 443, 9, 123, 9, 379, 9,
251, 9, 507, 9, 7, 9, 263, 9, 135, 9,
391, 9, 71, 9, 327, 9, 199, 9, 455, 9,
39, 9, 295, 9, 167, 9, 423, 9, 103, 9,
359, 9, 231, 9, 487, 9, 23, 9, 279, 9,
151, 9, 407, 9, 87, 9, 343, 9, 215, 9,
471, 9, 55, 9, 311, 9, 183, 9, 439, 9,
119, 9, 375, 9, 247, 9, 503, 9, 15, 9,
271, 9, 143, 9, 399, 9, 79, 9, 335, 9,
207, 9, 463, 9, 47, 9, 303, 9, 175, 9,
431, 9, 111, 9, 367, 9, 239, 9, 495, 9,
31, 9, 287, 9, 159, 9, 415, 9, 95, 9,
351, 9, 223, 9, 479, 9, 63, 9, 319, 9,
191, 9, 447, 9, 127, 9, 383, 9, 255, 9,
511, 9, 0, 7, 64, 7, 32, 7, 96, 7,
16, 7, 80, 7, 48, 7, 112, 7, 8, 7,
72, 7, 40, 7, 104, 7, 24, 7, 88, 7,
56, 7, 120, 7, 4, 7, 68, 7, 36, 7,
100, 7, 20, 7, 84, 7, 52, 7, 116, 7,
3, 8, 131, 8, 67, 8, 195, 8, 35, 8,
163, 8, 99, 8, 227, 8
};
internal static short[] static_dtree = {
0, 5, 16, 5, 8, 5, 24, 5, 4, 5,
20, 5, 12, 5, 28, 5, 2, 5, 18, 5,
10, 5, 26, 5, 6, 5, 22, 5, 14, 5,
30, 5, 1, 5, 17, 5, 9, 5, 25, 5,
5, 5, 21, 5, 13, 5, 29, 5, 3, 5,
19, 5, 11, 5, 27, 5, 7, 5, 23, 5
};
internal static StaticTree static_l_desc =
new StaticTree(static_ltree, Tree.extra_lbits,
LITERALS+1, L_CODES, MAX_BITS);
internal static StaticTree static_d_desc =
new StaticTree(static_dtree, Tree.extra_dbits,
0, D_CODES, MAX_BITS);
internal static StaticTree static_bl_desc =
new StaticTree(null, Tree.extra_blbits,
0, BL_CODES, MAX_BL_BITS);
internal short[] static_tree; // static tree or null
internal int[] extra_bits; // extra bits for each code or null
internal int extra_base; // base index for extra_bits
internal int elems; // max number of elements in the tree
internal int max_length; // max bit length for the codes
internal StaticTree(short[] static_tree,
int[] extra_bits,
int extra_base,
int elems,
int max_length
){
this.static_tree=static_tree;
this.extra_bits=extra_bits;
this.extra_base=extra_base;
this.elems=elems;
this.max_length=max_length;
}
}
}

View File

@@ -0,0 +1,367 @@
using System;
/*
* $Id: Tree.cs,v 1.2 2006/07/31 14:30:59 psoares33 Exp $
*
Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This program is based on zlib-1.1.3, so all credit should go authors
* Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
* and contributors of zlib.
*/
namespace System.util.zlib {
internal sealed class Tree{
private const int MAX_BITS=15;
private const int BL_CODES=19;
private const int D_CODES=30;
private const int LITERALS=256;
private const int LENGTH_CODES=29;
private const int L_CODES=(LITERALS+1+LENGTH_CODES);
private const int HEAP_SIZE=(2*L_CODES+1);
// Bit length codes must not exceed MAX_BL_BITS bits
internal const int MAX_BL_BITS=7;
// end of block literal code
internal const int END_BLOCK=256;
// repeat previous bit length 3-6 times (2 bits of repeat count)
internal const int REP_3_6=16;
// repeat a zero length 3-10 times (3 bits of repeat count)
internal const int REPZ_3_10=17;
// repeat a zero length 11-138 times (7 bits of repeat count)
internal const int REPZ_11_138=18;
// extra bits for each length code
internal static int[] extra_lbits={
0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0
};
// extra bits for each distance code
internal static int[] extra_dbits={
0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13
};
// extra bits for each bit length code
internal static int[] extra_blbits={
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7
};
internal static byte[] bl_order={
16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
// The lengths of the bit length codes are sent in order of decreasing
// probability, to avoid transmitting the lengths for unused bit
// length codes.
internal const int Buf_size=8*2;
// see definition of array dist_code below
internal const int DIST_CODE_LEN=512;
internal static byte[] _dist_code = {
0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
};
internal static byte[] _length_code={
0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
};
internal static int[] base_length = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
64, 80, 96, 112, 128, 160, 192, 224, 0
};
internal static int[] base_dist = {
0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
};
// Mapping from a distance to a distance code. dist is the distance - 1 and
// must not have side effects. _dist_code[256] and _dist_code[257] are never
// used.
internal static int d_code(int dist){
return ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)]);
}
internal short[] dyn_tree; // the dynamic tree
internal int max_code; // largest code with non zero frequency
internal StaticTree stat_desc; // the corresponding static tree
// Compute the optimal bit lengths for a tree and update the total bit length
// for the current block.
// IN assertion: the fields freq and dad are set, heap[heap_max] and
// above are the tree nodes sorted by increasing frequency.
// OUT assertions: the field len is set to the optimal bit length, the
// array bl_count contains the frequencies for each bit length.
// The length opt_len is updated; static_len is also updated if stree is
// not null.
internal void gen_bitlen(Deflate s){
short[] tree = dyn_tree;
short[] stree = stat_desc.static_tree;
int[] extra = stat_desc.extra_bits;
int based = stat_desc.extra_base;
int max_length = stat_desc.max_length;
int h; // heap index
int n, m; // iterate over the tree elements
int bits; // bit length
int xbits; // extra bits
short f; // frequency
int overflow = 0; // number of elements with bit length too large
for (bits = 0; bits <= MAX_BITS; bits++) s.bl_count[bits] = 0;
// In a first pass, compute the optimal bit lengths (which may
// overflow in the case of the bit length tree).
tree[s.heap[s.heap_max]*2+1] = 0; // root of the heap
for(h=s.heap_max+1; h<HEAP_SIZE; h++){
n = s.heap[h];
bits = tree[tree[n*2+1]*2+1] + 1;
if (bits > max_length){ bits = max_length; overflow++; }
tree[n*2+1] = (short)bits;
// We overwrite tree[n*2+1] which is no longer needed
if (n > max_code) continue; // not a leaf node
s.bl_count[bits]++;
xbits = 0;
if (n >= based) xbits = extra[n-based];
f = tree[n*2];
s.opt_len += f * (bits + xbits);
if (stree!=null) s.static_len += f * (stree[n*2+1] + xbits);
}
if (overflow == 0) return;
// This happens for example on obj2 and pic of the Calgary corpus
// Find the first bit length which could increase:
do {
bits = max_length-1;
while(s.bl_count[bits]==0) bits--;
s.bl_count[bits]--; // move one leaf down the tree
s.bl_count[bits+1]+=2; // move one overflow item as its brother
s.bl_count[max_length]--;
// The brother of the overflow item also moves one step up,
// but this does not affect bl_count[max_length]
overflow -= 2;
}
while (overflow > 0);
for (bits = max_length; bits != 0; bits--) {
n = s.bl_count[bits];
while (n != 0) {
m = s.heap[--h];
if (m > max_code) continue;
if (tree[m*2+1] != bits) {
s.opt_len += (int)(((long)bits - (long)tree[m*2+1])*(long)tree[m*2]);
tree[m*2+1] = (short)bits;
}
n--;
}
}
}
// Construct one Huffman tree and assigns the code bit strings and lengths.
// Update the total bit length for the current block.
// IN assertion: the field freq is set for all tree elements.
// OUT assertions: the fields len and code are set to the optimal bit length
// and corresponding code. The length opt_len is updated; static_len is
// also updated if stree is not null. The field max_code is set.
internal void build_tree(Deflate s){
short[] tree=dyn_tree;
short[] stree=stat_desc.static_tree;
int elems=stat_desc.elems;
int n, m; // iterate over heap elements
int max_code=-1; // largest code with non zero frequency
int node; // new node being created
// Construct the initial heap, with least frequent element in
// heap[1]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
// heap[0] is not used.
s.heap_len = 0;
s.heap_max = HEAP_SIZE;
for(n=0; n<elems; n++) {
if(tree[n*2] != 0) {
s.heap[++s.heap_len] = max_code = n;
s.depth[n] = 0;
}
else{
tree[n*2+1] = 0;
}
}
// The pkzip format requires that at least one distance code exists,
// and that at least one bit should be sent even if there is only one
// possible code. So to avoid special checks later on we force at least
// two codes of non zero frequency.
while (s.heap_len < 2) {
node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0);
tree[node*2] = 1;
s.depth[node] = 0;
s.opt_len--; if (stree!=null) s.static_len -= stree[node*2+1];
// node is 0 or 1 so it does not have extra bits
}
this.max_code = max_code;
// The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
// establish sub-heaps of increasing lengths:
for(n=s.heap_len/2;n>=1; n--)
s.pqdownheap(tree, n);
// Construct the Huffman tree by repeatedly combining the least two
// frequent nodes.
node=elems; // next internal node of the tree
do{
// n = node of least frequency
n=s.heap[1];
s.heap[1]=s.heap[s.heap_len--];
s.pqdownheap(tree, 1);
m=s.heap[1]; // m = node of next least frequency
s.heap[--s.heap_max] = n; // keep the nodes sorted by frequency
s.heap[--s.heap_max] = m;
// Create a new node father of n and m
tree[node*2] = (short)(tree[n*2] + tree[m*2]);
s.depth[node] = (byte)(System.Math.Max(s.depth[n],s.depth[m])+1);
tree[n*2+1] = tree[m*2+1] = (short)node;
// and insert the new node in the heap
s.heap[1] = node++;
s.pqdownheap(tree, 1);
}
while(s.heap_len>=2);
s.heap[--s.heap_max] = s.heap[1];
// At this point, the fields freq and dad are set. We can now
// generate the bit lengths.
gen_bitlen(s);
// The field len is now set, we can generate the bit codes
gen_codes(tree, max_code, s.bl_count);
}
// Generate the codes for a given tree and bit counts (which need not be
// optimal).
// IN assertion: the array bl_count contains the bit length statistics for
// the given tree and the field len is set for all tree elements.
// OUT assertion: the field code is set for all tree elements of non
// zero code length.
internal static void gen_codes(short[] tree, // the tree to decorate
int max_code, // largest code with non zero frequency
short[] bl_count // number of codes at each bit length
){
short[] next_code=new short[MAX_BITS+1]; // next code value for each bit length
short code = 0; // running code value
int bits; // bit index
int n; // code index
// The distribution counts are first used to generate the code values
// without bit reversal.
for (bits = 1; bits <= MAX_BITS; bits++) {
next_code[bits] = code = (short)((code + bl_count[bits-1]) << 1);
}
// Check that the bit counts in bl_count are consistent. The last code
// must be all ones.
//Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
// "inconsistent bit counts");
//Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
for (n = 0; n <= max_code; n++) {
int len = tree[n*2+1];
if (len == 0) continue;
// Now reverse the bits
tree[n*2] = (short)(bi_reverse(next_code[len]++, len));
}
}
// Reverse the first len bits of a code, using straightforward code (a faster
// method would use a table)
// IN assertion: 1 <= len <= 15
internal static int bi_reverse(int code, // the value to invert
int len // its bit length
){
int res = 0;
do{
res|=code&1;
code>>=1;
res<<=1;
}
while(--len>0);
return res>>1;
}
}
}

View File

@@ -0,0 +1,196 @@
using System;
using System.IO;
/*
* $Id: ZDeflaterOutputStream.cs,v 1.3 2007/06/22 14:14:13 psoares33 Exp $
*
* Copyright 2006 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License isp distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code isp 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code isp Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code isp Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library isp free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library isp 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 Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace System.util.zlib {
/// <summary>
/// Summary description for DeflaterOutputStream.
/// </summary>
public class ZDeflaterOutputStream : Stream {
protected ZStream z=new ZStream();
protected int flushLevel=JZlib.Z_NO_FLUSH;
private const int BUFSIZE = 4192;
protected byte[] buf=new byte[BUFSIZE];
private byte[] buf1=new byte[1];
protected Stream outp;
public ZDeflaterOutputStream(Stream outp) : this(outp, 6, false) {
}
public ZDeflaterOutputStream(Stream outp, int level) : this(outp, level, false) {
}
public ZDeflaterOutputStream(Stream outp, int level, bool nowrap) {
this.outp=outp;
z.deflateInit(level, nowrap);
}
public override bool CanRead {
get {
// TODO: Add DeflaterOutputStream.CanRead getter implementation
return false;
}
}
public override bool CanSeek {
get {
// TODO: Add DeflaterOutputStream.CanSeek getter implementation
return false;
}
}
public override bool CanWrite {
get {
// TODO: Add DeflaterOutputStream.CanWrite getter implementation
return true;
}
}
public override long Length {
get {
// TODO: Add DeflaterOutputStream.Length getter implementation
return 0;
}
}
public override long Position {
get {
// TODO: Add DeflaterOutputStream.Position getter implementation
return 0;
}
set {
// TODO: Add DeflaterOutputStream.Position setter implementation
}
}
public override void Write(byte[] b, int off, int len) {
if(len==0)
return;
int err;
z.next_in=b;
z.next_in_index=off;
z.avail_in=len;
do{
z.next_out=buf;
z.next_out_index=0;
z.avail_out=BUFSIZE;
err=z.deflate(flushLevel);
if(err!=JZlib.Z_OK)
throw new IOException("deflating: "+z.msg);
if (z.avail_out < BUFSIZE)
outp.Write(buf, 0, BUFSIZE-z.avail_out);
}
while(z.avail_in>0 || z.avail_out==0);
}
public override long Seek(long offset, SeekOrigin origin) {
// TODO: Add DeflaterOutputStream.Seek implementation
return 0;
}
public override void SetLength(long value) {
// TODO: Add DeflaterOutputStream.SetLength implementation
}
public override int Read(byte[] buffer, int offset, int count) {
// TODO: Add DeflaterOutputStream.Read implementation
return 0;
}
public override void Flush() {
outp.Flush();
}
public override void WriteByte(byte b) {
buf1[0]=(byte)b;
Write(buf1, 0, 1);
}
public void Finish() {
int err;
do{
z.next_out=buf;
z.next_out_index=0;
z.avail_out=BUFSIZE;
err=z.deflate(JZlib.Z_FINISH);
if(err!=JZlib.Z_STREAM_END && err != JZlib.Z_OK)
throw new IOException("deflating: "+z.msg);
if(BUFSIZE-z.avail_out>0){
outp.Write(buf, 0, BUFSIZE-z.avail_out);
}
}
while(z.avail_in>0 || z.avail_out==0);
Flush();
}
public void End() {
if(z==null)
return;
z.deflateEnd();
z.free();
z=null;
}
public override void Close() {
try{
try{Finish();}
catch (IOException) {}
}
finally{
End();
outp.Close();
outp=null;
}
}
}
}

View File

@@ -0,0 +1,174 @@
using System;
using System.IO;
/*
* $Id: ZInflaterInputStream.cs,v 1.4 2007/06/22 14:14:16 psoares33 Exp $
*
* Copyright 2006 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License isp distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code isp 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code isp Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code isp Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library isp free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library isp 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 Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace System.util.zlib {
/// <summary>
/// Summary description for DeflaterOutputStream.
/// </summary>
public class ZInflaterInputStream : Stream {
protected ZStream z=new ZStream();
protected int flushLevel=JZlib.Z_NO_FLUSH;
private const int BUFSIZE = 4192;
protected byte[] buf=new byte[BUFSIZE];
private byte[] buf1=new byte[1];
protected Stream inp=null;
private bool nomoreinput=false;
public ZInflaterInputStream(Stream inp) : this(inp, false) {
}
public ZInflaterInputStream(Stream inp, bool nowrap) {
this.inp=inp;
z.inflateInit(nowrap);
z.next_in=buf;
z.next_in_index=0;
z.avail_in=0;
}
public override bool CanRead {
get {
// TODO: Add DeflaterOutputStream.CanRead getter implementation
return true;
}
}
public override bool CanSeek {
get {
// TODO: Add DeflaterOutputStream.CanSeek getter implementation
return false;
}
}
public override bool CanWrite {
get {
// TODO: Add DeflaterOutputStream.CanWrite getter implementation
return false;
}
}
public override long Length {
get {
// TODO: Add DeflaterOutputStream.Length getter implementation
return 0;
}
}
public override long Position {
get {
// TODO: Add DeflaterOutputStream.Position getter implementation
return 0;
}
set {
// TODO: Add DeflaterOutputStream.Position setter implementation
}
}
public override void Write(byte[] b, int off, int len) {
}
public override long Seek(long offset, SeekOrigin origin) {
// TODO: Add DeflaterOutputStream.Seek implementation
return 0;
}
public override void SetLength(long value) {
// TODO: Add DeflaterOutputStream.SetLength implementation
}
public override int Read(byte[] b, int off, int len) {
if(len==0)
return(0);
int err;
z.next_out=b;
z.next_out_index=off;
z.avail_out=len;
do {
if((z.avail_in==0)&&(!nomoreinput)) { // if buffer is empty and more input is avaiable, refill it
z.next_in_index=0;
z.avail_in=inp.Read(buf, 0, BUFSIZE);//(BUFSIZE<z.avail_out ? BUFSIZE : z.avail_out));
if(z.avail_in==0) {
z.avail_in=0;
nomoreinput=true;
}
}
err=z.inflate(flushLevel);
if(nomoreinput&&(err==JZlib.Z_BUF_ERROR))
return(-1);
if(err!=JZlib.Z_OK && err!=JZlib.Z_STREAM_END)
throw new IOException("inflating: "+z.msg);
if((nomoreinput||err==JZlib.Z_STREAM_END)&&(z.avail_out==len))
return(0);
}
while(z.avail_out==len&&err==JZlib.Z_OK);
//System.err.print("("+(len-z.avail_out)+")");
return(len-z.avail_out);
}
public override void Flush() {
inp.Flush();
}
public override void WriteByte(byte b) {
}
public override void Close() {
inp.Close();
}
public override int ReadByte() {
if(Read(buf1, 0, 1)<=0)
return -1;
return(buf1[0]&0xFF);
}
}
}

View File

@@ -0,0 +1,214 @@
using System;
/*
* $Id: ZStream.cs,v 1.1 2006/06/16 10:56:20 psoares33 Exp $
*
Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This program is based on zlib-1.1.3, so all credit should go authors
* Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
* and contributors of zlib.
*/
namespace System.util.zlib {
public sealed class ZStream{
private const int MAX_WBITS=15; // 32K LZ77 window
private const int DEF_WBITS=MAX_WBITS;
private const int Z_NO_FLUSH=0;
private const int Z_PARTIAL_FLUSH=1;
private const int Z_SYNC_FLUSH=2;
private const int Z_FULL_FLUSH=3;
private const int Z_FINISH=4;
private const int MAX_MEM_LEVEL=9;
private const int Z_OK=0;
private const int Z_STREAM_END=1;
private const int Z_NEED_DICT=2;
private const int Z_ERRNO=-1;
private const int Z_STREAM_ERROR=-2;
private const int Z_DATA_ERROR=-3;
private const int Z_MEM_ERROR=-4;
private const int Z_BUF_ERROR=-5;
private const int Z_VERSION_ERROR=-6;
public byte[] next_in; // next input byte
public int next_in_index;
public int avail_in; // number of bytes available at next_in
public long total_in; // total nb of input bytes read so far
public byte[] next_out; // next output byte should be put there
public int next_out_index;
public int avail_out; // remaining free space at next_out
public long total_out; // total nb of bytes output so far
public String msg;
internal Deflate dstate;
internal Inflate istate;
internal int data_type; // best guess about the data type: ascii or binary
public long adler;
internal Adler32 _adler=new Adler32();
public int inflateInit(){
return inflateInit(DEF_WBITS);
}
public int inflateInit(bool nowrap){
return inflateInit(DEF_WBITS, nowrap);
}
public int inflateInit(int w){
return inflateInit(w, false);
}
public int inflateInit(int w, bool nowrap){
istate=new Inflate();
return istate.inflateInit(this, nowrap?-w:w);
}
public int inflate(int f){
if(istate==null) return Z_STREAM_ERROR;
return istate.inflate(this, f);
}
public int inflateEnd(){
if(istate==null) return Z_STREAM_ERROR;
int ret=istate.inflateEnd(this);
istate = null;
return ret;
}
public int inflateSync(){
if(istate == null)
return Z_STREAM_ERROR;
return istate.inflateSync(this);
}
public int inflateSetDictionary(byte[] dictionary, int dictLength){
if(istate == null)
return Z_STREAM_ERROR;
return istate.inflateSetDictionary(this, dictionary, dictLength);
}
public int deflateInit(int level){
return deflateInit(level, MAX_WBITS);
}
public int deflateInit(int level, bool nowrap){
return deflateInit(level, MAX_WBITS, nowrap);
}
public int deflateInit(int level, int bits){
return deflateInit(level, bits, false);
}
public int deflateInit(int level, int bits, bool nowrap){
dstate=new Deflate();
return dstate.deflateInit(this, level, nowrap?-bits:bits);
}
public int deflate(int flush){
if(dstate==null){
return Z_STREAM_ERROR;
}
return dstate.deflate(this, flush);
}
public int deflateEnd(){
if(dstate==null) return Z_STREAM_ERROR;
int ret=dstate.deflateEnd();
dstate=null;
return ret;
}
public int deflateParams(int level, int strategy){
if(dstate==null) return Z_STREAM_ERROR;
return dstate.deflateParams(this, level, strategy);
}
public int deflateSetDictionary (byte[] dictionary, int dictLength){
if(dstate == null)
return Z_STREAM_ERROR;
return dstate.deflateSetDictionary(this, dictionary, dictLength);
}
// Flush as much pending output as possible. All deflate() output goes
// through this function so some applications may wish to modify it
// to avoid allocating a large strm->next_out buffer and copying into it.
// (See also read_buf()).
internal void flush_pending(){
int len=dstate.pending;
if(len>avail_out) len=avail_out;
if(len==0) return;
if(dstate.pending_buf.Length<=dstate.pending_out ||
next_out.Length<=next_out_index ||
dstate.pending_buf.Length<(dstate.pending_out+len) ||
next_out.Length<(next_out_index+len)){
// System.out.println(dstate.pending_buf.length+", "+dstate.pending_out+
// ", "+next_out.length+", "+next_out_index+", "+len);
// System.out.println("avail_out="+avail_out);
}
System.Array.Copy(dstate.pending_buf, dstate.pending_out,
next_out, next_out_index, len);
next_out_index+=len;
dstate.pending_out+=len;
total_out+=len;
avail_out-=len;
dstate.pending-=len;
if(dstate.pending==0){
dstate.pending_out=0;
}
}
// Read a new buffer from the current input stream, update the adler32
// and total number of bytes read. All deflate() input goes through
// this function so some applications may wish to modify it to avoid
// allocating a large strm->next_in buffer and copying from it.
// (See also flush_pending()).
internal int read_buf(byte[] buf, int start, int size) {
int len=avail_in;
if(len>size) len=size;
if(len==0) return 0;
avail_in-=len;
if(dstate.noheader==0) {
adler=_adler.adler32(adler, next_in, next_in_index, len);
}
System.Array.Copy(next_in, next_in_index, buf, start, len);
next_in_index += len;
total_in += len;
return len;
}
public void free(){
next_in=null;
next_out=null;
msg=null;
_adler=null;
}
}
}