예제 #1
0
 /// <summary>
 /// Creates a new file-backed CacheMap or loads it in from the specified file
 /// if it already exists.
 /// </summary>
 /// <remarks>
 /// Creates a new file-backed CacheMap or loads it in from the specified file
 /// if it already exists.  The parameters passed in are the same as the
 /// constructor.  If useFileParams is true and the file exists, all of your
 /// parameters will be ignored (replaced with those stored in the file
 /// itself).  If useFileParams is false then we override the settings in the
 /// file with the ones you specify (except loadFactor and accessOrder) and
 /// reset the stats.
 /// </remarks>
 public static Edu.Stanford.Nlp.Util.CacheMap <K, V> Create <K, V>(int numEntries, float loadFactor, bool accessOrder, string file, bool useFileParams)
 {
     try
     {
         using (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)))
         {
             Edu.Stanford.Nlp.Util.CacheMap <K, V> c = ErasureUtils.UncheckedCast(ois.ReadObject());
             log.Info("Read cache from " + file + ", contains " + c.Count + " entries.  Backing file is " + c.backingFile);
             if (!useFileParams)
             {
                 c.backingFile  = file;
                 c.hits         = c.misses = c.puts = 0;
                 c.CacheEntries = numEntries;
             }
             return(c);
         }
     }
     catch (FileNotFoundException)
     {
         log.Info("Cache file " + file + " has not been created yet.  Making new one.");
         return(new Edu.Stanford.Nlp.Util.CacheMap <K, V>(numEntries, loadFactor, accessOrder, file));
     }
     catch (Exception)
     {
         log.Info("Error reading cache file " + file + ".  Making a new cache and NOT backing to file.");
         return(new Edu.Stanford.Nlp.Util.CacheMap <K, V>(numEntries, loadFactor, accessOrder));
     }
 }
예제 #2
0
 public override bool Equals(object o)
 {
     if (this == o)
     {
         return(true);
     }
     if (!(o is Edu.Stanford.Nlp.Util.Quintuple))
     {
         return(false);
     }
     Edu.Stanford.Nlp.Util.Quintuple <T1, T2, T3, T4, T5> quadruple = ErasureUtils.UncheckedCast(o);
     if (first != null ? !first.Equals(quadruple.first) : quadruple.first != null)
     {
         return(false);
     }
     if (second != null ? !second.Equals(quadruple.second) : quadruple.second != null)
     {
         return(false);
     }
     if (third != null ? !third.Equals(quadruple.third) : quadruple.third != null)
     {
         return(false);
     }
     if (fourth != null ? !fourth.Equals(quadruple.fourth) : quadruple.fourth != null)
     {
         return(false);
     }
     if (fifth != null ? !fifth.Equals(quadruple.fifth) : quadruple.fifth != null)
     {
         return(false);
     }
     return(true);
 }
        public override ICollection <V> Remove(object key)
        {
            ICollection <V> result = this[key];

            deltaMap[ErasureUtils.UncheckedCast(key)] = ErasureUtils.UncheckedCast(removedValue);
            return(result);
        }
 // Bulk Operations
 /// <summary>This is more expensive than normal.</summary>
 public override void Clear()
 {
     // iterate over all keys in originalMap and set them to null in deltaMap
     foreach (K key in originalMap.Keys)
     {
         deltaMap[key] = ErasureUtils.UncheckedCast(removedValue);
     }
 }
예제 #5
0
 /// <summary>
 /// Initializes this ArrayCoreMap, pre-allocating arrays to hold
 /// up to capacity key,value pairs.
 /// </summary>
 /// <remarks>
 /// Initializes this ArrayCoreMap, pre-allocating arrays to hold
 /// up to capacity key,value pairs.  This array will grow if necessary.
 /// </remarks>
 /// <param name="capacity">Initial capacity of object in key,value pairs</param>
 public ArrayCoreMap(int capacity)
 {
     /*, Serializable */
     // = null;
     // = 0;
     keys   = ErasureUtils.UncheckedCast(new Type[capacity]);
     values = new object[capacity];
 }
            public override bool Equals(object o)
            {
                if (!(o is DictionaryEntry))
                {
                    return(false);
                }
                DictionaryEntry e = ErasureUtils.UncheckedCast(o);

                return(Eq(key, e.Key) && Eq(value, e.Value));
            }
예제 #7
0
 /* Maps */
 public static IDictionary <K, V> NewHashMap <K, V>()
 {
     try
     {
         return(ErasureUtils.UncheckedCast(System.Activator.CreateInstance(HashMapClass)));
     }
     catch (Exception e)
     {
         throw new Exception(e);
     }
 }
예제 #8
0
 public static ICollection <E> NewHashSet <E>()
 {
     try
     {
         return(ErasureUtils.UncheckedCast(System.Activator.CreateInstance(HashSetClass)));
     }
     catch (Exception e)
     {
         throw new Exception(e);
     }
 }
예제 #9
0
 public static ICollection <E> NewHashSet <E, _T1>(ICollection <_T1> c)
     where _T1 : E
 {
     try
     {
         return(ErasureUtils.UncheckedCast(HashSetCollectionConstructor.NewInstance(c)));
     }
     catch (Exception e)
     {
         throw new Exception(e);
     }
 }
예제 #10
0
 public static IDictionary <K, V> NewHashMap <K, V, _T2>(IDictionary <_T2> m)
     where _T2 : K
 {
     try
     {
         return(ErasureUtils.UncheckedCast(HashMapFromMapConstructor.NewInstance(m)));
     }
     catch (Exception e)
     {
         throw new Exception(e);
     }
 }
예제 #11
0
        /// <summary>Adds the value to the collection given by map.get(key).</summary>
        /// <remarks>Adds the value to the collection given by map.get(key).  A new collection is created using the supplied CollectionFactory.</remarks>
        public static void PutIntoValueCollection <K, V, C>(IDictionary <K, C> map, K key, V value, CollectionFactory <V> cf)
            where C : ICollection <V>
        {
            C c = map[key];

            if (c == null)
            {
                c        = ErasureUtils.UncheckedCast <C>(cf.NewCollection());
                map[key] = c;
            }
            c.Add(value);
        }
예제 #12
0
 /// <summary>
 /// Reduces memory consumption to the minimum for representing the values
 /// currently stored stored in this object.
 /// </summary>
 public virtual void Compact()
 {
     if (keys.Length > size)
     {
         Type[]   newKeys   = new Type[size];
         object[] newValues = new object[size];
         System.Array.Copy(keys, 0, newKeys, 0, size);
         System.Array.Copy(values, 0, newValues, 0, size);
         keys   = ErasureUtils.UncheckedCast(newKeys);
         values = newValues;
     }
 }
예제 #13
0
 /// <summary>Returns a clone of this priority queue.</summary>
 /// <remarks>
 /// Returns a clone of this priority queue.  Modifications to one will not
 /// affect modifications to the other.
 /// </remarks>
 public Edu.Stanford.Nlp.Util.FixedPrioritiesPriorityQueue <E> Clone()
 {
     Edu.Stanford.Nlp.Util.FixedPrioritiesPriorityQueue <E> clonePQ;
     clonePQ            = ErasureUtils.UncheckedCast(base.MemberwiseClone());
     clonePQ.elements   = new List <E>(capacity);
     clonePQ.priorities = new double[capacity];
     if (Count > 0)
     {
         Sharpen.Collections.AddAll(clonePQ.elements, elements);
         System.Array.Copy(priorities, 0, clonePQ.priorities, 0, Count);
     }
     return(clonePQ);
 }
예제 #14
0
 public virtual void SetCapacity(int newSize)
 {
     if (size > newSize)
     {
         throw new Exception("You cannot set capacity to smaller than the current size.");
     }
     Type[]   newKeys   = new Type[newSize];
     object[] newValues = new object[newSize];
     System.Array.Copy(keys, 0, newKeys, 0, size);
     System.Array.Copy(values, 0, newValues, 0, size);
     keys   = ErasureUtils.UncheckedCast(newKeys);
     values = newValues;
 }
        /// <summary>Returns the current set of unique labels, sorted by their string order.</summary>
        private IList <U> SortKeys()
        {
            ICollection <U> labels = UniqueLabels();

            if (labels.Count == 0)
            {
                return(Java.Util.Collections.EmptyList());
            }
            bool comparable = true;

            foreach (U label in labels)
            {
                if (!(label is IComparable))
                {
                    comparable = false;
                    break;
                }
            }
            if (comparable)
            {
                IList <IComparable <object> > sorted = Generics.NewArrayList();
                foreach (U label_1 in labels)
                {
                    sorted.Add(ErasureUtils.UncheckedCast <IComparable <object> >(label_1));
                }
                sorted.Sort();
                IList <U> ret = Generics.NewArrayList();
                foreach (object o in sorted)
                {
                    ret.Add(ErasureUtils.UncheckedCast <U>(o));
                }
                return(ret);
            }
            else
            {
                List <string>          names  = new List <string>();
                Dictionary <string, U> lookup = new Dictionary <string, U>();
                foreach (U label_1 in labels)
                {
                    names.Add(label_1.ToString());
                    lookup[label_1.ToString()] = label_1;
                }
                names.Sort();
                List <U> ret = new List <U>();
                foreach (string name in names)
                {
                    ret.Add(lookup[name]);
                }
                return(ret);
            }
        }
예제 #16
0
 public static ICollection <E> NewHashSet <E>(int initialCapacity)
 {
     if (HashSetSizeConstructor == null)
     {
         return(NewHashSet());
     }
     try
     {
         return(ErasureUtils.UncheckedCast(HashSetSizeConstructor.NewInstance(initialCapacity)));
     }
     catch (Exception e)
     {
         throw new Exception(e);
     }
 }
        private int Compare(BinaryHeapPriorityQueue.Entry <E> entryA, BinaryHeapPriorityQueue.Entry <E> entryB)
        {
            int result = Compare(entryA.priority, entryB.priority);

            if (result != 0)
            {
                return(result);
            }
            if ((entryA.key is IComparable) && (entryB.key is IComparable))
            {
                IComparable <E> key = ErasureUtils.UncheckedCast(entryA.key);
                return(key.CompareTo(entryB.key));
            }
            return(result);
        }
예제 #18
0
        private static void RunRelaxingTests(string className)
        {
            BinaryHeapPriorityQueue <string> queue;

            try
            {
                queue = ErasureUtils.UncheckedCast(System.Activator.CreateInstance(Sharpen.Runtime.GetType(className)));
            }
            catch (Exception e)
            {
                Fail(e.ToString());
                return;
            }
            RunRelaxingTests(queue);
        }
예제 #19
0
 public static IDictionary <K, V> NewHashMap <K, V>(int initialCapacity)
 {
     if (HashMapSizeConstructor == null)
     {
         return(NewHashMap());
     }
     try
     {
         return(ErasureUtils.UncheckedCast(HashMapSizeConstructor.NewInstance(initialCapacity)));
     }
     catch (Exception e)
     {
         throw new Exception(e);
     }
 }
        /// <summary>
        /// Deserialize this Object in a manner which is binary-compatible with
        /// the JDK.
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.TypeLoadException"/>
        private void ReadObject(ObjectInputStream s)
        {
            int    size;
            int    expectedMaxSize;
            object o;

            expectedMaxSize = s.ReadInt();
            size            = s.ReadInt();
            map             = new IdentityHashMap <E, bool>(expectedMaxSize);
            for (int i = 0; i < size; i++)
            {
                o = s.ReadObject();
                InternalAdd(ErasureUtils.UncheckedCast <E>(o));
            }
        }
 /// <returns>
 /// true iff o is a CollectionValuedMap, and each key maps to the a
 /// Collection of the same objects in o as it does in this
 /// CollectionValuedMap.
 /// </returns>
 public override bool Equals(object o)
 {
     if (this == o)
     {
         return(true);
     }
     if (!(o is Edu.Stanford.Nlp.Util.CollectionValuedMap <object, object>))
     {
         return(false);
     }
     Edu.Stanford.Nlp.Util.CollectionValuedMap <K, V> other = ErasureUtils.UncheckedCast(o);
     if (other.Count != Count)
     {
         return(false);
     }
     try
     {
         foreach (KeyValuePair <K, ICollection <V> > e in this)
         {
             K key = e.Key;
             ICollection <V> value = e.Value;
             if (value == null)
             {
                 if (!(other[key] == null && other.Contains(key)))
                 {
                     return(false);
                 }
             }
             else
             {
                 if (!value.Equals(other[key]))
                 {
                     return(false);
                 }
             }
         }
     }
     catch (Exception)
     {
         return(false);
     }
     return(true);
 }
예제 #22
0
        private static void RunNotRelaxingTests(string className)
        {
            FixedPrioritiesPriorityQueue <string> pq;

            try
            {
                pq = ErasureUtils.UncheckedCast(System.Activator.CreateInstance(Sharpen.Runtime.GetType(className)));
            }
            catch (Exception e)
            {
                Fail(e.ToString());
                return;
            }
            NUnit.Framework.Assert.AreEqual("[]", pq.ToString());
            pq.Add("one", 1);
            NUnit.Framework.Assert.AreEqual("[one=1.0]", pq.ToString());
            pq.Add("three", 3);
            NUnit.Framework.Assert.AreEqual("[three=3.0, one=1.0]", pq.ToString());
            pq.Add("one", 1.1);
            NUnit.Framework.Assert.AreEqual("[three=3.0, one=1.1, one=1.0]", pq.ToString());
            pq.Add("two", 2);
            NUnit.Framework.Assert.AreEqual("[three=3.0, two=2.0, one=1.1, one=1.0]", pq.ToString());
            NUnit.Framework.Assert.AreEqual("[three=3.000, two=2.000, ...]", pq.ToString(2));
            FixedPrioritiesPriorityQueue <string> clone = pq.Clone();

            NUnit.Framework.Assert.AreEqual(3.0, clone.GetPriority());
            NUnit.Framework.Assert.AreEqual(pq.Current, clone.Current);
            NUnit.Framework.Assert.AreEqual(2.0, clone.GetPriority());
            NUnit.Framework.Assert.AreEqual(pq.Current, clone.Current);
            NUnit.Framework.Assert.AreEqual(1.1, clone.GetPriority());
            NUnit.Framework.Assert.AreEqual(pq.Current, clone.Current);
            NUnit.Framework.Assert.AreEqual(1.0, clone.GetPriority());
            NUnit.Framework.Assert.AreEqual(pq.Current, clone.Current);
            NUnit.Framework.Assert.IsFalse(clone.MoveNext());
            NUnit.Framework.Assert.IsTrue(clone.IsEmpty());
        }
예제 #23
0
 public static CollectionFactory <E> LinkedListFactory <E>()
 {
     return(ErasureUtils.UncheckedCast(LinkedListFactory));
 }
예제 #24
0
 public static CollectionFactory <E> TreeSetFactory <E>()
 {
     return(ErasureUtils.UncheckedCast(TreeSetFactory));
 }
예제 #25
0
 public Beam(int maxBeamSize)
     : this(maxBeamSize, ErasureUtils.UncheckedCast <IComparator <T> >(ScoredComparator.AscendingComparator))
 {
 }
 public static IComparator <T> LengthEndpointsComparator <T>()
     where T : IHasInterval <int>
 {
     return(ErasureUtils.UncheckedCast(HasIntervalConstants.LengthEndpointsComparator));
 }
 public static IToDoubleFunction <T> LengthScorer <T>()
     where T : IHasInterval <int>
 {
     return(ErasureUtils.UncheckedCast(LengthScorer));
 }
예제 #28
0
 public static CollectionFactory <E> ArrayListFactory <E>(int size)
 {
     return(ErasureUtils.UncheckedCast(new CollectionFactory.SizedArrayListFactory(size)));
 }