public static ICollection <T> Intersection <T>(ICollection <T> set1, ICollection <T> set2)
        {
            ICollection <T> intersect = Generics.NewHashSet();

            foreach (T t in set1)
            {
                if (set2.Contains(t))
                {
                    intersect.Add(t);
                }
            }
            return(intersect);
        }
Ejemplo n.º 2
0
        // private to prevent instantiation
        /// <summary>Returns the set cross product of s1 and s2, as <code>Pair</code>s</summary>
        public static ICollection <Pair <E, F> > Cross <E, F>(ICollection <E> s1, ICollection <F> s2)
        {
            ICollection <Pair <E, F> > s = Generics.NewHashSet();

            foreach (E o1 in s1)
            {
                foreach (F o2 in s2)
                {
                    s.Add(new Pair <E, F>(o1, o2));
                }
            }
            return(s);
        }
Ejemplo n.º 3
0
        /// <summary>Returns the difference of sets s1 and s2.</summary>
        public static ICollection <E> Diff <E>(ICollection <E> s1, ICollection <E> s2)
        {
            ICollection <E> s = Generics.NewHashSet();

            foreach (E o in s1)
            {
                if (!s2.Contains(o))
                {
                    s.Add(o);
                }
            }
            return(s);
        }
        public static ICollection <T> UnionAsSet <T>(params ICollection <T>[] sets)
        {
            ICollection <T> union = Generics.NewHashSet();

            foreach (ICollection <T> set in sets)
            {
                foreach (T t in set)
                {
                    union.Add(t);
                }
            }
            return(union);
        }
        /// <summary>Returns the current stack as a list</summary>
        public virtual IList <T> AsList()
        {
            IList <T> result = Generics.NewArrayList(size);

            Edu.Stanford.Nlp.Util.TreeShapedStack <T> current = this;
            for (int index = 0; index < size; ++index)
            {
                result.Add(current.data);
                current = current.Pop();
            }
            Java.Util.Collections.Reverse(result);
            return(result);
        }
        /// <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);
            }
        }
        public virtual ICollection <K3> ThirdKeySet()
        {
            ICollection <K3> keys = Generics.NewHashSet();

            foreach (K1 k1 in map.Keys)
            {
                ThreeDimensionalMap <K2, K3, K4, V> m3 = map[k1];
                foreach (K2 k2 in m3.FirstKeySet())
                {
                    Sharpen.Collections.AddAll(keys, m3.Get(k2).FirstKeySet());
                }
            }
            return(keys);
        }
Ejemplo n.º 8
0
        public virtual ICollection <V> Values()
        {
            ICollection <V> allValues = Generics.NewHashSet();

            foreach (K1 k1 in map.Keys)
            {
                ICollection <ICollection <V> > collectionOfValues = GetCollectionValuedMap(k1).Values;
                foreach (ICollection <V> values in collectionOfValues)
                {
                    Sharpen.Collections.AddAll(allValues, values);
                }
            }
            return(allValues);
        }
        public static ICollection <T> UnionAsSet <T>(ICollection <T> set1, ICollection <T> set2)
        {
            ICollection <T> union = Generics.NewHashSet();

            foreach (T t in set1)
            {
                union.Add(t);
            }
            foreach (T t_1 in set2)
            {
                union.Add(t_1);
            }
            return(union);
        }
Ejemplo n.º 10
0
        public static string GetSignature(string name, Properties properties)
        {
            string[] prefixes = new string[] { (name != null && !name.IsEmpty()) ? name + '.' : string.Empty };
            // TODO(gabor) This is a hack, as tokenize and ssplit depend on each other so heavily
            if ("tokenize".Equals(name) || "ssplit".Equals(name))
            {
                prefixes = new string[] { "tokenize", "ssplit" };
            }
            // TODO [chris 2017]: Another hack. Traditionally, we have called the cleanxml properties clean!
            if ("clean".Equals(name) || "cleanxml".Equals(name))
            {
                prefixes = new string[] { "clean", "cleanxml" };
            }
            if ("mention".Equals(name))
            {
                prefixes = new string[] { "mention", "coref" };
            }
            if ("ner".Equals(name))
            {
                prefixes = new string[] { "ner", "sutime" };
            }
            Properties propertiesCopy = new Properties();

            propertiesCopy.PutAll(properties);
            // handle special case of implied properties (e.g. sentiment implies parse should set parse.binaryTrees = true
            // TODO(jb) This is a hack: handle implied need for binary trees if sentiment annotator is present
            ICollection <string> annoNames = Generics.NewHashSet(Arrays.AsList(properties.GetProperty("annotators", string.Empty).Split("[, \t]+")));

            if ("parse".Equals(name) && annoNames.Contains("sentiment") && !properties.Contains("parse.binaryTrees"))
            {
                propertiesCopy.SetProperty("parse.binaryTrees", "true");
            }
            // keep track of all relevant properties for this annotator here!
            StringBuilder sb = new StringBuilder();

            foreach (string pname in propertiesCopy.StringPropertyNames())
            {
                foreach (string prefix in prefixes)
                {
                    if (pname.StartsWith(prefix))
                    {
                        string pvalue = propertiesCopy.GetProperty(pname);
                        sb.Append(pname).Append(':').Append(pvalue).Append(';');
                    }
                }
            }
            return(sb.ToString());
        }
Ejemplo n.º 11
0
 /// <summary>Returns a unique object o' that .equals the argument o.</summary>
 /// <remarks>
 /// Returns a unique object o' that .equals the argument o.  If o
 /// itself is returned, this is the first request for an object
 /// .equals to o.
 /// </remarks>
 public virtual T Intern(T o)
 {
     lock (this)
     {
         WeakReference <T> @ref = map[o];
         if (@ref == null)
         {
             @ref   = Generics.NewWeakReference(o);
             map[o] = @ref;
         }
         //    else {
         //      log.info("Found dup for " + o);
         //    }
         return(@ref.Get());
     }
 }
Ejemplo n.º 12
0
        /// <summary>Sample k items uniformly from an Iterable of size n (without replacement).</summary>
        /// <param name="items">The items from which to sample.</param>
        /// <param name="n">The total number of items in the Iterable.</param>
        /// <param name="k">The number of items to sample.</param>
        /// <param name="random">The random number generator.</param>
        /// <returns>An Iterable of k items, chosen randomly from the original n items.</returns>
        public static IEnumerable <T> Sample <T>(IEnumerable <T> items, int n, int k, Random random)
        {
            // assemble a list of all indexes
            IList <int> indexes = new List <int>();

            for (int i = 0; i < n; ++i)
            {
                indexes.Add(i);
            }
            // shuffle the indexes and select the first k
            Java.Util.Collections.Shuffle(indexes, random);
            ICollection <int> indexSet = Generics.NewHashSet(indexes.SubList(0, k));

            // filter down to only the items at the selected indexes
            return(Iterables.Filter(items, new _IPredicate_614(indexSet)));
        }
 /// <summary>
 /// Return an immutable Set containing the same elements as the specified
 /// array.
 /// </summary>
 /// <remarks>
 /// Return an immutable Set containing the same elements as the specified
 /// array. Arrays with 0 or 1 elements are special cased to return the
 /// efficient small sets from the Collections class.
 /// </remarks>
 public static ICollection <T> AsImmutableSet <T>(T[] a)
 {
     if (a.Length == 0)
     {
         return(Java.Util.Collections.EmptySet());
     }
     else
     {
         if (a.Length == 1)
         {
             return(Java.Util.Collections.Singleton(a[0]));
         }
         else
         {
             return(Java.Util.Collections.UnmodifiableSet(Generics.NewHashSet(Arrays.AsList(a))));
         }
     }
 }
        public virtual ICollection <K4> FourthKeySet()
        {
            ICollection <K4> keys = Generics.NewHashSet();

            foreach (K1 k1 in map.Keys)
            {
                ThreeDimensionalMap <K2, K3, K4, V> m3 = map[k1];
                foreach (K2 k2 in m3.FirstKeySet())
                {
                    TwoDimensionalMap <K3, K4, V> m2 = m3.Get(k2);
                    foreach (K3 k3 in m2.FirstKeySet())
                    {
                        Sharpen.Collections.AddAll(keys, m2.Get(k3).Keys);
                    }
                }
            }
            return(keys);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Checks to make sure that all properties specified in
        /// <paramref name="properties"/>
        /// are known to the program by checking that each simply overrides
        /// a default value.
        /// </summary>
        /// <param name="properties">Current properties</param>
        /// <param name="defaults">Default properties which lists all known keys</param>
        public static void CheckProperties(Properties properties, Properties defaults)
        {
            ICollection <string> names = Generics.NewHashSet();

            Sharpen.Collections.AddAll(names, properties.StringPropertyNames());
            foreach (string defaultName in defaults.StringPropertyNames())
            {
                names.Remove(defaultName);
            }
            if (!names.IsEmpty())
            {
                if (names.Count == 1)
                {
                    throw new ArgumentException("Unknown property: " + names.GetEnumerator().Current);
                }
                else
                {
                    throw new ArgumentException("Unknown properties: " + names);
                }
            }
        }
Ejemplo n.º 16
0
        /// <summary>Split a list into numFolds (roughly) equally sized folds.</summary>
        /// <remarks>
        /// Split a list into numFolds (roughly) equally sized folds. The earlier folds
        /// may have one more item in them than later folds.
        /// <br />
        /// The lists returned are subList()s of the original list.
        /// Therefore, don't try to modify the sublists, and don't modify the
        /// original list while the sublists are in use.
        /// </remarks>
        public static IList <IList <T> > PartitionIntoFolds <T>(IList <T> values, int numFolds)
        {
            IList <IList <T> > folds = Generics.NewArrayList();
            int numValues            = values.Count;
            int foldSize             = numValues / numFolds;
            int remainder            = numValues % numFolds;
            int start = 0;
            int end   = foldSize;

            for (int foldNum = 0; foldNum < numFolds; foldNum++)
            {
                // if we're in the first 'remainder' folds, we get an extra item
                if (foldNum < remainder)
                {
                    end++;
                }
                folds.Add(values.SubList(start, end));
                start = end;
                end  += foldSize;
            }
            return(folds);
        }
Ejemplo n.º 17
0
 /// <summary>Create an ArrayHeap.</summary>
 /// <param name="cmp">The objects added will be ordered using the <code>Comparator</code>.</param>
 public ArrayHeap(IComparator <E> cmp)
 {
     this.cmp      = cmp;
     indexToEntry  = new List <ArrayHeap.HeapEntry <E> >();
     objectToEntry = Generics.NewHashMap();
 }
 public ThreeDimensionalMap()
 {
     this.map = Generics.NewHashMap();
 }
 public OneToOneMap()
 {
     //------------------------------------------------------------
     m_leftAsKey  = Generics.NewHashMap();
     m_rightAsKey = Generics.NewHashMap();
 }
Ejemplo n.º 20
0
 /// <summary>Creates a new Index.</summary>
 /// <param name="capacity">Initial capacity of Index.</param>
 public HashIndex(int capacity)
     : base()
 {
     objects = new List <E>(capacity);
     indexes = Generics.NewHashMap(capacity);
 }
Ejemplo n.º 21
0
 public override IDictionary <K1, V1> SetMap <K1, V1>(IDictionary <K1, V1> map)
 {
     map = Generics.NewHashMap();
     return(map);
 }
Ejemplo n.º 22
0
 public override IDictionary <K1, V1> SetMap <K1, V1>(IDictionary <K1, V1> map, int initCapacity)
 {
     map = Generics.NewHashMap(initCapacity);
     return(map);
 }
Ejemplo n.º 23
0
 /// <summary>Creates a new Index.</summary>
 public HashIndex()
     : base()
 {
     objects = new List <E>();
     indexes = Generics.NewHashMap();
 }
Ejemplo n.º 24
0
 public virtual void Clear()
 {
     map = Generics.NewWeakHashMap();
 }
Ejemplo n.º 25
0
 public override IDictionary <K, V> NewMap()
 {
     return(Generics.NewHashMap());
 }
Ejemplo n.º 26
0
 public override ICollection <K> NewSet(ICollection <K> init)
 {
     return(Generics.NewHashSet(init));
 }
 public FourDimensionalMap()
 {
     this.map = Generics.NewHashMap();
 }
Ejemplo n.º 28
0
 public ArrayHeap(IComparator <E> cmp, int initCapacity)
 {
     this.cmp      = cmp;
     indexToEntry  = new List <ArrayHeap.HeapEntry <E> >(initCapacity);
     objectToEntry = Generics.NewHashMap(initCapacity);
 }
Ejemplo n.º 29
0
 public static ICollection <T> AsSet <T>(params T[] o)
 {
     return(Generics.NewHashSet(Arrays.AsList(o)));
 }
Ejemplo n.º 30
0
 public override IDictionary <K, V> NewMap(int initCapacity)
 {
     return(Generics.NewHashMap(initCapacity));
 }