Exemplo n.º 1
0
 private ReadOnlySpan <char> AppendNewline(ReadOnlySpan <char> chars)
 {
     m_buffer.Clear();
     m_buffer.Fill(chars.Length + Environment.NewLine.Length, default);
     chars.CopyTo(m_buffer.AsSpan());
     Environment.NewLine.AsSpan().CopyTo(m_buffer.AsSpan().Slice(chars.Length));
     return(m_buffer.AsSpan());
 }
Exemplo n.º 2
0
            /// <summary>
            /// All relationships have been added; sort them all and build the final relation table.
            /// </summary>
            public void Complete()
            {
                m_list.AsSpan().Sort((tuple1, tuple2) =>
                {
                    int fromIdCompare = tuple1.fromId.FromId().CompareTo(tuple2.fromId.FromId());
                    if (fromIdCompare != 0)
                    {
                        return(fromIdCompare);
                    }
                    return(tuple1.toId.FromId().CompareTo(tuple2.toId.FromId()));
                });

                // and bin them by groups
                int listIndex = 0;
                SpannableList <TToId> buffer = new SpannableList <TToId>();
                int listCount = m_list.Count;

                Table.SetMultiValueCapacity(listCount);

                foreach (TFromId id in Table.BaseTableOpt.Ids)
                {
                    if (listIndex >= m_list.Count)
                    {
                        // ran outta entries, rest all 0
                        break;
                    }

                    // Count up how many are for id.
                    int count = 0;
                    buffer.Clear();

                    // create a to-ID that will never equal any other ID (even default)
                    TToId lastToId = default(TToId).ToId(-1);

                    while (listIndex + count < m_list.Count)
                    {
                        var(fromId, toId) = m_list[listIndex + count];
                        if (fromId.Equals(id))
                        {
                            // drop duplicates (silently...)
                            // TODO: are duplicates here a logic bug? Because they do happen in practice.
                            if (!toId.Equals(lastToId))
                            {
                                buffer.Add(toId);
                            }
                            count++;
                            lastToId = toId;
                            continue;
                        }
                        // ok we're done
                        break;
                    }

                    Table.Add(buffer.AsSpan());
                    listIndex += count;
                }
            }
Exemplo n.º 3
0
        /// <summary>
        /// Load from file.
        /// </summary>
        public override void LoadFromFile(string directory, string name)
        {
            Offsets.Clear();
            MultiValues.Clear();

            base.LoadFromFile(directory, name);

            FileSpanUtilities.LoadFromFile(directory, InsertSuffix(name, "MultiValue"), MultiValues);

            Offsets.Fill(Count, default);
            CalculateOffsets();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Load a list of unmanaged values from the given filename in the given directory.
        /// </summary>
        public static void LoadFromFile <TValue>(string directory, string name, SpannableList <TValue> values)
            where TValue : unmanaged
        {
            values.Clear();

            string path = Path.Combine(directory, name);

            using (Stream reader = File.OpenRead(path))
            {
                int[] lengthBuf = new int[1];
                reader.Read(MemoryMarshal.Cast <int, byte>(new Span <int>(lengthBuf)));
                int length = lengthBuf[0];

                values.Capacity = length;
                values.Fill(length, default);

                Span <TValue> valueSpan     = values.AsSpan();
                Span <byte>   byteValueSpan = MemoryMarshal.Cast <TValue, byte>(valueSpan);
                reader.Read(byteValueSpan);
            }
        }