示例#1
0
        /// <summary>
        /// Deserializes a huge collection index from the given stream.
        /// </summary>
        /// <param name="stream">The source stream.</param>
        /// <param name="copy">The copy flag. When true all data is copied into memory, otherwise the source-stream is used as a memorymapped file.</param>
        /// <returns></returns>
        public static HugeCoordinateCollectionIndex Deserialize(Stream stream, bool copy = false)
        {
            // read sizes.
            long position  = 0;
            var  longBytes = new byte[8];

            stream.Read(longBytes, 0, 8);
            position = position + 8;
            var indexLength = BitConverter.ToInt64(longBytes, 0);

            stream.Read(longBytes, 0, 8);
            position = position + 8;
            var coordinateLength = BitConverter.ToInt64(longBytes, 0);

            var file            = new MemoryMappedStream(new LimitedStream(stream));
            var indexArray      = new MemoryMappedHugeArrayUInt64(file, indexLength, indexLength, 1024);
            var coordinateArray = new MemoryMappedHugeArraySingle(file, coordinateLength * 2, coordinateLength * 2, 1024);

            if (copy)
            { // copy the data.
                var indexArrayCopy = new HugeArray <ulong>(indexLength);
                indexArrayCopy.CopyFrom(indexArray);

                var coordinateArrayCopy = new HugeArray <float>(coordinateLength * 2);
                coordinateArrayCopy.CopyFrom(coordinateArray);

                file.Dispose();

                return(new HugeCoordinateCollectionIndex(indexLength, indexArrayCopy, coordinateArrayCopy));
            }

            return(new HugeCoordinateCollectionIndex(indexLength, indexArray, coordinateArray));
        }
示例#2
0
        /// <summary>
        /// Serializes this huge collection index to the given stream.
        /// </summary>
        /// <param name="stream"></param>
        public long Serialize(Stream stream)
        {
            // first trim to minimize the amount of useless data store.
            this.Trim();

            // start writing.
            long indexSize        = _index.Length;
            long coordinatesCount = _coordinates.Length;

            long position = 0;

            stream.Write(BitConverter.GetBytes(indexSize), 0, 8);        // write the actual index size.
            position = position + 8;
            stream.Write(BitConverter.GetBytes(coordinatesCount), 0, 8); // write the actual number of coordinates.
            position = position + 8;

            // write in this order: index, shapes.
            using (var file = new MemoryMappedStream(new LimitedStream(stream)))
            {
                // write index.
                var indexArray = new MemoryMappedHugeArrayUInt64(file, indexSize, indexSize, 1024);
                indexArray.CopyFrom(_index, indexSize);
                indexArray.Dispose();
                position = position + (indexSize * 8);

                // write coordinates.
                var coordinatesArray = new MemoryMappedHugeArraySingle(file, coordinatesCount * 2, coordinatesCount * 2, 1024);
                coordinatesArray.CopyFrom(_coordinates);
                coordinatesArray.Dispose();
                position = position + (coordinatesCount * 2 * 4);
            }

            return(position);
        }
        /// <summary>
        /// Serializes this huge collection index to the given stream.
        /// </summary>
        /// <param name="stream"></param>
        public long Serialize(Stream stream)
        {
            // first trim to minimize the amount of useless data store.
            this.Trim();

            // start writing.
            long indexSize = _index.Length;
            long coordinatesCount = _coordinates.Length;

            long position = 0;
            stream.Write(BitConverter.GetBytes(indexSize), 0, 8); // write the actual index size.
            position = position + 8;
            stream.Write(BitConverter.GetBytes(coordinatesCount), 0, 8); // write the actual number of coordinates.
            position = position + 8;

            // write in this order: index, shapes.
            using (var file = new MemoryMappedStream(new LimitedStream(stream)))
            {
                // write index.
                var indexArray = new MemoryMappedHugeArrayUInt64(file, indexSize, indexSize, 1024);
                indexArray.CopyFrom(_index, indexSize);
                indexArray.Dispose();
                position = position + (indexSize * 8);

                // write coordinates.
                var coordinatesArray = new MemoryMappedHugeArraySingle(file, coordinatesCount * 2, coordinatesCount * 2, 1024);
                coordinatesArray.CopyFrom(_coordinates);
                coordinatesArray.Dispose();
                position = position + (coordinatesCount * 2 * 4);
            }

            return position;
        }
        /// <summary>
        /// Deserializes a huge collection index from the given stream.
        /// </summary>
        /// <param name="stream">The source stream.</param>
        /// <param name="copy">The copy flag. When true all data is copied into memory, otherwise the source-stream is used as a memorymapped file.</param>
        /// <returns></returns>
        public static HugeCoordinateCollectionIndex Deserialize(Stream stream, bool copy = false)
        {
            // read sizes.
            long position = 0;
            var longBytes = new byte[8];
            stream.Read(longBytes, 0, 8);
            position = position + 8;
            var indexLength = BitConverter.ToInt64(longBytes, 0);
            stream.Read(longBytes, 0, 8);
            position = position + 8;
            var coordinateLength = BitConverter.ToInt64(longBytes, 0);

            var file = new MemoryMappedStream(new LimitedStream(stream));
            var indexArray = new MemoryMappedHugeArrayUInt64(file, indexLength, indexLength, 1024);
            var coordinateArray = new MemoryMappedHugeArraySingle(file, coordinateLength * 2, coordinateLength * 2, 1024);

            if (copy)
            { // copy the data.
                var indexArrayCopy = new HugeArray<ulong>(indexLength);
                indexArrayCopy.CopyFrom(indexArray);

                var coordinateArrayCopy = new HugeArray<float>(coordinateLength * 2);
                coordinateArrayCopy.CopyFrom(coordinateArray);

                file.Dispose();

                return new HugeCoordinateCollectionIndex(indexLength, indexArrayCopy, coordinateArrayCopy);
            }

            return new HugeCoordinateCollectionIndex(indexLength, indexArray, coordinateArray);
        }