public void PutEntries(List <BufferEntry> entries)
        {
            int size = CalculateEntriesSize(entries) * 2;

            this.buffer = new MemoryStreamWrapper(new MemoryStream(size + Constant.INTEGER_BYTES * 4));

            buffer.WriteInt32(size);
            buffer.WriteInt32(entries.Count);
            BufferEntry firstEntry = entries[0];

            buffer.WriteInt32(firstEntry.TokenInfo.Count);
            buffer.WriteInt32(firstEntry.PosInfo.Count);
            buffer.WriteInt32(firstEntry.Features.Count);

            foreach (BufferEntry entry in entries)
            {
                foreach (short s in entry.TokenInfo)
                {
                    buffer.WriteInt16(s);
                }

                buffer.Write(entry.PosInfo.ToArray(), 0, entry.PosInfo.Count);

                foreach (int feature in entry.Features)
                {
                    buffer.WriteInt32(feature);
                }
            }
        }
        public void ReadCosts(Stream input)
        {
            using (StreamReader reader = new StreamReader(input))
            {
                string   line          = reader.ReadLine();
                string[] cardinalities = line.SplitSpace();

                int forwardSize  = Int32.Parse(cardinalities[0]);
                int backwardSize = Int32.Parse(cardinalities[1]);

                cardinality = backwardSize;
                bufferSize  = forwardSize * backwardSize * Constant.SHORT_BYTES;

                if (costs != null)
                {
                    costs.Dispose();
                }

                costs = new MemoryStreamWrapper(new MemoryStream(bufferSize));

                while (!reader.EndOfStream)
                {
                    string[] fields = reader.ReadLine().SplitSpace();

                    short forwardId  = Int16.Parse(fields[0]);
                    short backwardId = Int16.Parse(fields[1]);
                    short cost       = Int16.Parse(fields[2]);

                    PutCost(forwardId, backwardId, cost);
                }
            }
        }
Пример #3
0
        public void TestReadAndWriteFromBuffer()
        {
            List <short> shorts = new List <short>();

            for (int i = 0; i < 10; i++)
            {
                shorts.Add((short)i);
            }

            MemoryStreamWrapper buffer = new MemoryStreamWrapper(new MemoryStream(shorts.Count * 2 + 2));

            buffer.WriteInt16((short)shorts.Count);

            foreach (short s in shorts)
            {
                buffer.WriteInt16(s);
            }

            buffer.Stream.Position = 0;

            short count = buffer.ReadInt16();

            List <short> readShorts = new List <short>();

            for (int i = 0; i < count; i++)
            {
                readShorts.Add(buffer.ReadInt16());
            }

            for (int i = 0; i < shorts.Count; i++)
            {
                Assert.AreEqual(readShorts[i], shorts[i]);
            }
        }
Пример #4
0
        public void SetUp()
        {
            string costs = "" +
                           "3 3\n" +
                           "0 0 1\n" +
                           "0 1 2\n" +
                           "0 2 3\n" +
                           "1 0 4\n" +
                           "1 1 5\n" +
                           "1 2 6\n" +
                           "2 0 7\n" +
                           "2 1 8\n" +
                           "2 2 9\n";

            using (ConnectionCostsCompiler compiler = new ConnectionCostsCompiler())
                using (var outputStream = File.Create(costFile))
                {
                    var    bytes       = Encoding.UTF8.GetBytes(costs);
                    Stream inputStream = new MemoryStream(bytes);
                    compiler.ReadCosts(inputStream);
                    compiler.Compile(outputStream);
                }

            using (var readStream = File.OpenRead(costFile))
                using (var reader = new BinaryReader(readStream))
                {
                    int size        = reader.ReadRawInt32();
                    var costsBuffer = new MemoryStreamWrapper(ByteBufferIO.Read(readStream));
                    connectionCosts = new ConnectionCosts(size, costsBuffer);
                }
        }
Пример #5
0
 public void Dispose()
 {
     if (buffer != null)
     {
         buffer.Dispose();
     }
     buffer = null;
 }
 private static ConnectionCosts Read(Stream input)
 {
     try
     {
         lock (input)
         {
             using (BinaryReader reader = new BinaryReader(input))
             {
                 int size  = reader.ReadRawInt32();
                 var costs = new MemoryStreamWrapper(ByteBufferIO.Read(reader));
                 return(new ConnectionCosts(size, costs));
             }
         }
     }
     catch (Exception ex)
     {
         throw new Exception("ConnectionCosts.Read: " + ex.Message);
     }
 }
Пример #7
0
        public TokenInfoBuffer(Stream inputStream)
        {
            try
            {
                if (buffer != null)
                {
                    buffer.Dispose();
                }

                buffer         = new MemoryStreamWrapper(ByteBufferIO.Read(inputStream));
                tokenInfoCount = GetTokenInfoCount();
                posInfoCount   = GetPosInfoCount();
                featureCount   = GetFeatureCount();
                entrySize      = GetEntrySize(tokenInfoCount, posInfoCount, featureCount);
            }
            catch (Exception ex)
            {
                throw new IOException("TokenInfoBuffer Constructor: " + ex.Message);
            }
        }
Пример #8
0
        private void Put(SortedDictionary <int, string> strings)
        {
            int bufferSize = CalculateSize(strings);

            size = strings.Count;

            if (buffer != null)
            {
                buffer.Dispose();
            }

            buffer = new MemoryStreamWrapper(new MemoryStream(bufferSize));
            buffer.WriteInt32(size);                 // Set entries

            int keyIndex   = Constant.INTEGER_BYTES; // First key index is past size
            int entryIndex = keyIndex + size * Constant.INTEGER_BYTES;

            foreach (string str in strings.Values)
            {
                buffer.WriteInt32(entryIndex, keyIndex);
                entryIndex = Put(entryIndex, str);
                keyIndex  += Constant.INTEGER_BYTES;
            }
        }
 public ConnectionCosts(int size, MemoryStreamWrapper costs)
 {
     this.size  = size;
     this.costs = costs;
 }
Пример #10
0
 public StringValueMapBuffer(Stream inputStream)
 {
     buffer      = new MemoryStreamWrapper(ByteBufferIO.Read(inputStream));
     streamArray = buffer.Stream.ToArray();
     size        = buffer.ReadInt32();
 }