From 4d58533d692803ab50539c8f8b2459f44fd5847b Mon Sep 17 00:00:00 2001 From: Libor Kubala Date: Tue, 1 May 2018 14:55:09 +0200 Subject: [PATCH] Upgrades 2.0.4, IDisposable, ICloneable --- FriendlyCSharp.Databases/BTreeN/FcsBTreeN.cs | 168 ++++++++++++------ .../BTreeN/FcsDuplValueFastBTreeN.cs | 115 +++++++----- .../BTreeN/FcsFastBTreeN.cs | 54 +++--- .../FriendlyCSharp.Databases.csproj | 8 +- .../Storage/FcsInmemStream.cs | 2 +- .../BtnEnumerator.Multi.sample.csproj | 2 +- Samples/BtnEnumerator.Multi.sample/Program.cs | 36 ++-- .../DuplicityKeys.Multi.sample.csproj | 2 +- .../FcsFastBTreeN.Multi.benchmark.csproj | 2 +- .../FcsInmemStream.Multi.sample.csproj | 2 +- .../MultipleKeys.Multi.sample.csproj | 2 +- 11 files changed, 245 insertions(+), 148 deletions(-) diff --git a/FriendlyCSharp.Databases/BTreeN/FcsBTreeN.cs b/FriendlyCSharp.Databases/BTreeN/FcsBTreeN.cs index 3bff5fd..fa417b8 100644 --- a/FriendlyCSharp.Databases/BTreeN/FcsBTreeN.cs +++ b/FriendlyCSharp.Databases/BTreeN/FcsBTreeN.cs @@ -8,8 +8,8 @@ namespace FriendlyCSharp.Databases { - public partial class FcsBTreeN : IEnumerable?> - where TKey : struct, IComparable + public partial class FcsBTreeN : IEnumerable> + where TKey : IComparable { protected const int _btnDefaultBTreeN = 32; protected object _btnLockAdd; @@ -57,14 +57,13 @@ public void Dispose() } } ////////////////////////// - protected internal class KeyValuePage + protected internal class KeyValuePage : IDisposable, ICloneable { public UInt32 flags; public bool bUpdatedValue; public int iDataCount; public KeyValue[] aData; public KeyValuePage[] kvPageNextRight; - public long[] aPos; ////////////////////////// public KeyValuePage(int iPageN, bool bPageNext) { @@ -76,7 +75,47 @@ public KeyValuePage(int iPageN, bool bPageNext) kvPageNextRight = new KeyValuePage[(iPageN * 2) + 1]; else kvPageNextRight = null; - aPos = null; + } + #region IDisposable + private bool disposedValue = false; + public void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + if (kvPageNextRight != null) + { + for (int oo = 0; oo <= iDataCount; oo++) + { + if (kvPageNextRight[oo] != null) + kvPageNextRight[oo].Dispose(true); + } + } + aData = null; + kvPageNextRight = null; + } + disposedValue = true; + } + } + void IDisposable.Dispose() + { + Dispose(true); + } + #endregion + public object Clone() + { + KeyValuePage root = (KeyValuePage)this.MemberwiseClone(); + aData = (KeyValue[])aData.Clone(); + if (kvPageNextRight != null) + { + for (int ii = 0; ii <= root.iDataCount; ii++) + { + if (root.kvPageNextRight[ii] != null) + root.kvPageNextRight[ii] = (KeyValuePage)root.kvPageNextRight[ii].Clone(); + } + } + return root; } } ////////////////////////// @@ -326,7 +365,7 @@ protected virtual bool BtnUpdates(TKey keyAdd, TValue valueAdd, ref TValue value return bNullResult; } ////////////////////////// - public bool? BtnAdd(TKey key, ref TValue value, object objUpdates) + public bool? BtnAddNoLock(TKey key, ref TValue value, object objUpdates) { KeyValue keyValue; keyValue.key = key; @@ -335,25 +374,22 @@ protected virtual bool BtnUpdates(TKey keyAdd, TValue valueAdd, ref TValue value KeyValuePage kvPageDown = _btnRoot; bool? bNullResult = null; - lock (_btnLockAdd) + bNullResult = _BtnAdd(ref keyValue, out KeyValue kvUp, ref kvPageDown, ref bUp, objUpdates); + if (bUp) { - bNullResult = _BtnAdd(ref keyValue, out KeyValue kvUp, ref kvPageDown, ref bUp, objUpdates); - if (bUp) + bUp = false; + _btnVersion++; + _btnVersionPage++; + _btnUpdatedRoot = true; + KeyValuePage QQ = new KeyValuePage(BtnBTreeN, kvPageDown != null) { iDataCount = 1 }; + QQ.aData[1] = kvUp; + if (QQ.kvPageNextRight != null) { - bUp = false; - _btnVersion++; - _btnVersionPage++; - _btnUpdatedRoot = true; - KeyValuePage QQ = new KeyValuePage(BtnBTreeN, kvPageDown != null) { iDataCount = 1 }; - QQ.aData[1] = kvUp; - if (QQ.kvPageNextRight != null) - { - QQ.kvPageNextRight[0] = _btnRoot; - QQ.kvPageNextRight[1] = kvPageDown; - } - _btnRoot = QQ; - bNullResult = new bool?(true); + QQ.kvPageNextRight[0] = _btnRoot; + QQ.kvPageNextRight[1] = kvPageDown; } + _btnRoot = QQ; + bNullResult = new bool?(true); } if (bNullResult != null) value = keyValue.value; @@ -363,19 +399,51 @@ protected virtual bool BtnUpdates(TKey keyAdd, TValue valueAdd, ref TValue value return bNullResult; // add = true, update = false, else null; } ////////////////////////// + public bool? BtnAddNoLock(TKey key, TValue value, object objUpdates) + { + return BtnAddNoLock(key, ref value, objUpdates); + } + ////////////////////////// + public bool? BtnAddNoLock(TKey key, ref TValue value) + { + return BtnAddNoLock(key, ref value, null); + } + ////////////////////////// + public bool? BtnAddNoLock(TKey key, TValue value) + { + return BtnAddNoLock(key, ref value, null); + } + ////////////////////////// + public bool? BtnAdd(TKey key, ref TValue value, object objUpdates) + { + lock (_btnLockAdd) + { + return BtnAddNoLock(key, ref value, objUpdates); + } + } + ////////////////////////// public bool? BtnAdd(TKey key, TValue value, object objUpdates) { - return BtnAdd(key, ref value, objUpdates); + lock (_btnLockAdd) + { + return BtnAddNoLock(key, ref value, objUpdates); + } } ////////////////////////// public bool? BtnAdd(TKey key, ref TValue value) { - return BtnAdd(key, ref value, null); + lock (_btnLockAdd) + { + return BtnAddNoLock(key, ref value, null); + } } ////////////////////////// public bool? BtnAdd(TKey key, TValue value) { - return BtnAdd(key, ref value, null); + lock (_btnLockAdd) + { + return BtnAddNoLock(key, ref value, null); + } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////// BtnDeleteAll ////////////////////////////////////////////////////// @@ -1196,20 +1264,20 @@ public virtual uint BtnUsedKeys() ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public FcsBTreeN.BtnEnumerator GetEnumeratorEx(bool reverse) { - return new BtnEnumerator(this, null, null, reverse, -2); + return new BtnEnumerator(this, default(TKey), default(TKey), reverse, -2); } ////////////////////////// public FcsBTreeN.BtnEnumerator GetEnumeratorEx(bool reverse, int maxCount) { - return new BtnEnumerator(this, null, null, reverse, maxCount); + return new BtnEnumerator(this, default(TKey), default(TKey), reverse, maxCount); } ////////////////////////// - public FcsBTreeN.BtnEnumerator GetEnumeratorEx(TKey? keyLo, TKey? keyHi, bool reverse) + public FcsBTreeN.BtnEnumerator GetEnumeratorEx(TKey keyLo, TKey keyHi, bool reverse) { return new BtnEnumerator(this, keyLo, keyHi, reverse, -3); } ////////////////////////// - public FcsBTreeN.BtnEnumerator GetEnumeratorEx(TKey? keyLo, TKey? keyHi, bool reverse, int maxCount) + public FcsBTreeN.BtnEnumerator GetEnumeratorEx(TKey keyLo, TKey keyHi, bool reverse, int maxCount) { return new BtnEnumerator(this, keyLo, keyHi, reverse, maxCount); } @@ -1220,17 +1288,17 @@ IEnumerator IEnumerable.GetEnumerator() return GetEnumerator(); } ////////////////////////// - public IEnumerator?> GetEnumerator() + public IEnumerator> GetEnumerator() { - return new BtnEnumerator(this, null, null, false, -1); + return new BtnEnumerator(this, default(TKey), default(TKey), false, -1); } ////////////////////////// - public class BtnEnumerator : IEnumerator?> + public class BtnEnumerator : IEnumerator> { // constructor private FcsBTreeN _btn = null; - private TKey? _keyLo; - private TKey? _keyHi; + private TKey _keyLo; + private TKey _keyHi; private int _maxCount; private bool _reverse; // locals @@ -1240,7 +1308,7 @@ public class BtnEnumerator : IEnumerator?> private TValue _value; private int _count; ////////////////////////// - public BtnEnumerator(FcsBTreeN btn, TKey? keyLo, TKey? keyHi, bool reverse, int maxCount) + public BtnEnumerator(FcsBTreeN btn, TKey keyLo, TKey keyHi, bool reverse, int maxCount) { _btn = btn ?? throw new NullReferenceException(); _keyLo = keyLo; @@ -1260,25 +1328,25 @@ public bool MoveNext() if (_bFirst) { _bFirst = false; - if ((_keyLo == null) && (!_reverse)) + if ((_keyLo.Equals(default(TKey))) && (!_reverse)) _bOK = (_btn.BtnFirst(out _key, out _value) != null); - else if ((_keyHi == null) && (_reverse)) + else if ((_keyHi.Equals(default(TKey))) && (_reverse)) _bOK = (_btn.BtnLast(out _key, out _value) != null); else { if (_reverse) { - _key = _keyHi.GetValueOrDefault(); + _key = _keyHi; _bOK = (_btn.BtnSearchPrev(ref _key, out _value) != null); - if ((_keyLo != null) && (_bOK)) - _bOK = (_key.CompareTo(_keyLo.GetValueOrDefault()) >= 0); + if ((!_keyLo.Equals(default(TKey))) && (_bOK)) + _bOK = (_key.CompareTo(_keyLo) >= 0); } else { - _key = _keyLo.GetValueOrDefault(); + _key = _keyLo; _bOK = (_btn.BtnSearch(ref _key, out _value) != null); - if ((_keyHi != null) && (_bOK)) - _bOK = (_key.CompareTo(_keyHi.GetValueOrDefault()) <= 0); + if ((!_keyHi.Equals(default(TKey))) && (_bOK)) + _bOK = (_key.CompareTo(_keyHi) <= 0); } } } @@ -1287,25 +1355,25 @@ public bool MoveNext() if (_reverse) { _bOK = (_btn.BtnPrev(ref _key, out _value) != null); - if ((_keyLo != null) && (_bOK)) - _bOK = (_key.CompareTo(_keyLo.GetValueOrDefault()) >= 0); + if ((!_keyLo.Equals(default(TKey))) && (_bOK)) + _bOK = (_key.CompareTo(_keyLo) >= 0); } else { _bOK = (_btn.BtnNext(ref _key, out _value) != null); - if ((_keyHi != null) && (_bOK)) - _bOK = (_key.CompareTo(_keyHi.GetValueOrDefault()) <= 0); + if ((!_keyHi.Equals(default(TKey))) && (_bOK)) + _bOK = (_key.CompareTo(_keyHi) <= 0); } } return _bOK; } ////////////////////////// - public KeyValuePair? Current + public KeyValuePair Current { get { if (!_bOK) - return null; + return new KeyValuePair(default(TKey), default(TValue)); return new KeyValuePair(_key, _value); } } @@ -1315,7 +1383,7 @@ object IEnumerator.Current get { if (!_bOK) - return null; + return new KeyValuePair(default(TKey), default(TValue)); return new KeyValuePair(_key, _value); } } diff --git a/FriendlyCSharp.Databases/BTreeN/FcsDuplValueFastBTreeN.cs b/FriendlyCSharp.Databases/BTreeN/FcsDuplValueFastBTreeN.cs index 9458b9d..3753a68 100644 --- a/FriendlyCSharp.Databases/BTreeN/FcsDuplValueFastBTreeN.cs +++ b/FriendlyCSharp.Databases/BTreeN/FcsDuplValueFastBTreeN.cs @@ -9,7 +9,7 @@ namespace FriendlyCSharp.Databases { public partial class FcsDuplValueFastBTreeN : IEnumerable> - where TKey : struct, IComparable + where TKey : IComparable { protected const int _btnDefaultBTreeN = 32; protected object _btnLockAdd; @@ -321,7 +321,7 @@ protected virtual bool BtnUpdates(TKey keyAdd, TValue[] valueAdd, ref TValue[] v return bNullResult; } ////////////////////////// - public bool? BtnAdd(TKey key, ref TValue[] value, object objUpdates) + public bool? BtnAddNoLock(TKey key, ref TValue[] value, object objUpdates) { DuplValue kvAdd; kvAdd.key = key; @@ -330,25 +330,22 @@ protected virtual bool BtnUpdates(TKey keyAdd, TValue[] valueAdd, ref TValue[] v DuplValuePage kvPageDown = _btnRoot; bool? bNullResult = null; - lock (_btnLockAdd) + bNullResult = _BtnAdd(ref kvAdd, out DuplValue kvUp, ref kvPageDown, ref bUp, objUpdates); + if (bUp) { - bNullResult = _BtnAdd(ref kvAdd, out DuplValue kvUp, ref kvPageDown, ref bUp, objUpdates); - if (bUp) + bUp = false; + _btnVersion++; + _btnVersionPage++; + _btnUpdatedRoot = true; + DuplValuePage QQ = new DuplValuePage(BtnBTreeN, kvPageDown != null) { iDataCount = 1 }; + QQ.aData[1] = kvUp; + if (QQ.kvPageNextRight != null) { - bUp = false; - _btnVersion++; - _btnVersionPage++; - _btnUpdatedRoot = true; - DuplValuePage QQ = new DuplValuePage(BtnBTreeN, kvPageDown != null) { iDataCount = 1 }; - QQ.aData[1] = kvUp; - if (QQ.kvPageNextRight != null) - { - QQ.kvPageNextRight[0] = _btnRoot; - QQ.kvPageNextRight[1] = kvPageDown; - } - _btnRoot = QQ; - bNullResult = new bool?(true); + QQ.kvPageNextRight[0] = _btnRoot; + QQ.kvPageNextRight[1] = kvPageDown; } + _btnRoot = QQ; + bNullResult = new bool?(true); } if (bNullResult != null) value = kvAdd.value; @@ -358,19 +355,51 @@ protected virtual bool BtnUpdates(TKey keyAdd, TValue[] valueAdd, ref TValue[] v return bNullResult; // add = true, update = false, else null; } ////////////////////////// + public bool? BtnAddNoLock(TKey key, TValue[] value, object objUpdates) + { + return BtnAddNoLock(key, ref value, objUpdates); + } + ////////////////////////// + public bool? BtnAddNoLock(TKey key, ref TValue[] value) + { + return BtnAddNoLock(key, ref value, null); + } + ////////////////////////// + public bool? BtnAddNoLock(TKey key, TValue[] value) + { + return BtnAddNoLock(key, ref value, null); + } + ////////////////////////// + public bool? BtnAdd(TKey key, ref TValue[] value, object objUpdates) + { + lock (_btnLockAdd) + { + return BtnAddNoLock(key, ref value, objUpdates); + } + } + ////////////////////////// public bool? BtnAdd(TKey key, TValue[] value, object objUpdates) { - return BtnAdd(key, ref value, objUpdates); + lock (_btnLockAdd) + { + return BtnAddNoLock(key, ref value, objUpdates); + } } ////////////////////////// public bool? BtnAdd(TKey key, ref TValue[] value) { - return BtnAdd(key, ref value, null); + lock (_btnLockAdd) + { + return BtnAddNoLock(key, ref value, null); + } } ////////////////////////// public bool? BtnAdd(TKey key, TValue[] value) { - return BtnAdd(key, ref value, null); + lock (_btnLockAdd) + { + return BtnAddNoLock(key, ref value, null); + } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////// BtnDeleteAll ////////////////////////////////////////////////////// @@ -1384,20 +1413,20 @@ public virtual uint BtnUsedKeys() ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public FcsDuplValueFastBTreeN.BtnEnumerator GetEnumeratorEx(bool reverse) { - return new BtnEnumerator(this, null, null, reverse, -2); + return new BtnEnumerator(this, default(TKey), default(TKey), reverse, -2); } ////////////////////////// public FcsDuplValueFastBTreeN.BtnEnumerator GetEnumeratorEx(bool reverse, int maxCount) { - return new BtnEnumerator(this, null, null, reverse, maxCount); + return new BtnEnumerator(this, default(TKey), default(TKey), reverse, maxCount); } ////////////////////////// - public FcsDuplValueFastBTreeN.BtnEnumerator GetEnumeratorEx(TKey? keyLo, TKey? keyHi, bool reverse) + public FcsDuplValueFastBTreeN.BtnEnumerator GetEnumeratorEx(TKey keyLo, TKey keyHi, bool reverse) { return new BtnEnumerator(this, keyLo, keyHi, reverse, -3); } ////////////////////////// - public FcsDuplValueFastBTreeN.BtnEnumerator GetEnumeratorEx(TKey? keyLo, TKey? keyHi, bool reverse, int maxCount) + public FcsDuplValueFastBTreeN.BtnEnumerator GetEnumeratorEx(TKey keyLo, TKey keyHi, bool reverse, int maxCount) { return new BtnEnumerator(this, keyLo, keyHi, reverse, maxCount); } @@ -1410,15 +1439,15 @@ IEnumerator IEnumerable.GetEnumerator() ////////////////////////// public IEnumerator> GetEnumerator() { - return new BtnEnumerator(this, null, null, false, -1); + return new BtnEnumerator(this, default(TKey), default(TKey), false, -1); } ////////////////////////// public class BtnEnumerator : IEnumerator> { // constructor private FcsDuplValueFastBTreeN _btn = null; - private TKey? _keyLo; - private TKey? _keyHi; + private TKey _keyLo; + private TKey _keyHi; private int _maxCount; private bool _reverse; // locals @@ -1428,7 +1457,7 @@ public class BtnEnumerator : IEnumerator> private int _count; private DuplValueFast _btnFast; ////////////////////////// - public BtnEnumerator(FcsDuplValueFastBTreeN btn, TKey? keyLo, TKey? keyHi, bool reverse, int maxCount) + public BtnEnumerator(FcsDuplValueFastBTreeN btn, TKey keyLo, TKey keyHi, bool reverse, int maxCount) { _btn = btn ?? throw new NullReferenceException(); _keyLo = keyLo; @@ -1448,25 +1477,25 @@ public bool MoveNext() if (_btnFast.version == int.MinValue) { _btnFast = default(DuplValueFast); - if ((_keyLo == null) && (!_reverse)) + if ((_keyLo.Equals(default(TKey))) && (!_reverse)) _bOK = (_btn.BtnFastFirst(out _key, out _value, out _btnFast) != null); - else if ((_keyHi == null) && (_reverse)) + else if ((_keyHi.Equals(default(TKey))) && (_reverse)) _bOK = (_btn.BtnFastLast(out _key, out _value, out _btnFast) != null); else { if (_reverse) { - _key = _keyHi.GetValueOrDefault(); + _key = _keyHi; _bOK = (_btn.BtnFastSearchPrev(ref _key, out _value, out _btnFast) != null); - if ((_keyLo != null) && (_bOK)) - _bOK = (_key.CompareTo(_keyLo.GetValueOrDefault()) >= 0); + if ((!_keyLo.Equals(default(TKey))) && (_bOK)) + _bOK = (_key.CompareTo(_keyLo) >= 0); } else { - _key = _keyLo.GetValueOrDefault(); + _key = _keyLo; _bOK = (_btn.BtnFastSearch(ref _key, out _value, out _btnFast) != null); - if ((_keyHi != null) && (_bOK)) - _bOK = (_key.CompareTo(_keyHi.GetValueOrDefault()) <= 0); + if ((!_keyHi.Equals(default(TKey))) && (_bOK)) + _bOK = (_key.CompareTo(_keyHi) <= 0); } } } @@ -1475,14 +1504,14 @@ public bool MoveNext() if (_reverse) { _bOK = (_btn.BtnFastPrev(ref _key, out _value, ref _btnFast) != null); - if ((_keyLo != null) && (_bOK)) - _bOK = (_key.CompareTo(_keyLo.GetValueOrDefault()) >= 0); + if ((!_keyLo.Equals(default(TKey))) && (_bOK)) + _bOK = (_key.CompareTo(_keyLo) >= 0); } else { _bOK = (_btn.BtnFastNext(ref _key, out _value, ref _btnFast) != null); - if ((_keyHi != null) && (_bOK)) - _bOK = (_key.CompareTo(_keyHi.GetValueOrDefault()) <= 0); + if ((!_keyHi.Equals(default(TKey))) && (_bOK)) + _bOK = (_key.CompareTo(_keyHi) <= 0); } } return _bOK; @@ -1493,7 +1522,7 @@ public KeyValuePair Current get { if (!_bOK) - _value = null; + return new KeyValuePair(default(TKey), default(TValue[])); return new KeyValuePair(_key, _value); } } @@ -1503,7 +1532,7 @@ object IEnumerator.Current get { if (!_bOK) - return null; + return new KeyValuePair(default(TKey), default(TValue[])); return new KeyValuePair(_key, _value); } } diff --git a/FriendlyCSharp.Databases/BTreeN/FcsFastBTreeN.cs b/FriendlyCSharp.Databases/BTreeN/FcsFastBTreeN.cs index 59e7650..35d6441 100644 --- a/FriendlyCSharp.Databases/BTreeN/FcsFastBTreeN.cs +++ b/FriendlyCSharp.Databases/BTreeN/FcsFastBTreeN.cs @@ -8,8 +8,8 @@ namespace FriendlyCSharp.Databases { - public partial class FcsFastBTreeN : FcsBTreeN, IEnumerable?> - where TKey : struct, IComparable + public partial class FcsFastBTreeN : FcsBTreeN, IEnumerable> + where TKey : IComparable { ////////////////////////// public FcsFastBTreeN(uint allocIdxFast) : base(_btnDefaultBTreeN, allocIdxFast, null) @@ -341,35 +341,35 @@ public virtual (TKey key, TValue value)? BtnFastSearchPrev(TKey key, uint idxFas ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public FcsFastBTreeN.BtnEnumeratorFast GetEnumeratorFastEx(bool reverse) { - return new BtnEnumeratorFast(this, null, null, reverse, -2); + return new BtnEnumeratorFast(this, default(TKey), default(TKey), reverse, -2); } ////////////////////////// public FcsFastBTreeN.BtnEnumeratorFast GetEnumeratorFastEx(bool reverse, int maxCount) { - return new BtnEnumeratorFast(this, null, null, reverse, maxCount); + return new BtnEnumeratorFast(this, default(TKey), default(TKey), reverse, maxCount); } ////////////////////////// - public FcsFastBTreeN.BtnEnumeratorFast GetEnumeratorFastEx(TKey? keyLo, TKey? keyHi, bool reverse) + public FcsFastBTreeN.BtnEnumeratorFast GetEnumeratorFastEx(TKey keyLo, TKey keyHi, bool reverse) { return new BtnEnumeratorFast(this, keyLo, keyHi, reverse, -3); } ////////////////////////// - public FcsFastBTreeN.BtnEnumeratorFast GetEnumeratorFastEx(TKey? keyLo, TKey? keyHi, bool reverse, int maxCount) + public FcsFastBTreeN.BtnEnumeratorFast GetEnumeratorFastEx(TKey keyLo, TKey keyHi, bool reverse, int maxCount) { return new BtnEnumeratorFast(this, keyLo, keyHi, reverse, maxCount); } ////////////////////////// - public new IEnumerator?> GetEnumerator() + public new IEnumerator> GetEnumerator() { - return new BtnEnumeratorFast(this, null, null, false, -1); + return new BtnEnumeratorFast(this, default(TKey), default(TKey), false, -1); } ////////////////////////// - public class BtnEnumeratorFast : IEnumerator?> + public class BtnEnumeratorFast : IEnumerator> { // constructor private FcsFastBTreeN _btn = null; - private TKey? _keyLo; - private TKey? _keyHi; + private TKey _keyLo; + private TKey _keyHi; private int _maxCount; private bool _reverse; // locals @@ -379,7 +379,7 @@ public class BtnEnumeratorFast : IEnumerator?> private int _count; private KeyValueFast _btnFast; ////////////////////////// - public BtnEnumeratorFast(FcsFastBTreeN btn, TKey? keyLo, TKey? keyHi, bool reverse, int maxCount) + public BtnEnumeratorFast(FcsFastBTreeN btn, TKey keyLo, TKey keyHi, bool reverse, int maxCount) { _btn = btn ?? throw new NullReferenceException(); _keyLo = keyLo; @@ -399,25 +399,25 @@ public bool MoveNext() if (_btnFast.version == int.MinValue) { _btnFast = default(KeyValueFast); - if ((_keyLo == null) && (!_reverse)) + if ((_keyLo.Equals(default(TKey))) && (!_reverse)) _bOK = (_btn.BtnFastFirst(out _key, out _value, out _btnFast) != null); - else if ((_keyHi == null) && (_reverse)) + else if ((_keyHi.Equals(default(TKey))) && (_reverse)) _bOK = (_btn.BtnFastLast(out _key, out _value, out _btnFast) != null); else { if (_reverse) { - _key = _keyHi.GetValueOrDefault(); + _key = _keyHi; _bOK = (_btn.BtnFastSearchPrev(ref _key, out _value, out _btnFast)!=null); - if ((_keyLo != null) && (_bOK)) - _bOK = (_key.CompareTo(_keyLo.GetValueOrDefault()) >= 0); + if ((!_keyLo.Equals(default(TKey))) && (_bOK)) + _bOK = (_key.CompareTo(_keyLo) >= 0); } else { - _key = _keyLo.GetValueOrDefault(); + _key = _keyLo; _bOK = (_btn.BtnFastSearch(ref _key, out _value, out _btnFast) != null); - if ((_keyHi != null) && (_bOK)) - _bOK = (_key.CompareTo(_keyHi.GetValueOrDefault()) <= 0); + if ((!_keyHi.Equals(default(TKey))) && (_bOK)) + _bOK = (_key.CompareTo(_keyHi) <= 0); } } } @@ -426,25 +426,25 @@ public bool MoveNext() if (_reverse) { _bOK = (_btn.BtnFastPrev(ref _key, out _value, ref _btnFast) != null); - if ((_keyLo != null) && (_bOK)) - _bOK = (_key.CompareTo(_keyLo.GetValueOrDefault()) >= 0); + if ((!_keyLo.Equals(default(TKey))) && (_bOK)) + _bOK = (_key.CompareTo(_keyLo) >= 0); } else { _bOK = (_btn.BtnFastNext(ref _key, out _value, ref _btnFast) != null); - if ((_keyHi != null) && (_bOK)) - _bOK = (_key.CompareTo(_keyHi.GetValueOrDefault()) <= 0); + if ((!_keyHi.Equals(default(TKey))) && (_bOK)) + _bOK = (_key.CompareTo(_keyHi) <= 0); } } return _bOK; } ////////////////////////// - public KeyValuePair? Current + public KeyValuePair Current { get { if (!_bOK) - return null; + return new KeyValuePair(default(TKey), default(TValue)); return new KeyValuePair(_key, _value); } } @@ -454,7 +454,7 @@ object IEnumerator.Current get { if (!_bOK) - return null; + return new KeyValuePair(default(TKey), default(TValue)); return new KeyValuePair(_key, _value); } } diff --git a/FriendlyCSharp.Databases/FriendlyCSharp.Databases.csproj b/FriendlyCSharp.Databases/FriendlyCSharp.Databases.csproj index e626ab6..5bece53 100644 --- a/FriendlyCSharp.Databases/FriendlyCSharp.Databases.csproj +++ b/FriendlyCSharp.Databases/FriendlyCSharp.Databases.csproj @@ -13,14 +13,14 @@ The B-tree is a generalization of a binary search tree in that a node can have more than two children (Comer 1979, p. 123). Unlike self-balancing binary search trees, the B-tree is optimized for systems that read and write large blocks of data. - Upgrades BtnFind + Upgrades FcsBTreeN and FcsDuplValueFastBTreeN, including addition BtnAddNoLock netstandard2.0 - 2.0.3 - 2.0.3.0 - 2.0.3.0 + 2.0.4 + 2.0.4.0 + 2.0.4.0 false diff --git a/FriendlyCSharp.Databases/Storage/FcsInmemStream.cs b/FriendlyCSharp.Databases/Storage/FcsInmemStream.cs index 6613475..76210b3 100644 --- a/FriendlyCSharp.Databases/Storage/FcsInmemStream.cs +++ b/FriendlyCSharp.Databases/Storage/FcsInmemStream.cs @@ -1100,7 +1100,7 @@ IEnumerator IEnumerable.GetEnumerator() ///////////////////////////////////////////// public IEnumerator GetEnumerator() { - return new ImsEnumerator(this, OFFSET_ERROR, -1); + return new ImsEnumerator(this, ErrorId, -1); } ///////////////////////////////////////////// public class ImsEnumerator : IEnumerator diff --git a/Samples/BtnEnumerator.Multi.sample/BtnEnumerator.Multi.sample.csproj b/Samples/BtnEnumerator.Multi.sample/BtnEnumerator.Multi.sample.csproj index 83b5bd6..4a2747b 100644 --- a/Samples/BtnEnumerator.Multi.sample/BtnEnumerator.Multi.sample.csproj +++ b/Samples/BtnEnumerator.Multi.sample/BtnEnumerator.Multi.sample.csproj @@ -6,7 +6,7 @@ - + diff --git a/Samples/BtnEnumerator.Multi.sample/Program.cs b/Samples/BtnEnumerator.Multi.sample/Program.cs index 1ab8d2b..22118dc 100644 --- a/Samples/BtnEnumerator.Multi.sample/Program.cs +++ b/Samples/BtnEnumerator.Multi.sample/Program.cs @@ -66,42 +66,42 @@ static void Main(string[] args) // output: 5,7,8,10,13,15,18,20,22,24,25,26, FcsBTreeN.BtnEnumerator btnEn = btnTest.GetEnumeratorEx(false, 12); while (btnEn.MoveNext()) - Console.Write(btnEn.Current.GetValueOrDefault().Key + ","); + Console.Write(btnEn.Current.Key + ","); btnEn.Dispose(); Console.WriteLine(); - int? keyLo = 22; - int? keyHi = 38; + int keyLo = 22; + int keyHi = 38; // output: 5,7,8,10,13,15,18,20,22,24,25,26,27,30,32,35,38, - btnEn = btnTest.GetEnumeratorEx(null, keyHi, false); + btnEn = btnTest.GetEnumeratorEx(default(int), keyHi, false); while (btnEn.MoveNext()) - Console.Write(btnEn.Current.GetValueOrDefault().Key + ","); + Console.Write(btnEn.Current.Key + ","); Console.WriteLine(); btnEn.Dispose(); // output: 22,24,25,26,27,30,32,35,38,40,42,45,46, - btnEn = btnTest.GetEnumeratorEx(keyLo, null, false); + btnEn = btnTest.GetEnumeratorEx(keyLo, default(int), false); while (btnEn.MoveNext()) - Console.Write(btnEn.Current.GetValueOrDefault().Key + ","); + Console.Write(btnEn.Current.Key + ","); Console.WriteLine(); btnEn.Dispose(); // output: 22,24,25,26,27,30,32,35,38, btnEn = btnTest.GetEnumeratorEx(keyLo, keyHi, false); while (btnEn.MoveNext()) - Console.Write(btnEn.Current.GetValueOrDefault().Key + ","); + Console.Write(btnEn.Current.Key + ","); Console.WriteLine(); // output: 22,24,25,26,27,30,32,35,38, btnEn.Reset(); while (btnEn.MoveNext()) - Console.Write(btnEn.Current.GetValueOrDefault().Key + ","); + Console.Write(btnEn.Current.Key + ","); Console.WriteLine(); btnEn.Dispose(); // output: 22,24,25,26,27, btnEn = btnTest.GetEnumeratorEx(keyLo, keyHi, false, 5); while (btnEn.MoveNext()) - Console.Write(btnEn.Current.GetValueOrDefault().Key + ","); + Console.Write(btnEn.Current.Key + ","); Console.WriteLine(); btnEn.Dispose(); @@ -110,42 +110,42 @@ static void Main(string[] args) // output: 46,45,42,40,38,35,32,30,27,26,25,24,22,20,18,15,13,10,8,7,5, btnEn = btnTest.GetEnumeratorEx(true); while (btnEn.MoveNext()) - Console.Write(btnEn.Current.GetValueOrDefault().Key + ","); + Console.Write(btnEn.Current.Key + ","); Console.WriteLine(); btnEn.Dispose(); // output: 46,45,42,40,38,35,32,30,27,26,25,24, btnEn = btnTest.GetEnumeratorEx(true, 12); while (btnEn.MoveNext()) - Console.Write(btnEn.Current.GetValueOrDefault().Key + ","); + Console.Write(btnEn.Current.Key + ","); Console.WriteLine(); btnEn.Dispose(); // output: 38,35,32,30,27,26,25,24,22,20,18,15,13,10,8,7,5, - btnEn = btnTest.GetEnumeratorEx(null, keyHi, true); + btnEn = btnTest.GetEnumeratorEx(default(int), keyHi, true); while (btnEn.MoveNext()) - Console.Write(btnEn.Current.GetValueOrDefault().Key + ","); + Console.Write(btnEn.Current.Key + ","); Console.WriteLine(); btnEn.Dispose(); // output: 46,45,42,40,38,35,32,30,27,26,25,24,22, - btnEn = btnTest.GetEnumeratorEx(keyLo, null, true); + btnEn = btnTest.GetEnumeratorEx(keyLo, default(int), true); while (btnEn.MoveNext()) - Console.Write(btnEn.Current.GetValueOrDefault().Key + ","); + Console.Write(btnEn.Current.Key + ","); Console.WriteLine(); btnEn.Dispose(); // output: 38,35,32,30,27,26,25,24,22, btnEn = btnTest.GetEnumeratorEx(keyLo, keyHi, true); while (btnEn.MoveNext()) - Console.Write(btnEn.Current.GetValueOrDefault().Key + ","); + Console.Write(btnEn.Current.Key + ","); Console.WriteLine(); btnEn.Dispose(); // output: 38,35,32,30,27, btnEn = btnTest.GetEnumeratorEx(keyLo, keyHi, true, 5); while (btnEn.MoveNext()) - Console.Write(btnEn.Current.GetValueOrDefault().Key + ","); + Console.Write(btnEn.Current.Key + ","); Console.WriteLine(); btnEn.Dispose(); diff --git a/Samples/DuplicityKeys.Multi.sample/DuplicityKeys.Multi.sample.csproj b/Samples/DuplicityKeys.Multi.sample/DuplicityKeys.Multi.sample.csproj index 287f2e8..d497139 100644 --- a/Samples/DuplicityKeys.Multi.sample/DuplicityKeys.Multi.sample.csproj +++ b/Samples/DuplicityKeys.Multi.sample/DuplicityKeys.Multi.sample.csproj @@ -6,7 +6,7 @@ - + diff --git a/Samples/FcsFastBTreeN.Multi.benchmark/FcsFastBTreeN.Multi.benchmark.csproj b/Samples/FcsFastBTreeN.Multi.benchmark/FcsFastBTreeN.Multi.benchmark.csproj index 83b5bd6..4a2747b 100644 --- a/Samples/FcsFastBTreeN.Multi.benchmark/FcsFastBTreeN.Multi.benchmark.csproj +++ b/Samples/FcsFastBTreeN.Multi.benchmark/FcsFastBTreeN.Multi.benchmark.csproj @@ -6,7 +6,7 @@ - + diff --git a/Samples/FcsInmemStream.Multi.sample/FcsInmemStream.Multi.sample.csproj b/Samples/FcsInmemStream.Multi.sample/FcsInmemStream.Multi.sample.csproj index b006d32..176ddbd 100644 --- a/Samples/FcsInmemStream.Multi.sample/FcsInmemStream.Multi.sample.csproj +++ b/Samples/FcsInmemStream.Multi.sample/FcsInmemStream.Multi.sample.csproj @@ -14,7 +14,7 @@ - + diff --git a/Samples/MultipleKeys.Multi.sample/MultipleKeys.Multi.sample.csproj b/Samples/MultipleKeys.Multi.sample/MultipleKeys.Multi.sample.csproj index 287f2e8..d497139 100644 --- a/Samples/MultipleKeys.Multi.sample/MultipleKeys.Multi.sample.csproj +++ b/Samples/MultipleKeys.Multi.sample/MultipleKeys.Multi.sample.csproj @@ -6,7 +6,7 @@ - +