/// <summary> /// Initializes a new instance of the <see cref="FixedSizeBucket{T}" /> class. /// </summary> public FixedSizeBucket(IEnumerable <T> source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } var collection = source as ICollection <T>; _entries = ArrayReservoir <object> .GetArray(collection?.Count ?? 64); Capacity = _entries.Length; foreach (var item in source) { if (_count == Capacity) { var old = _entries; _entries = ArrayReservoir <object> .GetArray(Capacity << 1); if (old != null) { Array.Copy(old, 0, _entries, 0, _count); ArrayReservoir <object?> .DonateArray(old); } Capacity = _entries.Length; } _entries[_count] = (object?)item ?? BucketHelper.Null; _count++; } }
private Branch[] Map(uint index, out int resultCount) { var result = ArrayReservoir <Branch> .GetArray(16); var branch = this; resultCount = 0; while (true) { Interlocked.Increment(ref branch._useCount); result[resultCount] = branch; resultCount++; // do we need a leaf? if (branch._offset == 0) { // It is not responsability of this method to handle leafs return(result); } object found; if (branch.PrivateTryGetBranch(index, out found)) { // if found were null, PrivateTryGetBranch would have returned false // if found cannot be a leaf, because Leaf only appear on _offset == 0 // only Branch and Leaf are inserted, ergo... found is Branch branch = (Branch)found; continue; } branch = branch.Grow(index); } }
/// <summary> /// Initializes a new instance of the <see cref="FixedSizeBucket{T}" /> class. /// </summary> /// <param name="capacity">The capacity.</param> public FixedSizeBucket(int capacity) { _count = 0; _entries = ArrayReservoir <object> .GetArray(capacity); Capacity = _entries.Length; }
private BucketCore(SerializationInfo info, StreamingContext context) { _ = context; if ( info.GetValue("childFactory", typeof(Func <object>)) is not Func <object> childFactory || info.GetValue("level", typeof(int)) is not int level || info.GetValue("contents", typeof(object?[])) is not object?[] contents ) { throw new SerializationException(); } _childFactory = childFactory; _level = level; _arrayFirst = ArrayReservoir <object> .GetArray(_capacity); _arraySecond = ArrayReservoir <object> .GetArray(_capacity); _arrayUse = ArrayReservoir <int> .GetArray(_capacity); for (int subIndex = 0; subIndex < Math.Min(_capacity, contents.Length); subIndex++) { _arrayFirst[subIndex] = contents[subIndex]; _arraySecond[subIndex] = contents[subIndex]; _arrayUse[subIndex] = 1; } }
private BucketCore(int level) { _level = level; _arrayFirst = ArrayReservoir <object> .GetArray(_capacity); _arraySecond = ArrayReservoir <object> .GetArray(_capacity); _arrayUse = ArrayReservoir <int> .GetArray(_capacity); }
private Branch(int offset, Branch parent, int subindex) { _offset = offset; _parent = parent; _subindex = subindex; _entries = ArrayReservoir <object> .GetArray(INT_Capacity); _buffer = ArrayReservoir <object> .GetArray(INT_Capacity); }
private BucketCore(int level) { _childFactory = level == 1 ? FuncHelper.GetDefaultFunc <object>() : () => new BucketCore(_level - 1); _level = level; _arrayFirst = ArrayReservoir <object> .GetArray(_capacity); _arraySecond = ArrayReservoir <object> .GetArray(_capacity); _arrayUse = ArrayReservoir <int> .GetArray(_capacity); }
/// <summary> /// Initializes a new instance of the <see cref="FixedSizeBucket{T}" /> class. /// </summary> public FixedSizeBucket(T[] source) { if (source == null) { throw new ArgumentNullException("source"); } _entries = ArrayReservoir <object> .GetArray(source.Length); _capacity = _entries.Length; foreach (var item in source) { _entries[_count] = (object)item ?? BucketHelper.Null; _count++; } }
public static Branch Create(int offset, Branch parent, int subindex) { Branch result; if (_branchPool.TryGet(out result)) { result._offset = offset; result._parent = parent; result._subindex = subindex; result._entries = ArrayReservoir <object> .GetArray(INT_Capacity); result._buffer = ArrayReservoir <object> .GetArray(INT_Capacity); return(result); } return(new Branch(offset, parent, subindex)); }