📄 persistentlistimpl.cs
字号:
}
internal ListIntermediatePage() {}
internal ListIntermediatePage(Storage storage) : base(storage)
{
nChildren = new int[nIntermediatePageItems];
}
}
#if USE_GENERICS
public T this[int i]
#else
public Object this[int i]
#endif
{
get
{
if (i < 0 || i >= nElems)
{
throw new IndexOutOfRangeException("index=" + i + ", size=" + nElems);
}
#if USE_GENERICS
return (T)root[i];
#else
return root[i];
#endif
}
set
{
if (i < 0 || i >= nElems)
{
throw new IndexOutOfRangeException("index=" + i + ", size=" + nElems);
}
root[i] = (IPersistent)value;
}
}
internal IPersistent getPosition(TreePosition pos, int i)
{
if (i < 0 || i >= nElems)
{
throw new IndexOutOfRangeException("index=" + i + ", size=" + nElems);
}
if (pos.page != null && i >= pos.index && i < pos.index + pos.page.nItems)
{
return pos.page.items[i - pos.index];
}
pos.index = i;
return root.getPosition(pos, i);
}
internal IPersistent getRawPosition(TreePosition pos, int i)
{
if (i < 0 || i >= nElems)
{
throw new IndexOutOfRangeException("index=" + i + ", size=" + nElems);
}
if (pos.page != null && i >= pos.index && i < pos.index + pos.page.nItems)
{
return pos.page.items.GetRaw(i - pos.index);
}
pos.index = i;
return root.getRawPosition(pos, i);
}
#if USE_GENERICS
public T[] ToArray()
{
T[] a = new T[nElems];
int i = 0;
foreach (T obj in this) {
a[i++] = obj;
}
return a;
}
#else
public IPersistent[] ToArray()
{
IPersistent[] a = new IPersistent[nElems];
int i = 0;
foreach (IPersistent obj in this)
{
a[i++] = obj;
}
return a;
}
#endif
#if !USE_GENERICS
public bool IsReadOnly
{
get
{
return false;
}
}
#endif
public bool IsFixedSize
{
get
{
return false;
}
}
public Array ToArray(Type elemType)
{
Array a = Array.CreateInstance(elemType, nElems);
int i = 0;
foreach (object obj in this)
{
a.SetValue(obj, i++);
}
return a;
}
#if USE_GENERICS
public override bool Contains(T obj)
#else
public bool Contains(object obj)
#endif
{
return IndexOf(obj) >= 0;
}
#if USE_GENERICS
public int IndexOf(T obj)
#else
public int IndexOf(object obj)
#endif
{
int i = 0;
if (obj == null)
{
foreach (object o in this)
{
if (o == null)
{
return i;
}
i += 1;
}
}
else
{
foreach (object o in this)
{
if (obj == o)
{
return i;
}
i += 1;
}
}
return -1;
}
#if USE_GENERICS
public override void Clear()
#else
public void Clear()
#endif
{
modCount += 1;
root.prune();
root = new ListPage(Storage);
nElems = 0;
Modify();
}
public override int Count
{
get
{
return nElems;
}
}
#if USE_GENERICS
public override void Add(T o)
{
Insert(nElems, o);
}
#else
public int Add(object o)
{
Insert(nElems, o);
return nElems;
}
#endif
#if USE_GENERICS
public void Insert(int i, T o)
#else
public void Insert(int i, object o)
#endif
{
if (i < 0 || i > nElems)
{
throw new IndexOutOfRangeException("index=" + i + ", size=" + nElems);
}
ListPage overflow = root.add(i, (IPersistent)o);
if (overflow != null)
{
ListIntermediatePage pg = new ListIntermediatePage(Storage);
pg.setItem(0, overflow);
pg.items[1] = root;
pg.nChildren[1] = int.MaxValue;
pg.nItems = 2;
root = pg;
}
nElems += 1;
modCount += 1;
Modify();
}
public void RemoveAt(int i)
{
if (i < 0 || i >= nElems)
{
throw new IndexOutOfRangeException("index=" + i + ", size=" + nElems);
}
root.remove(i);
if (root.nItems == 1 && root is ListIntermediatePage)
{
ListPage newRoot = (ListPage)root.items[0];
root.Deallocate();
root = newRoot;
}
nElems -= 1;
modCount += 1;
Modify();
}
#if USE_GENERICS
public override bool Remove(T obj)
#else
public void Remove(object obj)
{
RemoveObject(obj);
}
public bool RemoveObject(object obj)
#endif
{
int i = IndexOf(obj);
if (i >= 0)
{
RemoveAt(i);
return true;
}
return false;
}
#if USE_GENERICS
class ListEnumerator : TreePosition, IBidirectionalEnumerator<T>, PersistentEnumerator
#else
class ListEnumerator : TreePosition, PersistentEnumerator, IBidirectionalEnumerator
#endif
{
public void Dispose() {}
public bool MoveNext()
{
checkForCommodification();
if (i+1 < list.Count)
{
i += 1;
return true;
}
return false;
}
public bool MovePrevious()
{
checkForCommodification();
if (i > 0)
{
i -= 1;
return true;
}
return false;
}
#if USE_GENERICS
object IEnumerator.Current
{
get
{
checkForCommodification();
return list.getPosition(this, i);
}
}
public T Current
{
get
{
checkForCommodification();
return (T)list.getPosition(this, i);
}
}
#else
public object Current
{
get
{
checkForCommodification();
return list.getPosition(this, i);
}
}
#endif
public int CurrentOid
{
get
{
checkForCommodification();
return list.getRawPosition(this, i).Oid;
}
}
public void Reset()
{
i = start;
}
void checkForCommodification()
{
if (modCount != list.modCount)
{
throw new InvalidOperationException("B-Tree was modified");
}
}
#if USE_GENERICS
internal ListEnumerator(PersistentListImpl<T> list, int start)
#else
internal ListEnumerator(PersistentListImpl list, int start)
#endif
{
this.list = list;
this.start = start;
modCount = list.modCount;
i = start;
}
private int i;
private int start;
private int modCount;
#if USE_GENERICS
private PersistentListImpl<T> list;
#else
private PersistentListImpl list;
#endif
}
#if USE_GENERICS
public override IEnumerator<T> GetEnumerator()
#else
public override IEnumerator GetEnumerator()
#endif
{
return GetEnumerator(-1);
}
#if USE_GENERICS
public IBidirectionalEnumerator<T> GetEnumerator(int i)
#else
public IBidirectionalEnumerator GetEnumerator(int i)
#endif
{
return new ListEnumerator(this, i);
}
internal PersistentListImpl() {}
internal PersistentListImpl(Storage storage) : base(storage)
{
root = new ListPage(storage);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -