private static ComponentEntry GetEntry(object component, Type componentType) { ComponentEntry entry = null; lock(cachedComponentEntries) { entry = (ComponentEntry)cachedComponentEntries[componentType]; if (entry == null) { entry = new ComponentEntry(componentType); cachedComponentEntries[componentType] = entry; } } return entry; }
/// <summary> /// Same with {@link #slice(int, int)} except that this method returns a list. /// </summary> public IList <IByteBuffer> Decompose(int offset, int length) { this.CheckIndex(offset, length); if (length == 0) { return(new ReadOnlyCollection <IByteBuffer>(new IByteBuffer[0])); // todo: optimize } int componentId = this.ToComponentIndex(offset); var slice = new List <IByteBuffer>(this.components.Count); // The first component ComponentEntry firstC = this.components[componentId]; IByteBuffer first = firstC.Buffer.Duplicate(); first.SetReaderIndex(offset - firstC.Offset); IByteBuffer buffer = first; int bytesToSlice = length; do { int readableBytes = buffer.ReadableBytes; if (bytesToSlice <= readableBytes) { // Last component buffer.SetWriterIndex(buffer.ReaderIndex + bytesToSlice); slice.Add(buffer); break; } else { // Not the last component slice.Add(buffer); bytesToSlice -= readableBytes; componentId++; // Fetch the next component. buffer = this.components[componentId].Buffer.Duplicate(); } }while (bytesToSlice > 0); // Slice all components because only readable bytes are interesting. for (int i = 0; i < slice.Count; i++) { slice[i] = slice[i].Slice(); } return(slice); }
/// <summary> /// Remove the {@link IByteBuffer} from the given index. /// @param cIndex the index on from which the {@link IByteBuffer} will be remove /// </summary> public CompositeByteBuffer RemoveComponent(int cIndex) { this.CheckComponentIndex(cIndex); ComponentEntry comp = this.components[cIndex]; this.components.RemoveAt(cIndex); comp.FreeIfNecessary(); if (comp.Length > 0) { // Only need to call updateComponentOffsets if the length was > 0 this.UpdateComponentOffsets(cIndex); } return(this); }
public override async Task <int> SetBytesAsync(int index, Stream src, int length, CancellationToken cancellationToken) { this.CheckIndex(index, length); if (length == 0) { return(0); //return src.Read(EmptyArrays.EMPTY_BYTES); } int i = this.ToComponentIndex(index); int readBytes = 0; do { ComponentEntry c = this.components[i]; IByteBuffer s = c.Buffer; int adjustment = c.Offset; int localLength = Math.Min(length, s.Capacity - (index - adjustment)); int localReadBytes = await s.SetBytesAsync(index - adjustment, src, localLength, cancellationToken); if (localReadBytes < 0) { if (readBytes == 0) { return(-1); } else { break; } } if (localReadBytes == localLength) { index += localLength; length -= localLength; readBytes += localLength; i++; } else { index += localReadBytes; length -= localReadBytes; readBytes += localReadBytes; } }while (length > 0); return(readBytes); }
protected override short _GetShort(int index) { ComponentEntry c = this.FindComponent(index); if (index + 2 <= c.EndOffset) { return(c.Buffer.GetShort(index - c.Offset)); } else if (this.Order == ByteOrder.BigEndian) { return((short)(this._GetByte(index) << 8 | this._GetByte(index + 1))); } else { return((short)(this._GetByte(index) | this._GetByte(index + 1) << 8)); } }
protected override int _GetInt(int index) { ComponentEntry c = this.FindComponent(index); if (index + 4 <= c.EndOffset) { return(c.Buffer.GetInt(index - c.Offset)); } else if (this.Order == ByteOrder.BigEndian) { return(this._GetShort(index) << 16 | (ushort)this._GetShort(index + 2)); } else { return((ushort)this._GetShort(index) | this._GetShort(index + 2) << 16); } }
protected override long _GetLong(int index) { ComponentEntry c = this.FindComponent(index); if (index + 8 <= c.EndOffset) { return(c.Buffer.GetLong(index - c.Offset)); } else if (this.Order == ByteOrder.BigEndian) { return((long)this._GetInt(index) << 32 | (uint)this._GetInt(index + 4)); } else { return((uint)this._GetInt(index) | (((long)this._GetInt(index + 4)) << 32)); } }
protected override void _SetMedium(int index, int value) { ComponentEntry c = this.FindComponent(index); if (index + 3 <= c.EndOffset) { c.Buffer.SetMedium(index - c.Offset, value); } else if (this.Order == ByteOrder.BigEndian) { this._SetShort(index, (short)(value >> 8)); this._SetByte(index + 2, (byte)value); } else { this._SetShort(index, (short)value); this._SetByte(index + 2, (byte)(value.RightUShift(16))); } }
protected override void _SetInt(int index, int value) { ComponentEntry c = this.FindComponent(index); if (index + 4 <= c.EndOffset) { c.Buffer.SetInt(index - c.Offset, value); } else if (this.Order == ByteOrder.BigEndian) { this._SetShort(index, (short)((uint)value >> 16)); this._SetShort(index + 2, (short)value); } else { this._SetShort(index, (short)value); this._SetShort(index + 2, (short)((uint)value >> 16)); } }
protected override void _SetLong(int index, long value) { ComponentEntry c = this.FindComponent(index); if (index + 8 <= c.EndOffset) { c.Buffer.SetLong(index - c.Offset, value); } else if (this.Order == ByteOrder.BigEndian) { this._SetInt(index, (int)((ulong)value >> 32)); this._SetInt(index + 4, (int)value); } else { this._SetInt(index, (int)value); this._SetInt(index + 4, (int)((ulong)value >> 32)); } }
/// <summary> /// Discard all {@link IByteBuffer}s which are read. /// </summary> public CompositeByteBuffer DiscardReadComponents() { this.EnsureAccessible(); int readerIndex = this.ReaderIndex; if (readerIndex == 0) { return(this); } // Discard everything if (readerIndex = writerIndex = capacity). int writerIndex = this.WriterIndex; if (readerIndex == writerIndex && writerIndex == this.Capacity) { foreach (ComponentEntry c in this.components) { c.FreeIfNecessary(); } this.components.Clear(); this.SetIndex(0, 0); this.AdjustMarkers(readerIndex); return(this); } // Remove read components. int firstComponentId = this.ToComponentIndex(readerIndex); for (int i = 0; i < firstComponentId; i++) { this.components[i].FreeIfNecessary(); } this.components.RemoveRange(0, firstComponentId); // Update indexes and markers. ComponentEntry first = this.components[0]; int offset = first.Offset; this.UpdateComponentOffsets(0); this.SetIndex(readerIndex - offset, writerIndex - offset); this.AdjustMarkers(offset); return(this); }
protected internal override Span <byte> _GetSpan(int index, int count) { if (0u >= (uint)count) { return(Span <byte> .Empty); } switch (_componentCount) { case 0: return(Span <byte> .Empty); case 1: ComponentEntry c = _components[0]; return(c.Buffer.GetSpan(index, count)); default: throw ThrowHelper.GetNotSupportedException(); } }
void CopyTo(int index, int length, int componentId, IByteBuffer dst) { int dstIndex = 0; int i = componentId; while (length > 0) { ComponentEntry c = this.components[i]; IByteBuffer s = c.Buffer; int adjustment = c.Offset; int localLength = Math.Min(length, s.Capacity - (index - adjustment)); s.GetBytes(index - adjustment, dst, dstIndex, localLength); index += localLength; dstIndex += localLength; length -= localLength; i++; } dst.SetWriterIndex(dst.Capacity); }
protected internal override ReadOnlySpan <byte> _GetReadableSpan(int index, int count) { if (0u >= (uint)count) { return(ReadOnlySpan <byte> .Empty); } switch (_componentCount) { case 0: return(ReadOnlySpan <byte> .Empty); case 1: //ComponentEntry c = _components[0]; //return c.Buffer.GetReadableSpan(index, count); ComponentEntry c = _components[0]; IByteBuffer buf = c.Buffer; if (buf.IsSingleIoBuffer) { return(buf.GetReadableSpan(c.Idx(index), count)); } break; } var merged = new Memory <byte>(new byte[count]); var buffers = GetSequence(index, count); int offset = 0; foreach (var buf in buffers) { Debug.Assert(merged.Length - offset >= buf.Length); buf.CopyTo(merged.Slice(offset)); offset += buf.Length; } return(merged.Span); }
protected internal override void _GetBytes(int index, Memory <byte> destination, int length) { CheckIndex(index, length); if (0u >= (uint)length) { return; } var srcIndex = 0; int i = ToComponentIndex0(index); while (length > 0) { ComponentEntry c = _components[i]; int localLength = Math.Min(length, c.EndOffset - index); _ = c.Buffer.GetBytes(c.Idx(index), destination.Slice(srcIndex, localLength)); index += localLength; srcIndex += localLength; length -= localLength; i++; } }
public override ArraySegment <byte>[] GetIoBuffers(int index, int length) { this.CheckIndex(index, length); if (length == 0) { return(new[] { EmptyNioBuffer }); } var buffers = new List <ArraySegment <byte> >(this.components.Count); int i = this.ToComponentIndex(index); while (length > 0) { ComponentEntry c = this.components[i]; IByteBuffer s = c.Buffer; int adjustment = c.Offset; int localLength = Math.Min(length, s.Capacity - (index - adjustment)); switch (s.IoBufferCount) { case 0: throw new NotSupportedException(); case 1: buffers.Add(s.GetIoBuffer(index - adjustment, localLength)); break; default: buffers.AddRange(s.GetIoBuffers(index - adjustment, localLength)); break; } index += localLength; length -= localLength; i++; } return(buffers.ToArray()); }
ReadOnlyCompositeByteBuffer AddComponent(IByteBuffer buffer) { int readableBytes = buffer.ReadableBytes; var c = new ComponentEntry(buffer.Slice()); this.Components.Add(c); int count = this.Components.Count; if (count == 1) //第一个 { c.Offset = 0; c.EndOffset = readableBytes; } else { ComponentEntry prev = this.Components[count - 2]; c.Offset = prev.EndOffset; c.EndOffset = c.Offset + readableBytes; } return(this); }
public override IByteBuffer GetBytes(int index, Stream destination, int length) { this.CheckIndex(index, length); if (length == 0) { return(this); } int i = this.ToComponentIndex(index); while (length > 0) { ComponentEntry c = this.components[i]; IByteBuffer s = c.Buffer; int adjustment = c.Offset; int localLength = Math.Min(length, s.Capacity - (index - adjustment)); s.GetBytes(index - adjustment, destination, localLength); index += localLength; length -= localLength; i++; } return(this); }
private void CreateButtons(HierarchyComponentFolder folder) { int sortIndex = 1; int sortLength = (folder.Entries.Count + NavigationElements.Count + sortIndex).ToString().Length; var subfolders = (from entry in folder.Entries where entry is HierarchyComponentFolder select entry).ToList(); var files = (from entry in folder.Entries where entry is HierarchyComponentFile select entry).ToList(); subfolders.Sort((x, y) => string.Compare(x.Name, y.Name)); files.Sort((x, y) => string.Compare(x.Name, y.Name)); if (Current.Parent != null) { CreateEntries(NavigationElements, new Color(0.68f, 0.83f, 0.38f)); } CreateEntries(subfolders, Colors.BlueCyanColors.lightSkyBlue); CreateEntries(files, Colors.white); void CreateEntries(List <HierarchyComponentData> entries, Color color) { foreach (var entry in entries) { string sort = sortIndex.ToString(); while (sort.Length < sortLength) { sort = $"0{sort}"; } ComponentEntry componentEntry = ComponentEntry.CreateEntry(entry, ClickOnEntry, DoubleClickOnEntry, true, color); componentEntry.sortString_ = sort; buttonList_.Add(componentEntry); ++sortIndex; } } }
public override IByteBuffer SetBytes(int index, IByteBuffer src, int srcIndex, int length) { this.CheckSrcIndex(index, length, srcIndex, src.Capacity); if (length == 0) { return(this); } int i = this.ToComponentIndex(index); while (length > 0) { ComponentEntry c = this.components[i]; IByteBuffer s = c.Buffer; int adjustment = c.Offset; int localLength = Math.Min(length, s.Capacity - (index - adjustment)); s.SetBytes(index - adjustment, src, srcIndex, localLength); index += localLength; srcIndex += localLength; length -= localLength; i++; } return(this); }
/// <summary> /// Return the index for the given offset /// </summary> public int ToComponentIndex(int offset) { this.CheckIndex(offset); for (int low = 0, high = this.components.Count; low <= high;) { int mid = (low + high).RightUShift(1); ComponentEntry c = this.components[mid]; if (offset >= c.EndOffset) { low = mid + 1; } else if (offset < c.Offset) { high = mid - 1; } else { return(mid); } } throw new Exception("should not reach here"); }
private void DisplayComponent(ComponentEntry entry) { GUILayout.BeginHorizontal(); if (Elements.Tools.DoCollapseArrow(entry.IsExpanded)) { entry.IsExpanded = !entry.IsExpanded; } GUILayout.TextField(entry.Component.GetType().Name, Elements.Labels.LogEntry); GUILayout.EndHorizontal(); if (entry.IsExpanded) { ShowFields("Properties", entry.Properties); ShowFields("Fields", entry.Fields); } }
public void Reset() { Component = null; Previous = null; Next = null; }
public void ClickOnEntry(UIExButtonContainer.Entry e) { ComponentEntry entry = e as ComponentEntry; SetLevelPathInput(entry.item_.Path); }
public override IByteBuffer DiscardReadBytes() { this.EnsureAccessible(); int readerIndex = this.ReaderIndex; if (readerIndex == 0) { return this; } // Discard everything if (readerIndex = writerIndex = capacity). int writerIndex = this.WriterIndex; if (readerIndex == writerIndex && writerIndex == this.Capacity) { foreach (ComponentEntry c1 in this.components) { c1.FreeIfNecessary(); } this.components.Clear(); this.SetIndex(0, 0); this.AdjustMarkers(readerIndex); return this; } // Remove read components. int firstComponentId = this.ToComponentIndex(readerIndex); for (int i = 0; i < firstComponentId; i++) { this.components[i].FreeIfNecessary(); } this.components.RemoveRange(0, firstComponentId); // Remove or replace the first readable component with a new slice. ComponentEntry c = this.components[0]; int adjustment = readerIndex - c.Offset; if (adjustment == c.Length) { // new slice would be empty, so remove instead this.components.RemoveAt(0); } else { var newC = new ComponentEntry(c.Buffer.Slice(adjustment, c.Length - adjustment)); this.components[0] = newC; } // Update indexes and markers. this.UpdateComponentOffsets(0); this.SetIndex(0, writerIndex - readerIndex); this.AdjustMarkers(readerIndex); return this; }
void AddComponent0(int cIndex, IByteBuffer buffer) { Contract.Requires(buffer != null); this.CheckComponentIndex(cIndex); int readableBytes = buffer.ReadableBytes; // No need to consolidate - just add a component to the list. var c = new ComponentEntry(buffer.WithOrder(ByteOrder.BigEndian).Slice()); if (cIndex == this.components.Count) { this.components.Add(c); if (cIndex == 0) { c.EndOffset = readableBytes; } else { ComponentEntry prev = this.components[cIndex - 1]; c.Offset = prev.EndOffset; c.EndOffset = c.Offset + readableBytes; } } else { this.components.Insert(cIndex, c); if (readableBytes != 0) { this.UpdateComponentOffsets(cIndex); } } }
/// <summary> /// This should only be called as last operation from a method as this may adjust the underlying /// array of components and so affect the index etc. /// </summary> void ConsolidateIfNeeded() { // Consolidate if the number of components will exceed the allowed maximum by the current // operation. int numComponents = this.components.Count; if (numComponents > this.maxNumComponents) { int capacity = this.components[numComponents - 1].EndOffset; IByteBuffer consolidated = this.AllocateBuffer(capacity); // We're not using foreach to avoid creating an iterator. for (int i = 0; i < numComponents; i++) { ComponentEntry c1 = this.components[i]; IByteBuffer b = c1.Buffer; consolidated.WriteBytes(b); c1.FreeIfNecessary(); } var c = new ComponentEntry(consolidated); c.EndOffset = c.Length; this.components.Clear(); this.components.Add(c); } }
protected override byte _GetByte(int index) { ComponentEntry c = this.FindComponent(index); return(c.Buffer.GetByte(index - c.Offset)); }
public override IByteBuffer AdjustCapacity(int newCapacity) { this.EnsureAccessible(); Contract.Requires(newCapacity >= 0 && newCapacity <= this.MaxCapacity); int oldCapacity = this.Capacity; if (newCapacity > oldCapacity) { int paddingLength = newCapacity - oldCapacity; IByteBuffer padding; int nComponents = this.components.Count; if (nComponents < this.maxNumComponents) { padding = this.AllocateBuffer(paddingLength); padding.SetIndex(0, paddingLength); this.AddComponent0(this.components.Count, padding); } else { padding = this.AllocateBuffer(paddingLength); padding.SetIndex(0, paddingLength); // FIXME: No need to create a padding buffer and consolidate. // Just create a big single buffer and put the current content there. this.AddComponent0(this.components.Count, padding); this.ConsolidateIfNeeded(); } } else if (newCapacity < oldCapacity) { int bytesToTrim = oldCapacity - newCapacity; for (int i = this.components.Count - 1; i >= 0; i--) { ComponentEntry c = this.components[i]; if (bytesToTrim >= c.Length) { bytesToTrim -= c.Length; this.components.RemoveAt(i); continue; } // Replace the last component with the trimmed slice. var newC = new ComponentEntry(c.Buffer.Slice(0, c.Length - bytesToTrim)); newC.Offset = c.Offset; newC.EndOffset = newC.Offset + newC.Length; this.components[i] = newC; break; } if (this.ReaderIndex > newCapacity) { this.SetIndex(newCapacity, newCapacity); } else if (this.WriterIndex > newCapacity) { this.SetWriterIndex(newCapacity); } } return this; }
public MemberList(ComponentEntry owner) { this.owner = owner; }
protected internal override ReadOnlySequence <byte> _GetSequence(int index, int count) { if (0u >= (uint)count) { return(ReadOnlySequence <byte> .Empty); } int i = ToComponentIndex0(index); if (i == ToComponentIndex0(index + count - 1)) { ComponentEntry c = _components[i]; return(c.Buffer.GetSequence(c.Idx(index), count)); } var buffers = ThreadLocalList <ReadOnlyMemory <byte> > .NewInstance(_componentCount); try { while (count > 0) { ComponentEntry c = _components[i]; IByteBuffer s = c.Buffer; int localLength = Math.Min(count, c.EndOffset - index); switch (s.IoBufferCount) { case 0: ThrowHelper.ThrowNotSupportedException(); break; case 1: if (s.IsSingleIoBuffer) { buffers.Add(s.GetReadableMemory(c.Idx(index), localLength)); } else { var sequence0 = s.GetSequence(c.Idx(index), localLength); foreach (var memory in sequence0) { buffers.Add(memory); } } break; default: var sequence = s.GetSequence(c.Idx(index), localLength); foreach (var memory in sequence) { buffers.Add(memory); } break; } index += localLength; count -= localLength; i++; } return(ReadOnlyBufferSegment.Create(buffers)); } finally { buffers.Return(); } }