/// <summary> /// Returns a copy of the given set as a <seealso cref="CharArraySet"/>. If the given set /// is a <seealso cref="CharArraySet"/> the ignoreCase property will be preserved. /// <para> /// <b>Note:</b> If you intend to create a copy of another <seealso cref="CharArraySet"/> where /// the <seealso cref="LuceneVersion"/> of the source set differs from its copy /// <seealso cref="#CharArraySet(Version, Collection, boolean)"/> should be used instead. /// The <seealso cref="#copy(Version, Set)"/> will preserve the <seealso cref="LuceneVersion"/> of the /// source set it is an instance of <seealso cref="CharArraySet"/>. /// </para> /// </summary> /// <param name="matchVersion"> /// compatibility match version see <a href="#version">Version /// note</a> above for details. This argument will be ignored if the /// given set is a <seealso cref="CharArraySet"/>. </param> /// <param name="set"> /// a set to copy </param> /// <returns> a copy of the given set as a <seealso cref="CharArraySet"/>. If the given set /// is a <seealso cref="CharArraySet"/> the ignoreCase property as well as the /// matchVersion will be of the given set will be preserved. </returns> public static CharArraySet Copy <T1>(LuceneVersion matchVersion, HashSet <T1> set) { if (set == EMPTY_SET) { return(EMPTY_SET); } if (set is CharArraySet) { CharArraySet source = (CharArraySet)set; return(new CharArraySet(CharArrayMap.copy(source.map.matchVersion, source.map))); } return(new CharArraySet(matchVersion, set, false)); }
public virtual void TestToString() { CharArrayMap <int?> cm = new CharArrayMap <int?>(TEST_VERSION_CURRENT, Collections.SingletonMap <object, int?>("test", 1), false); assertEquals("[test]", cm.Keys.ToString()); //assertEquals("[1]", cm.Values.ToString()); // TODO: In .NET it would not be possible to make a generic type override the ToString() method to customize it like this without wrapping the result. assertEquals("[test=1]", cm.EntrySet().ToString()); assertEquals("{test=1}", cm.ToString()); cm.Put("test2", 2); assertTrue(cm.Keys.ToString().Contains(", ")); // NOTE: See the note in the KeySet() method as for why this test fails. //assertTrue(cm.Values.ToString().Contains(", ")); // TODO: In .NET it would not be possible to make a generic type override the ToString() method to customize it like this without wrapping the result. assertTrue(cm.EntrySet().ToString().Contains(", ")); assertTrue(cm.ToString().Contains(", ")); }
/// <summary> /// Create set from the supplied map (used internally for readonly maps...) </summary> private CharArrayMap(CharArrayMap <V> toCopy) { this.keys = toCopy.keys; this.values = toCopy.values; this.ignoreCase = toCopy.ignoreCase; this.count = toCopy.count; this.charUtils = toCopy.charUtils; this.matchVersion = toCopy.matchVersion; foreach (var kvp in toCopy.innerDictionary) { this.innerDictionary[kvp.Key] = kvp.Value; } }
public virtual void TestToString() { CharArrayMap <int?> cm = new CharArrayMap <int?>(TEST_VERSION_CURRENT, Collections.SingletonMap <string, int?>("test", 1), false); assertEquals("[test]", cm.Keys.ToString()); assertEquals("[1]", cm.Values.ToString()); assertEquals("[test=1]", cm.EntrySet().ToString()); assertEquals("{test=1}", cm.ToString()); cm.Put("test2", 2); assertTrue(cm.Keys.ToString().Contains(", ")); assertTrue(cm.Values.ToString().Contains(", ")); assertTrue(cm.EntrySet().ToString().Contains(", ")); assertTrue(cm.ToString().Contains(", ")); }
/// <summary> /// Returns an unmodifiable <seealso cref="CharArrayMap"/>. This allows to provide /// unmodifiable views of internal map for "read-only" use. /// </summary> /// <param name="map"> /// a map for which the unmodifiable map is returned. </param> /// <returns> an new unmodifiable <seealso cref="CharArrayMap"/>. </returns> /// <exception cref="NullPointerException"> /// if the given map is <code>null</code>. </exception> public static CharArrayMap <V> UnmodifiableMap(CharArrayMap <V> map) { if (map == null) { throw new ArgumentException("Given map is null", "map"); } if (map == EmptyMap() || map.Count == 0) { return(EmptyMap()); } if (map is UnmodifiableCharArrayMap <V> ) { return(map); } return(new UnmodifiableCharArrayMap <V>(map)); }
/// <summary> /// Returns a copy of the given set as a <seealso cref="CharArraySet"/>. If the given set /// is a <seealso cref="CharArraySet"/> the ignoreCase property will be preserved. /// <para> /// <b>Note:</b> If you intend to create a copy of another <seealso cref="CharArraySet"/> where /// the <seealso cref="LuceneVersion"/> of the source set differs from its copy /// <seealso cref="#CharArraySet(Version, Collection, boolean)"/> should be used instead. /// The <seealso cref="#copy(Version, Set)"/> will preserve the <seealso cref="LuceneVersion"/> of the /// source set it is an instance of <seealso cref="CharArraySet"/>. /// </para> /// </summary> /// <param name="matchVersion"> /// compatibility match version see <a href="#version">Version /// note</a> above for details. This argument will be ignored if the /// given set is a <seealso cref="CharArraySet"/>. </param> /// <param name="set"> /// a set to copy </param> /// <returns> a copy of the given set as a <seealso cref="CharArraySet"/>. If the given set /// is a <seealso cref="CharArraySet"/> the ignoreCase property as well as the /// matchVersion will be of the given set will be preserved. </returns> public static CharArraySet Copy <T1>(LuceneVersion matchVersion, ISet <T1> set) { if (set == EMPTY_SET) { return(EMPTY_SET); } var source = set as CharArraySet; if (source != null) { return(new CharArraySet(CharArrayMap <object> .Copy(source.map.matchVersion, source.map))); } return(new CharArraySet(matchVersion, set.Cast <object>(), false)); }
/// <summary> /// Returns an unmodifiable <seealso cref="CharArraySet"/>. This allows to provide /// unmodifiable views of internal sets for "read-only" use. /// </summary> /// <param name="set"> /// a set for which the unmodifiable set is returned. </param> /// <returns> an new unmodifiable <seealso cref="CharArraySet"/>. </returns> /// <exception cref="NullPointerException"> /// if the given set is <code>null</code>. </exception> public static CharArraySet UnmodifiableSet(CharArraySet set) { if (set == null) { throw new System.NullReferenceException("Given set is null"); } if (set == EMPTY_SET) { return(EMPTY_SET); } if (set.map is CharArrayMap.UnmodifiableCharArrayMap) { return(set); } return(new CharArraySet(CharArrayMap.unmodifiableMap(set.map))); }
/// <summary> /// Returns an unmodifiable <seealso cref="CharArrayMap"/>. This allows to provide /// unmodifiable views of internal map for "read-only" use. /// </summary> /// <param name="map"> /// a map for which the unmodifiable map is returned. </param> /// <returns> an new unmodifiable <seealso cref="CharArrayMap"/>. </returns> /// <exception cref="NullPointerException"> /// if the given map is <code>null</code>. </exception> public static CharArrayMap <V> UnmodifiableMap <V>(CharArrayMap <V> map) { if (map == null) { throw new System.NullReferenceException("Given map is null"); } if (map == EmptyMap() || map.Empty) { return(EmptyMap()); } if (map is UnmodifiableCharArrayMap) { return(map); } return(new UnmodifiableCharArrayMap <V>(map)); }
/// <summary> /// Reads a stem dictionary. Each line contains: /// <code>word<b>\t</b>stem</code> /// (i.e. two tab separated words) /// </summary> /// <returns> stem dictionary that overrules the stemming algorithm </returns> /// <exception cref="IOException"> If there is a low-level I/O error. </exception> public static CharArrayMap <string> GetStemDict(TextReader reader, CharArrayMap <string> result) { try { string line; while ((line = reader.ReadLine()) != null) { string[] wordstem = line.Split(new char[] { '\t' }, 2, StringSplitOptions.RemoveEmptyEntries); result.Put(wordstem[0], wordstem[1]); } } finally { IOUtils.Close(reader); } return(result); }
/// <summary> /// Reads a stem dictionary. Each line contains: /// <pre>word<b>\t</b>stem</pre> /// (i.e. two tab separated words) /// </summary> /// <returns> stem dictionary that overrules the stemming algorithm </returns> /// <exception cref="IOException"> If there is a low-level I/O error. </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public static CharArrayMap<String> getStemDict(java.io.Reader reader, CharArrayMap<String> result) throws java.io.IOException public static CharArrayMap <string> getStemDict(Reader reader, CharArrayMap <string> result) { BufferedReader br = null; try { br = getBufferedReader(reader); string line; while ((line = br.readLine()) != null) { string[] wordstem = line.Split("\t", 2); result.put(wordstem[0], wordstem[1]); } } finally { IOUtils.close(br); } return(result); }
/// <summary> /// Returns a copy of the given map as a <seealso cref="CharArrayMap"/>. If the given map /// is a <seealso cref="CharArrayMap"/> the ignoreCase property will be preserved. /// <para> /// <b>Note:</b> If you intend to create a copy of another <seealso cref="CharArrayMap"/> where /// the <seealso cref="Version"/> of the source map differs from its copy /// <seealso cref="#CharArrayMap(Version, Map, boolean)"/> should be used instead. /// The <seealso cref="#copy(Version, Map)"/> will preserve the <seealso cref="Version"/> of the /// source map it is an instance of <seealso cref="CharArrayMap"/>. /// </para> /// </summary> /// <param name="matchVersion"> /// compatibility match version see <a href="#version">Version /// note</a> above for details. This argument will be ignored if the /// given map is a <seealso cref="CharArrayMap"/>. </param> /// <param name="map"> /// a map to copy </param> /// <returns> a copy of the given map as a <seealso cref="CharArrayMap"/>. If the given map /// is a <seealso cref="CharArrayMap"/> the ignoreCase property as well as the /// matchVersion will be of the given map will be preserved. </returns> public static CharArrayMap <V> copy(LuceneVersion matchVersion, IDictionary <object, V> map) { if (map == EMPTY_MAP) { return(EmptyMap()); } if (map is CharArrayMap) { var m = (CharArrayMap <V>)map; // use fast path instead of iterating all values // this is even on very small sets ~10 times faster than iterating var keys = new char[m.keys.Length][]; Array.Copy(m.keys, 0, keys, 0, keys.Length); var values = new V[m.values.Length]; Array.Copy(m.values, 0, values, 0, values.Length); m = new CharArrayMap <V>(m) { keys = keys, values = values }; return(m); } return(new CharArrayMap <V>(matchVersion, map, false)); }
public UnmodifiableCharArrayMap(CharArrayMap <V> map) : base(map) { }
internal EntrySet(CharArrayMap <V> outerInstance, bool allowModify) { this.outerInstance = outerInstance; this.allowModify = allowModify; }
internal MapEntry(CharArrayMap <V> outerInstance, int pos, bool allowModify) { this.outerInstance = outerInstance; this.pos = pos; this.allowModify = allowModify; }
internal EntryIterator(CharArrayMap <V> outerInstance, bool allowModify) { this.outerInstance = outerInstance; this.allowModify = allowModify; GoNext(); }
internal UnmodifiableCharArraySet(CharArrayMap <object> map) : base(map) { }
public virtual void TestMethods() { CharArrayMap <int?> cm = new CharArrayMap <int?>(TEST_VERSION_CURRENT, 2, false); //Dictionary<string, int?> hm = new Dictionary<string, int?>(); Dictionary <object, int?> hm = new Dictionary <object, int?>(); // TODO: In .NET, we cannot implicitly convert from string to object using generics hm["foo"] = 1; hm["bar"] = 2; cm.PutAll(hm); assertEquals(hm.Count, cm.Count); hm["baz"] = 3; cm.PutAll(hm); assertEquals(hm.Count, cm.Count); // TODO: In .NET we cannot make this conversion implicitly. CharArraySet cs = cm.Keys as CharArraySet; int n = 0; foreach (object o in cs) { assertTrue(cm.ContainsKey(o)); char[] co = (char[])o; assertTrue(cm.ContainsKey(co, 0, co.Length)); n++; } assertEquals(hm.Count, n); assertEquals(hm.Count, cs.Count); assertEquals(cm.Count, cs.Count); // TODO: This directly contradicts the TestModifyOnUnmodifiable test, // where clear is not allowed from the Keys property. //cs.Clear(); //assertEquals(0, cs.Count); //assertEquals(0, cm.Count); try { cs.Add("test"); fail("keySet() allows adding new keys"); } catch (System.NotSupportedException) { // pass } cm.PutAll(hm); assertEquals(hm.Count, cs.Count); assertEquals(cm.Count, cs.Count); IEnumerator <KeyValuePair <object, int?> > iter1 = IDictionaryExtensions.EntrySet(cm).GetEnumerator(); n = 0; while (iter1.MoveNext()) { KeyValuePair <object, int?> entry = iter1.Current; object key = entry.Key; int? val = entry.Value; assertEquals(cm.Get(key), val); // TODO: In .NET the Value property of KeyValuePair is read-only. Do we need a solution? //entry.Value = val * 100; //assertEquals(val * 100, (int)cm.Get(key)); n++; } assertEquals(hm.Count, n); cm.Clear(); cm.PutAll(hm); assertEquals(cm.size(), n); CharArrayMap <int?> .EntryIterator iter2 = cm.EntrySet().GetEnumerator() as CharArrayMap <int?> .EntryIterator; n = 0; while (iter2.MoveNext()) { char[] keyc = (char[])iter2.Current.Key; int? val = iter2.Current.Value; assertEquals(hm[new string(keyc)], val); // TODO: In .NET the Value property of KeyValuePair is read-only. Do we need a solution? //iter2.Value = val * 100; //assertEquals(val * 100, (int)cm.Get(keyc)); n++; } assertEquals(hm.Count, n); // TODO: In .NET, the EntrySet extension method makes a copy of the data // so clearing it won't work like this. cm.EntrySet().clear(); assertEquals(0, cm.size()); assertEquals(0, cm.EntrySet().size()); assertTrue(cm.Count == 0); }
public void TestModifyOnUnmodifiable() { CharArrayMap <int?> map = new CharArrayMap <int?>(TEST_VERSION_CURRENT, 2, false); map.Put("foo", 1); map.Put("bar", 2); int size = map.Count; assertEquals(2, size); assertTrue(map.ContainsKey("foo")); assertEquals(1, map.Get("foo")); assertTrue(map.ContainsKey("bar")); assertEquals(2, map.Get("bar")); map = CharArrayMap.UnmodifiableMap(map); assertEquals("Map size changed due to unmodifiableMap call", size, map.Count); var NOT_IN_MAP = "SirGallahad"; assertFalse("Test String already exists in map", map.ContainsKey(NOT_IN_MAP)); assertNull("Test String already exists in map", map.Get(NOT_IN_MAP)); try { map.Put(NOT_IN_MAP.ToCharArray(), 3); fail("Modified unmodifiable map"); } catch (System.NotSupportedException) { // expected assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP)); assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP)); assertEquals("Size of unmodifiable map has changed", size, map.Count); } try { map.Put(NOT_IN_MAP, 3); fail("Modified unmodifiable map"); } catch (System.NotSupportedException) { // expected assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP)); assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP)); assertEquals("Size of unmodifiable map has changed", size, map.Count); } try { map.Put(new StringBuilder(NOT_IN_MAP), 3); fail("Modified unmodifiable map"); } catch (System.NotSupportedException) { // expected assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP)); assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP)); assertEquals("Size of unmodifiable map has changed", size, map.Count); } #region Added for better .NET support try { map.Add(NOT_IN_MAP, 3); fail("Modified unmodifiable map"); } catch (System.NotSupportedException) { // expected assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP)); assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP)); assertEquals("Size of unmodifiable map has changed", size, map.Count); } try { map.Add(new KeyValuePair <string, int?>(NOT_IN_MAP, 3)); fail("Modified unmodifiable map"); } catch (System.NotSupportedException) { // expected assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP)); assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP)); assertEquals("Size of unmodifiable map has changed", size, map.Count); } try { map[new StringBuilder(NOT_IN_MAP)] = 3; fail("Modified unmodifiable map"); } catch (System.NotSupportedException) { // expected assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP)); assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP)); assertEquals("Size of unmodifiable map has changed", size, map.Count); } try { #pragma warning disable 612, 618 map.Remove(new KeyValuePair <string, int?>("foo", 1)); #pragma warning restore 612, 618 fail("Modified unmodifiable map"); } catch (System.NotSupportedException) { // expected assertEquals("Size of unmodifiable map has changed", size, map.Count); } #endregion try { map.Clear(); fail("Modified unmodifiable map"); } catch (System.NotSupportedException) { // expected assertEquals("Size of unmodifiable map has changed", size, map.size()); } try { map.EntrySet().Clear(); fail("Modified unmodifiable map"); } catch (System.NotSupportedException) { // expected assertEquals("Size of unmodifiable map has changed", size, map.size()); } try { map.Keys.Clear(); fail("Modified unmodifiable map"); } catch (System.NotSupportedException) { // expected assertEquals("Size of unmodifiable map has changed", size, map.size()); } try { map.Put((object)NOT_IN_MAP, 3); fail("Modified unmodifiable map"); } catch (System.NotSupportedException) { // expected assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP)); assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP)); assertEquals("Size of unmodifiable map has changed", size, map.size()); } try { map.PutAll(Collections.SingletonMap <string, int?>(NOT_IN_MAP, 3)); fail("Modified unmodifiable map"); } catch (System.NotSupportedException) { // expected assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP)); assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP)); assertEquals("Size of unmodifiable map has changed", size, map.size()); } assertTrue(map.ContainsKey("foo")); assertEquals(1, map.Get("foo")); assertTrue(map.ContainsKey("bar")); assertEquals(2, map.Get("bar")); }
/// <summary> /// Create set from the specified map (internal only), used also by <seealso cref="CharArrayMap#keySet()"/> </summary> internal CharArraySet(CharArrayMap <object> map) { this.map = map; }
public virtual void TestMethods() { CharArrayMap <int?> cm = new CharArrayMap <int?>(TEST_VERSION_CURRENT, 2, false); Dictionary <string, int?> hm = new Dictionary <string, int?>(); hm["foo"] = 1; hm["bar"] = 2; cm.PutAll(hm); assertEquals(hm.Count, cm.Count); hm["baz"] = 3; cm.PutAll(hm); assertEquals(hm.Count, cm.Count); // LUCENENET: Need to cast here - no implicit conversion. CharArraySet cs = cm.Keys as CharArraySet; int n = 0; foreach (string o in cs) { assertTrue(cm.ContainsKey(o)); char[] co = o.ToCharArray(); assertTrue(cm.ContainsKey(co, 0, co.Length)); n++; } assertEquals(hm.Count, n); assertEquals(hm.Count, cs.Count); assertEquals(cm.Count, cs.Count); cs.Clear(); assertEquals(0, cs.Count); assertEquals(0, cm.Count); try { cs.Add("test"); fail("keySet() allows adding new keys"); } catch (System.NotSupportedException) { // pass } cm.PutAll(hm); assertEquals(hm.Count, cs.Count); assertEquals(cm.Count, cs.Count); // LUCENENET: Need to cast here - no implicit conversion IEnumerator <KeyValuePair <string, int?> > iter1 = (IEnumerator <KeyValuePair <string, int?> >)cm.EntrySet().GetEnumerator(); n = 0; while (iter1.MoveNext()) { KeyValuePair <string, int?> entry = iter1.Current; object key = entry.Key; int? val = entry.Value; assertEquals(cm.Get(key), val); // LUCENENET: Need a cast to get to this method because it is not part of the IEnumerator<T> interface ((CharArrayMap <int?> .EntryIterator)iter1).SetValue(val * 100); assertEquals(val * 100, (int)cm.Get(key)); n++; } assertEquals(hm.Count, n); cm.Clear(); cm.PutAll(hm); assertEquals(cm.size(), n); CharArrayMap <int?> .EntryIterator iter2 = cm.EntrySet().GetEnumerator() as CharArrayMap <int?> .EntryIterator; n = 0; while (iter2.MoveNext()) { var keyc = iter2.Current.Key; int?val = iter2.Current.Value; assertEquals(hm[keyc], val); iter2.SetValue(val * 100); assertEquals(val * 100, (int)cm.Get(keyc)); n++; } assertEquals(hm.Count, n); cm.EntrySet().Clear(); assertEquals(0, cm.size()); assertEquals(0, cm.EntrySet().size()); assertTrue(cm.Count == 0); }
internal CharArraySetAnonymousInnerClassHelper(CharArrayMap <object> map) : base(map) { // TODO }