19 using System.Collections;
21 using System.Collections.Generic;
23 namespace Lucene.Net.Analysis
48 bool _ReadOnly =
false;
49 const int INIT_SIZE = 8;
55 private void Init(
int startSize,
bool ignoreCase)
57 this._IgnoreCase = ignoreCase;
59 while (startSize + (startSize >> 2) > size)
61 _Entries =
new char[size][];
69 Init(startSize, ignoreCase);
74 Init(c.Count(), ignoreCase);
81 Init(c.Count(), ignoreCase);
85 private void AddItems<T>(IEnumerable<T> items)
87 foreach(var item
in items)
94 private CharArraySet(
char[][] entries,
bool ignoreCase,
int count)
96 this._Entries = entries;
97 this._IgnoreCase = ignoreCase;
104 public virtual bool Contains(
char[] text,
int off,
int len)
106 return _Entries[GetSlot(text, off, len)] != null;
109 public virtual bool Contains(
string text)
111 return _Entries[GetSlot(text)] != null;
115 private int GetSlot(
char[] text,
int off,
int len)
117 int code = GetHashCode(text, off, len);
118 int pos = code & (_Entries.Length - 1);
119 char[] text2 = _Entries[pos];
120 if (text2 != null && !Equals(text, off, len, text2))
122 int inc = ((code >> 8) + code) | 1;
126 pos = code & (_Entries.Length - 1);
127 text2 = _Entries[pos];
129 while (text2 != null && !Equals(text, off, len, text2));
135 private int GetSlot(
string text)
137 int code = GetHashCode(text);
138 int pos = code & (_Entries.Length - 1);
139 char[] text2 = _Entries[pos];
140 if (text2 != null && !Equals(text, text2))
142 int inc = ((code >> 8) + code) | 1;
146 pos = code & (_Entries.Length - 1);
147 text2 = _Entries[pos];
149 while (text2 != null && !Equals(text, text2));
154 public bool Add(
string text)
156 if (_ReadOnly)
throw new NotSupportedException();
157 return Add(text.ToCharArray());
164 public bool Add(
char[] text)
166 if (_ReadOnly)
throw new NotSupportedException();
169 for (
int i = 0; i < text.Length; i++)
170 text[i] = Char.ToLower(text[i]);
171 int slot = GetSlot(text, 0, text.Length);
172 if (_Entries[slot] != null)
174 _Entries[slot] = text;
177 if (_Count + (_Count >> 2) > _Entries.Length)
185 private bool Equals(
char[] text1,
int off,
int len,
char[] text2)
187 if (len != text2.Length)
191 for (
int i = 0; i < len; i++)
193 if (
char.ToLower(text1[off + i]) != text2[i])
199 for (
int i = 0; i < len; i++)
201 if (text1[off + i] != text2[i])
208 private bool Equals(
string text1,
char[] text2)
210 int len = text1.Length;
211 if (len != text2.Length)
215 for (
int i = 0; i < len; i++)
217 if (
char.ToLower(text1[i]) != text2[i])
223 for (
int i = 0; i < len; i++)
225 if (text1[i] != text2[i])
232 private void Rehash()
234 int newSize = 2 * _Entries.Length;
235 char[][] oldEntries = _Entries;
236 _Entries =
new char[newSize][];
238 for (
int i = 0; i < oldEntries.Length; i++)
240 char[] text = oldEntries[i];
244 _Entries[GetSlot(text, 0, text.Length)] = text;
249 private int GetHashCode(
char[] text,
int offset,
int len)
252 int stop = offset + len;
255 for (
int i = offset; i < stop; i++)
257 code = code * 31 +
char.ToLower(text[i]);
262 for (
int i = offset; i < stop; i++)
264 code = code * 31 + text[i];
270 private int GetHashCode(
string text)
273 int len = text.Length;
276 for (
int i = 0; i < len; i++)
278 code = code * 31 +
char.ToLower(text[i]);
283 for (
int i = 0; i < len; i++)
285 code = code * 31 + text[i];
293 get {
return _Count; }
298 get {
return _Count == 0; }
301 public bool Contains(
object item)
303 var text = item as
char[];
304 return text != null ? Contains(text, 0, text.Length) : Contains(item.ToString());
307 public bool Add(
object item)
309 return Add(item.ToString());
312 void ICollection<string>.Add(
string item)
327 throw new ArgumentNullException(
"Given set is null");
328 if (
set == EMPTY_SET)
333 var newSet =
new CharArraySet(
set._Entries,
set._IgnoreCase,
set.Count) {IsReadOnly =
true};
347 throw new ArgumentNullException(
"set",
"Given set is null!");
348 if (
set == EMPTY_SET)
352 arrSet.AddItems(
set);
358 throw new NotSupportedException(
"Remove not supported!");
361 public bool IsReadOnly
363 get {
return _ReadOnly; }
364 private set { _ReadOnly = value; }
368 public void UnionWith(IEnumerable<string> other)
370 if (_ReadOnly)
throw new NotSupportedException();
372 foreach (
string s
in other)
374 Add(s.ToCharArray());
379 public void AddAll(IEnumerable<string> coll)
384 #region Unneeded methods
385 public void RemoveAll(ICollection<string> c)
387 throw new NotSupportedException();
390 public void RetainAll(ICollection<string> c)
392 throw new NotSupportedException();
395 void ICollection<string>.CopyTo(
string[] array,
int arrayIndex)
397 throw new NotSupportedException();
400 void ISet<string>.IntersectWith(IEnumerable<string> other)
402 throw new NotSupportedException();
405 void ISet<string>.ExceptWith(IEnumerable<string> other)
407 throw new NotSupportedException();
410 void ISet<string>.SymmetricExceptWith(IEnumerable<string> other)
412 throw new NotSupportedException();
415 bool ISet<string>.IsSubsetOf(IEnumerable<string> other)
417 throw new NotSupportedException();
420 bool ISet<string>.IsSupersetOf(IEnumerable<string> other)
422 throw new NotSupportedException();
425 bool ISet<string>.IsProperSupersetOf(IEnumerable<string> other)
427 throw new NotSupportedException();
430 bool ISet<string>.IsProperSubsetOf(IEnumerable<string> other)
432 throw new NotSupportedException();
435 bool ISet<string>.Overlaps(IEnumerable<string> other)
437 throw new NotSupportedException();
440 bool ISet<string>.SetEquals(IEnumerable<string> other)
442 throw new NotSupportedException();
445 bool ICollection<string>.Remove(
string item)
447 throw new NotSupportedException();
466 public bool MoveNext()
470 while (pos < _Creator._Entries.Length && (cur = _Creator._Entries[pos]) == null)
476 public char[] NextCharArray()
481 public string Current
483 get {
return new string(NextCharArray()); }
486 public void Dispose()
490 object IEnumerator.Current
492 get {
return new string(NextCharArray()); }
497 throw new NotImplementedException();
501 public IEnumerator<string> StringEnumerator()
506 public IEnumerator<string> GetEnumerator()
511 IEnumerator IEnumerable.GetEnumerator()
513 return GetEnumerator();