antlr/Antlr4.Runtime/Dfa/ArrayEdgeMap`1.cs

273 lines
8.8 KiB
C#
Raw Normal View History

2013-02-16 05:30:47 +08:00
/*
* [The "BSD license"]
* Copyright (c) 2013 Terence Parr
* Copyright (c) 2013 Sam Harwell
* 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 name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 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 THE AUTHOR 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.
*/
using System;
using System.Collections.Generic;
using Antlr4.Runtime.Dfa;
using Sharpen;
namespace Antlr4.Runtime.Dfa
{
2013-02-16 22:14:20 +08:00
/// <author>sam</author>
public class ArrayEdgeMap<T> : AbstractEdgeMap<T>
{
private readonly T[] arrayData;
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
private int size;
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
public ArrayEdgeMap(int minIndex, int maxIndex) : base(minIndex, maxIndex)
{
arrayData = (T[])new object[maxIndex - minIndex + 1];
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
public override int Size()
{
return size;
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
public override bool IsEmpty()
{
return size == 0;
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
public override bool ContainsKey(int key)
{
return Get(key) != null;
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
public override T Get(int key)
{
if (key < minIndex || key > maxIndex)
{
return null;
}
return arrayData[key - minIndex];
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
public override AbstractEdgeMap<T> Put(int key, T value)
{
if (key >= minIndex && key <= maxIndex)
{
T existing = arrayData[key - minIndex];
arrayData[key - minIndex] = value;
if (existing == null && value != null)
{
size++;
}
else
{
if (existing != null && value == null)
{
size--;
}
}
}
return this;
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
public override AbstractEdgeMap<T> Remove(int key)
{
return ((Antlr4.Runtime.Dfa.ArrayEdgeMap<T>)Put(key, null));
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
public override AbstractEdgeMap<T> PutAll<_T0>(IEdgeMap<_T0> m)
{
if (m.IsEmpty())
{
return this;
}
if (m is Antlr4.Runtime.Dfa.ArrayEdgeMap<object>)
{
Antlr4.Runtime.Dfa.ArrayEdgeMap<T> other = (Antlr4.Runtime.Dfa.ArrayEdgeMap<T>)m;
int minOverlap = Math.Max(minIndex, other.minIndex);
int maxOverlap = Math.Min(maxIndex, other.maxIndex);
for (int i = minOverlap; i <= maxOverlap; i++)
{
T target = other.arrayData[i - other.minIndex];
if (target != null)
{
T current = this.arrayData[i - this.minIndex];
this.arrayData[i - this.minIndex] = target;
size += (current != null ? 0 : 1);
}
}
return this;
}
else
{
if (m is SingletonEdgeMap<object>)
{
SingletonEdgeMap<T> other = (SingletonEdgeMap<T>)m;
System.Diagnostics.Debug.Assert(!other.IsEmpty());
return ((Antlr4.Runtime.Dfa.ArrayEdgeMap<T>)Put(other.GetKey(), other.GetValue())
);
}
else
{
if (m is SparseEdgeMap<object>)
{
SparseEdgeMap<T> other = (SparseEdgeMap<T>)m;
int[] keys = other.GetKeys();
IList<T> values = other.GetValues();
Antlr4.Runtime.Dfa.ArrayEdgeMap<T> result = this;
for (int i = 0; i < values.Count; i++)
{
result = ((Antlr4.Runtime.Dfa.ArrayEdgeMap<T>)result.Put(keys[i], values[i]));
}
return result;
}
else
{
throw new NotSupportedException(string.Format("EdgeMap of type %s is supported yet."
, m.GetType().FullName));
}
}
}
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
public override AbstractEdgeMap<T> Clear()
{
Arrays.Fill(arrayData, null);
return this;
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
public override IDictionary<int, T> ToMap()
{
if (IsEmpty())
{
return Sharpen.Collections.EmptyMap();
}
IDictionary<int, T> result = new LinkedHashMap<int, T>();
for (int i = 0; i < arrayData.Length; i++)
{
if (arrayData[i] == null)
{
continue;
}
result.Put(i + minIndex, arrayData[i]);
}
return result;
}
2013-02-16 05:30:47 +08:00
2013-02-17 04:34:47 +08:00
public override ISet<KeyValuePair<int, T>> EntrySet()
2013-02-16 22:14:20 +08:00
{
return new ArrayEdgeMap.EntrySet(this);
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
private class EntrySet : AbstractEdgeMap.AbstractEntrySet
{
public override IEnumerator<KeyValuePair<int, T>> GetEnumerator()
{
return new ArrayEdgeMap.EntryIterator(this);
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
internal EntrySet(ArrayEdgeMap<T> _enclosing) : base(_enclosing)
{
this._enclosing = _enclosing;
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
private readonly ArrayEdgeMap<T> _enclosing;
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
private class EntryIterator : IEnumerator<KeyValuePair<int, T>>
{
private int current;
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
private int currentIndex;
2013-02-16 05:30:47 +08:00
public virtual bool HasNext()
2013-02-16 22:14:20 +08:00
{
return this.current < this._enclosing.Size();
}
2013-02-16 05:30:47 +08:00
public virtual KeyValuePair<int, T> Next()
2013-02-16 22:14:20 +08:00
{
if (this.current >= this._enclosing.Size())
{
throw new InvalidOperationException();
2013-02-16 22:14:20 +08:00
}
while (this._enclosing.arrayData[this.currentIndex] == null)
{
this.currentIndex++;
}
this.current++;
this.currentIndex++;
return new _KeyValuePair_193(this);
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
private sealed class _KeyValuePair_193 : KeyValuePair<int, T>
{
public _KeyValuePair_193()
{
this.key = this._enclosing._enclosing.minIndex + this._enclosing.currentIndex - 1;
this.value = this._enclosing._enclosing.arrayData[this._enclosing.currentIndex -
1];
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
private readonly int key;
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
private readonly T value;
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
public int Key
{
get
{
return this.key;
}
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
public T Value
{
get
{
return this.value;
}
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
public T SetValue(T value)
{
throw new NotSupportedException("Not supported yet.");
}
}
2013-02-16 05:30:47 +08:00
public virtual void Remove()
2013-02-16 22:14:20 +08:00
{
throw new NotSupportedException("Not supported yet.");
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
internal EntryIterator(ArrayEdgeMap<T> _enclosing)
{
this._enclosing = _enclosing;
}
2013-02-16 05:30:47 +08:00
2013-02-16 22:14:20 +08:00
private readonly ArrayEdgeMap<T> _enclosing;
}
}
2013-02-16 05:30:47 +08:00
}