Пример #1
0
        public Heap(List <int> data, HeapType heapType)
        {
            var cloneData = new List <int>();

            foreach (var item in data)
            {
                cloneData.Add(item);
            }
            switch (heapType)
            {
            case HeapType.Max:
                ReHeapify = new HeapMethod(Heapify.GetMaxHeap);
                break;

            case HeapType.Min:
                ReHeapify = new HeapMethod(Heapify.GetMinHeap);
                break;

            default:
                break;
            }
            Data = ReHeapify(cloneData);
            Tree = BinaryTree <int> .CreateBinaryTree(Data);

            HeapType = heapType;
        }
Пример #2
0
        public HeapItem Alloc(int size, out HeapType type, out int block)
        {
            type  = HeapType.None;
            block = -1;

            if (size <= kSendQueueSmallBlockSize)
            {
                for (int i = 0; i < maxSmallBlocks; i++)
                {
                    if (1 == Interlocked.CompareExchange(ref freeSmallBlocks[i], 0, 1))
                    {
                        type  = HeapType.SmallHeap;
                        block = i;
                        return(new HeapItem(this, smallObjectHeap, HeapType.SmallHeap, i));
                    }
                }

                return(default(HeapItem));
            }

            for (int i = 0; i < maxLargeBlocks; i++)
            {
                if (1 == Interlocked.CompareExchange(ref freeLargeBlocks[i], 0, 1))
                {
                    type  = HeapType.LargeHeap;
                    block = i;
                    return(new HeapItem(this, largeObjectHeap, HeapType.LargeHeap, i));
                }
            }

            return(default(HeapItem));
        }
Пример #3
0
 private static void ValidateHeapTypeValues(HeapType heapType)
 {
     if (Enum.GetValues(typeof(HeapType)).Length > 2)
     {
         throw new Exception();
     }
 }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="metadata"></param>
        public StreamHeader(System.IO.BinaryReader reader, byte[] metadata)
        {
            _offset = reader.ReadInt32();
            _size   = reader.ReadInt32();

            int position = (int)reader.BaseStream.Position;
            int length   = Array.IndexOf <byte>(metadata, 0, position, 32);

            _name = Encoding.ASCII.GetString(metadata, position, length - position);

            if (_name.Equals("#Strings"))
            {
                _kind = HeapType.String;
            }
            else if (_name.Equals("#US"))
            {
                _kind = HeapType.UserString;
            }
            else if (_name.Equals("#Blob"))
            {
                _kind = HeapType.Blob;
            }
            else if (_name.Equals("#GUID"))
            {
                _kind = HeapType.Guid;
            }
            else if (_name.Equals("#~"))
            {
                _kind = HeapType.Tables;
            }
            else
            {
                throw new NotSupportedException("Offset: " + _offset + " \\ Size: " + _size + " \\ Name: " + _name.ToString());
            }
        }
Пример #5
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="metadata"></param>
        public StreamHeader(System.IO.BinaryReader reader, byte[] metadata)
        {
            _offset = reader.ReadInt32();
            _size = reader.ReadInt32();

            int position = (int)reader.BaseStream.Position;
            int length = Array.IndexOf<byte>(metadata, 0, position, 32);
            _name = Encoding.ASCII.GetString(metadata, position, length - position);

            if (_name.Equals("#Strings"))
            {
                _kind = HeapType.String;
            }
            else if (_name.Equals("#US"))
            {
                _kind = HeapType.UserString;
            }
            else if (_name.Equals("#Blob"))
            {
                _kind = HeapType.Blob;
            }
            else if (_name.Equals("#GUID"))
            {
                _kind = HeapType.Guid;
            }
            else if (_name.Equals("#~"))
            {
                _kind = HeapType.Tables;
            }
            else
            {
                throw new NotSupportedException("Offset: " + _offset + " \\ Size: " + _size + " \\ Name: " + _name.ToString());
            }
        }
Пример #6
0
 public Texture(GraphicsDevice device, ResourceDescription description, HeapType heapType) : base(device, description, heapType)
 {
     if (description.Dimension < ResourceDimension.Texture1D)
     {
         throw new ArgumentException();
     }
 }
Пример #7
0
        /// <summary>
        /// Constructs a new <see cref="Heap{T}"/> from an <see cref="IEnumerable{T}"/>
        /// </summary>
        public static Heap <T> ToHeap <T>(this IEnumerable <T> items, HeapType type = HeapType.Min) where T : IComparable <T>
        {
            var heap = new Heap <T>(type);

            heap.AddRange(items);
            return(heap);
        }
Пример #8
0
        /// <summary>
        /// Создание экземпляра класса <see cref="Heap{TKey, TValue}"/>
        /// </summary>
        /// <param name="heapType">Тип кучи</param>
        /// <param name="maxSize">Максимальный размер кучи</param>
        public Heap(HeapType heapType, int maxSize)
        {
            if (maxSize <= 0)
            {
                throw new ArgumentException($"{nameof(maxSize)} should be greater than 0", nameof(maxSize));
            }

            _heap = new ComparableElement <TKey, TValue> [maxSize];
            switch (heapType)
            {
            case HeapType.Min:
                _heapType = new MinHeapType();
                //_isChildCorrect = (index) => index == 0 || _heap[GetParentIndex(index)].Key.CompareTo(_heap[index].Key) <= 0;
                //_elementCandidateSubstitution = (int first, int second) => _heap[first].Key.CompareTo(_heap[second].Key) <= 0 ? first : second;
                break;

            case HeapType.Max:
                _heapType = new MaxHeapType();
                //_isChildCorrect = (index) => index == 0 || _heap[GetParentIndex(index)].Key.CompareTo(_heap[index].Key) >= 0;
                //_elementCandidateSubstitution = (int first, int second) => _heap[first].Key.CompareTo(_heap[second].Key) >= 0 ? first : second;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(heapType), $"unhandled value: {heapType}");
            }
        }
Пример #9
0
 public Heap(int capacity, HeapType heapType)
 {
     this.Capacity = capacity;
     this.Count    = 0;
     this.HeapType = heapType;
     data          = new HeapItem <T> [capacity];
 }
Пример #10
0
 public EasyHeapSmallFixedSize(HeapType hType, uint size, Func <T, object> selector)
 {
     HType         = hType;
     items         = new List <T>();
     heapSize      = size;
     this.selector = selector;
 }
Пример #11
0
 public Heap(HeapType heapType, IEnumerable <KeyValuePair <TKey, TValue> > source) : this(heapType)
 {
     foreach (var pair in source)
     {
         Add(pair.Key, pair.Value);
     }
 }
Пример #12
0
 public BufferDescription(int sizeInBytes, BufferFlags bufferFlags, HeapType heapType, int structureByteStride = 0)
 {
     SizeInBytes         = sizeInBytes;
     Flags               = bufferFlags;
     HeapType            = heapType;
     StructureByteStride = structureByteStride;
 }
Пример #13
0
 public BinaryHeap(int[] inputData, HeapType heapType)
 {
     storage = new int[inputData.Length];
     inputData.CopyTo(storage, 0);
     _heapType = heapType;
     BuildHeap();
 }
Пример #14
0
 public HeapItem(SendQueue queue, byte[] heap, HeapType type, int block)
 {
     this.queue = queue;
     this.HeapType = type;
     this.Heap = heap;
     this.Block = block;
 }
 public CustomerCollection(int capacity, HeapType ht)
 {
     this._heap    = new Customer[capacity + 1];
     this._heap[0] = new Customer(long.MinValue, long.MinValue);
     this._size    = 0;
     this._orderBy = ht;
 }
Пример #16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HeapProperties"/> struct.
 /// </summary>
 /// <param name="type">The type.</param>
 public HeapProperties(HeapType type)
 {
     Type                  = type;
     CPUPageProperty       = CpuPageProperty.Unknown;
     MemoryPoolPreference  = MemoryPool.Unknown;
     this.CreationNodeMask = 1;
     this.VisibleNodeMask  = 1;
 }
Пример #17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HeapProperties"/> struct.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="cpuPageProperty">The cpu page properties.</param>
 /// <param name="memoryPoolPreference">The memory pool preference.</param>
 public HeapProperties(HeapType type, CpuPageProperty cpuPageProperty, MemoryPool memoryPoolPreference)
 {
     Type                  = type;
     CPUPageProperty       = cpuPageProperty;
     MemoryPoolPreference  = memoryPoolPreference;
     this.CreationNodeMask = 1;
     this.VisibleNodeMask  = 1;
 }
Пример #18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HeapProperties"/> struct.
 /// </summary>
 /// <param name="type">The type.</param>
 public HeapProperties(HeapType type)
 {
     Type = type;
     CPUPageProperty = CpuPageProperty.Unknown;
     MemoryPoolPreference = MemoryPool.Unknown;
     this.CreationNodeMask = 1;
     this.VisibleNodeMask = 1;
 }
Пример #19
0
 public Heap(int maxSize, HeapType heapType)
 {
     this.MaxSize     = maxSize;
     this.CurrentSize = 0;
     this.heapType    = heapType;
     this.arr         = new T[this.MaxSize];
     AssignComparer();
 }
Пример #20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HeapProperties"/> struct.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="cpuPageProperty">The cpu page properties.</param>
 /// <param name="memoryPoolPreference">The memory pool preference.</param>
 public HeapProperties(HeapType type, CpuPageProperty cpuPageProperty, MemoryPool memoryPoolPreference)
 {
     Type = type;
     CPUPageProperty = cpuPageProperty;
     MemoryPoolPreference = memoryPoolPreference;
     this.CreationNodeMask = 1;
     this.VisibleNodeMask = 1;
 }
Пример #21
0
 public Heap(T[] array, HeapType heapType)
 {
     Array      = array;
     Length     = array.Length;
     HeapType   = heapType;
     comparator = (heapType == HeapType.MaxHeap) ? 1 : -1;
     BuildHeap();
 }
Пример #22
0
        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="heapSize">heapsize must not bigger than arr.Length</param>
        /// <param name="type"></param>
        public BinaryHeap(T[] arr, int heapSize, HeapType type)
        {
            Debug.Assert(heapSize <= arr.Length);

            HeapType = type;

            Root = new BinaryHeapNode <T>(arr[0]);
            BuildHeap(arr, Root, 0, heapSize);
        }
Пример #23
0
        /// <summary>
        /// Creates an instance of a specific heap type.
        /// </summary>
        /// <param name="provider">The provider buffer, which contains the heap.</param>
        /// <param name="type">The type of the heap to create.</param>
        /// <param name="metadata">The metadata.</param>
        /// <param name="offset">The offset into the buffer, where the heap starts.</param>
        /// <param name="size">The size of the heap in bytes.</param>
        /// <returns>An instance of the requested heap type.</returns>
        /// <exception cref="System.ArgumentException">An invalid heap type was requested.</exception>
        public static Heap CreateHeap(IMetadataProvider provider, HeapType type, byte[] metadata, int offset, int size)
        {
            switch (type)
            {
                case HeapType.String: return new StringHeap(metadata, offset, size);
                case HeapType.Guid: return new GuidHeap(metadata, offset, size);
                case HeapType.Blob: return new BlobHeap(metadata, offset, size);
                case HeapType.UserString: return new UserStringHeap(metadata, offset, size);
                case HeapType.Tables: return new TableHeap(provider, metadata, offset, size);
            }

            throw new ArgumentException(@"Invalid heap type.", @"type");
        }
Пример #24
0
        /// <summary>
        /// Frees a block for re-use.
        /// </summary>
        public void Free(int block, HeapType type)
        {
            if (type == HeapType.LargeHeap)
            {
                FreeLarge(block);
                return;
            }

            Free(block);
        }
Пример #25
0
        /// <summary>
        /// Allocates a new HeapItem based on the first free block on the small
        /// or large heaps.
        /// </summary>
        /// <param name="size">The size of packet requested</param>
        /// <param name="type">(out) Small or large heap</param>
        /// <param name="block">(out) the block ID</param>
        /// <returns>
        /// A HeapItem pointing to a segment of the packet heap in which the
        /// packet may be written to
        /// </returns>
        public HeapItem Alloc(int size, out HeapType type, out int block)
        {
            type = HeapType.None;
            block = -1;

            if (size <= kSendQueueSmallBlockSize)
            {
                for (int i = 0; i < maxSmallBlocks; i++)
                {
                    if (1 == Interlocked.CompareExchange(ref freeSmallBlocks[i], 0, 1))
                    {
                        type = HeapType.SmallHeap;
                        block = i;
                        return new HeapItem(this, smallObjectHeap, HeapType.SmallHeap, i);
                    }
                }

                overflowSmallBlocks += 1;

                return default(HeapItem);
            }

            for (int i = 0; i < maxLargeBlocks; i++)
            {
                if (1 == Interlocked.CompareExchange(ref freeLargeBlocks[i], 0, 1))
                {
                    type = HeapType.LargeHeap;
                    block = i;
                    return new HeapItem(this, largeObjectHeap, HeapType.LargeHeap, i);
                }
            }

            overflowLargeBlocks += 1;

            return default(HeapItem);
        }
Пример #26
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="heapType">Heap type</param>
		/// <param name="reader">Data stream</param>
		/// <param name="baseOffset">Offset in <paramref name="reader"/> of start of data</param>
		protected HotHeapStream(HeapType heapType, IImageStream reader, long baseOffset) {
			this.heapType = heapType;
			this.reader = reader;
			this.baseOffset = baseOffset;
		}
Пример #27
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="heapType">Heap type</param>
		/// <param name="reader">Data stream</param>
		/// <param name="baseOffset">Offset in <paramref name="reader"/> of start of data</param>
		public HotHeapStreamCLR40(HeapType heapType, IImageStream reader, long baseOffset)
			: base(heapType, reader, baseOffset) {
		}
Пример #28
0
		/// <summary>
		/// Reads a hot heap directory
		/// </summary>
		/// <param name="reader">Reader stream</param>
		/// <param name="dirBaseOffs">Pool directory base offset</param>
		/// <param name="heapType">Updated with heap type</param>
		/// <param name="hotHeapOffs">Updated with heap offset</param>
		protected abstract void ReadHotHeapDirectory(IImageStream reader, long dirBaseOffs, out HeapType heapType, out long hotHeapOffs);
Пример #29
0
 /// <summary>
 /// Gets the heaps of a specified type
 /// </summary>
 /// <param name="heapType">Type of the heap.</param>
 /// <returns></returns>
 IList<Heap> IMetadataProvider.GetHeaps(HeapType heapType)
 {
     List<Heap> list = new List<Heap>();
     list.Add(_streams[(int)heapType]);
     return list;
 }
Пример #30
0
		/// <summary>
		/// Creates a <see cref="HotPool"/> instance
		/// </summary>
		/// <param name="heapType">Pool type</param>
		public abstract HotPool CreateHotPool(HeapType heapType);
Пример #31
0
		/// <inheritdoc/>
		public override HotPool CreateHotPool(HeapType heapType) {
			return new HotPool40(heapType);
		}
Пример #32
0
		/// <inheritdoc/>
		protected override void ReadHotHeapDirectory(IImageStream reader, long dirBaseOffs, out HeapType heapType, out long hotHeapOffs) {
			heapType = (HeapType)reader.ReadUInt32();
			// All bits are used
			hotHeapOffs = dirBaseOffs - reader.ReadUInt32();
		}
Пример #33
0
		/// <summary>
		/// Creates a <see cref="HotHeapStream"/>
		/// </summary>
		/// <param name="heapType">Heap type</param>
		/// <param name="stream">Data stream</param>
		/// <param name="baseOffset">Offset in <paramref name="stream"/> of start of data</param>
		/// <returns>A new <see cref="HotHeapStream"/> instance</returns>
		protected abstract HotHeapStream CreateHotHeapStream(HeapType heapType, IImageStream stream, long baseOffset);
Пример #34
0
		/// <inheritdoc/>
		protected override HotHeapStream CreateHotHeapStream(HeapType heapType, IImageStream stream, long baseOffset) {
			return new HotHeapStreamCLR40(heapType, stream, baseOffset);
		}
Пример #35
0
        /// <summary>
        /// Retrieves the requested provider heap.
        /// </summary>
        /// <param name="type">The requested provider heap.</param>
        /// <returns>The provider heap requested.</returns>
        /// <exception cref="System.ArgumentException"><paramref name="type"/> is invalid.</exception>
        public Heap GetHeap(HeapType type)
        {
            if (0 > type || type >= HeapType.MaxType)
                throw new ArgumentException(@"Invalid heap type.", @"type");

            return _streams[(int)type];
        }
Пример #36
0
		/// <inheritdoc/>
		protected override void ReadHotHeapDirectory(IImageStream reader, long dirBaseOffs, out HeapType heapType, out long hotHeapOffs) {
			heapType = (HeapType)reader.ReadUInt32();
			// Lower 2 bits are ignored
			hotHeapOffs = dirBaseOffs - (reader.ReadUInt32() & ~3);
		}