Exemplo n.º 1
0
        /// <summary>
        /// Get the subsampled data in this storage
        /// </summary>
        /// <param name="subsampleRate">The subsample rate</param>
        /// <returns>The sub-sampled data in this storage</returns>
        public IEnumerable <T> GetSubsamples(int subsampleRate)
        {
            if (!_fileInfo.Exists)
            {
                yield break;
            }

            int bufferSize = _elementSize * subsampleRate;

            using (FileStream stream = _fileInfo.OpenRead())
#if !NETSTANDARD1_4
                using (BufferedStream bufferStream = new BufferedStream(stream, _trunkSize))
#endif
                using (PinnedArray <Byte> buffer = new PinnedArray <byte>(bufferSize))
                    using (PinnedArray <T> structure = new PinnedArray <T>(subsampleRate))
                    {
#if NETSTANDARD1_4
                        var bufferStream = stream;
#endif
                        IntPtr structAddr = structure.AddrOfPinnedObject();
                        IntPtr addr       = buffer.AddrOfPinnedObject();
                        while (bufferStream.Read(buffer.Array, 0, bufferSize) > 0)
                        {
                            CvToolbox.Memcpy(structAddr, addr, bufferSize);
                            yield return(structure.Array[0]);
                        }
                    }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get a copy of the first element in the storage. If the storage is empty, a default value will be returned
        /// </summary>
        /// <returns>A copy of the first element in the storage. If the storage is empty, a default value will be returned</returns>
        public T Peek()
        {
            using (FileStream stream = _fileInfo.OpenRead())
                using (PinnedArray <Byte> buffer = new PinnedArray <byte>(_elementSize))
                {
                    return((stream.Read(buffer.Array, 0, _elementSize) > 0) ?
#if NETSTANDARD1_4
                           Marshal.PtrToStructure <T>(buffer.AddrOfPinnedObject())
#else
                           (T)Marshal.PtrToStructure(buffer.AddrOfPinnedObject(), typeof(T))
#endif
               :

                           new T());
                }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get the data in this storage
        /// </summary>
        /// <returns>The data in this storage</returns>
        public IEnumerator <T> GetEnumerator()
        {
            if (!_fileInfo.Exists)
            {
                yield break;
            }

            int elementsInTrunk = _trunkSize / _elementSize;

            if (elementsInTrunk <= 0)
            {
                elementsInTrunk = 1;
            }

            Byte[] buffer = new byte[_elementSize * elementsInTrunk];

            using (FileStream stream = _fileInfo.OpenRead())
                using (BufferedStream bufferStream = new BufferedStream(stream, _trunkSize))
                    using (PinnedArray <T> structures = new PinnedArray <T>(elementsInTrunk))
                    {
                        IntPtr structAddr = structures.AddrOfPinnedObject();
                        int    bytesRead;
                        while ((bytesRead = bufferStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            Marshal.Copy(buffer, 0, structAddr, bytesRead);
                            int elementsRead = bytesRead / _elementSize;
                            for (int i = 0; i < elementsRead; i++)
                            {
                                yield return(structures.Array[i]);
                            }
                        }
                    }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Get a copy of the first element in the storage. If the storage is empty, a default value will be returned
 /// </summary>
 /// <returns>A copy of the first element in the storage. If the storage is empty, a default value will be returned</returns>
 public T Peek()
 {
     using (FileStream stream = _fileInfo.OpenRead())
         using (PinnedArray <Byte> buffer = new PinnedArray <byte>(_elementSize))
         {
             return((stream.Read(buffer.Array, 0, _elementSize) > 0) ?
                    Marshal.PtrToStructure <T>(buffer.AddrOfPinnedObject()) : new T());
         }
 }
Exemplo n.º 5
0
        void Append(IEnumerable <T> samples)
        {
            int elementsInTrunk = _trunkSize / _elementSize;

            if (elementsInTrunk <= 0)
            {
                elementsInTrunk = 1;
            }

            byte[] byteBuffer = new byte[elementsInTrunk * _elementSize];
            int    index      = 0;

#if NETFX_CORE
            StorageFile storageFile = await StorageFile.GetFileFromPathAsync(_fileInfo);

            using (PinnedArray <T> buffer = new PinnedArray <T>(elementsInTrunk))
                using (Stream bufferStream = await storageFile.OpenStreamForWriteAsync())
                {
                    bufferStream.Seek(0, SeekOrigin.End);
#else
            using (PinnedArray <T> buffer = new PinnedArray <T>(elementsInTrunk))
                using (FileStream stream = _fileInfo.Open(FileMode.Append, FileAccess.Write))
#if !NETSTANDARD1_4
                    using (BufferedStream bufferStream = new BufferedStream(stream, _trunkSize))
#endif
                {
#endif
#if NETSTANDARD1_4
                    var bufferStream = stream;
#endif
                    IntPtr ptr = buffer.AddrOfPinnedObject();
                    foreach (T s in samples)
                    {
                        buffer.Array[index++] = s;

                        if (index == buffer.Array.Length)
                        {
                            int writeCount = index * _elementSize;
                            Marshal.Copy(ptr, byteBuffer, 0, writeCount);
                            bufferStream.Write(byteBuffer, 0, writeCount);
                            index = 0;
                        }
                    }
                    if (index != 0)
                    {
                        int writeCount = index * _elementSize;
                        Marshal.Copy(ptr, byteBuffer, 0, writeCount);
                        bufferStream.Write(byteBuffer, 0, writeCount);
                        //index = 0;
                    }
                }
        }
Exemplo n.º 6
0
 /// <summary>
 /// return an enumerator of the elements in the sequence
 /// </summary>
 /// <returns>an enumerator of the elements in the sequence</returns>
 public IEnumerator <T> GetEnumerator()
 {
     using (PinnedArray <T> buffer = new PinnedArray <T>(1))
     {
         IntPtr address = buffer.AddrOfPinnedObject();
         for (int i = 0; i < Total; i++)
         {
             Toolbox.memcpy(address, CvInvoke.cvGetSeqElem(_ptr, i), _sizeOfElement);
             yield return(buffer.Array[0]);
             //yield return (T)Marshal.PtrToStructure(CvInvoke.cvGetSeqElem(_ptr, i), typeof(T));
             //yield return this[i];
         }
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// Append the samples to the end of the storage
        /// </summary>
        /// <param name="samples">The samples to be appended to the storage</param>
        public void Append(IEnumerable <T> samples)
        {
            int elementsInTrunk = _trunkSize / _elementSize;

            if (elementsInTrunk <= 0)
            {
                elementsInTrunk = 1;
            }

            byte[] byteBuffer = new byte[elementsInTrunk * _elementSize];
            int    index      = 0;

            using (PinnedArray <T> buffer = new PinnedArray <T>(elementsInTrunk))
                using (FileStream stream = _fileInfo.Open(FileMode.Append, FileAccess.Write))
                    using (BufferedStream bufferStream = new BufferedStream(stream, _trunkSize))
                    {
                        IntPtr ptr = buffer.AddrOfPinnedObject();
                        foreach (T s in samples)
                        {
                            buffer.Array[index++] = s;

                            if (index == buffer.Array.Length)
                            {
                                int writeCount = index * _elementSize;
                                Marshal.Copy(ptr, byteBuffer, 0, writeCount);
                                bufferStream.Write(byteBuffer, 0, writeCount);
                                index = 0;
                            }
                        }
                        if (index != 0)
                        {
                            int writeCount = index * _elementSize;
                            Marshal.Copy(ptr, byteBuffer, 0, writeCount);
                            bufferStream.Write(byteBuffer, 0, writeCount);
                            //index = 0;
                        }
                    }
        }