Esempio n. 1
0
        public void DictionaryColumn_NonString()
        {
            DictionaryColumn <int, int> column     = new DictionaryColumn <int, int>(new NumberColumn <int>(-1), new NumberColumn <int>(-1), Nullability.NullsDisallowed);
            IDictionary <int, int>      dictionary = column[0];

            dictionary[5]  = 5;
            dictionary[8]  = 8;
            dictionary[10] = 10;

            Assert.Equal(10, dictionary[10]);
            Assert.Equal(5, dictionary[5]);
            Assert.False(dictionary.ContainsKey(6));

            // Trigger column collection with all values still used; verify everything kept
            column.Trim();
            Assert.Equal(10, dictionary[10]);
            Assert.Equal(5, dictionary[5]);
            Assert.False(dictionary.ContainsKey(6));

            // Replace a value
            dictionary[10] = 11;
            Assert.Equal(11, dictionary[10]);

            // Remove a key
            dictionary.Remove(5);
            Assert.True(dictionary.ContainsKey(10));
            Assert.False(dictionary.ContainsKey(5));

            // Trigger unused key/value pair collection
            column.Trim();

            // Verify remaining key still present and with correct value
            Assert.Equal(11, dictionary[10]);
            Assert.Equal(8, dictionary[8]);
        }
Esempio n. 2
0
        public static void Set(DictionaryColumn <TKey, TValue> column, int index, IDictionary <TKey, TValue> value)
        {
            if (index < 0)
            {
                throw new IndexOutOfRangeException(nameof(index));
            }

            if (value == null)
            {
                column._pairs[index] = null;
            }
            else
            {
                // Setting List to empty 'coerces' list creation in correct column
                NumberList <int> pairs = column._pairs[index];

                if (pairs == null)
                {
                    column._pairs[index] = NumberList <int> .Empty;
                    pairs = column._pairs[index];
                }

                new ColumnDictionary <TKey, TValue>(column, index, pairs).SetTo(value);
            }
        }
        public void DictionaryColumn_Basics()
        {
            DictionaryColumn <string, string> scratch      = new DictionaryColumn <string, string>(new StringColumn(), new StringColumn());
            ColumnDictionary <string, string> defaultValue = ColumnDictionary <string, string> .Empty;

            ColumnDictionary <string, string> otherValue = SampleRow();

            otherValue.SetTo(new Dictionary <string, string>()
            {
                ["Name"] = "Scott",
                ["City"] = "Redmond"
            });

            Column.Basics <IDictionary <string, string> >(
                () => new DictionaryColumn <string, string>(
                    new DistinctColumn <string>(new StringColumn()),
                    new StringColumn()),
                defaultValue,
                otherValue,
                (i) =>
            {
                if (scratch[i].Count == 0)
                {
                    scratch[i][(i % 10).ToString()]       = i.ToString();
                    scratch[i][((i + 1) % 10).ToString()] = i.ToString();
                }

                return(scratch[i]);
            }
                );
        }
        public static ColumnDictionary <string, string> SampleRow()
        {
            DictionaryColumn <string, string> column = new DictionaryColumn <string, string>(
                new DistinctColumn <string>(new StringColumn(), null),
                new StringColumn());

            return((ColumnDictionary <string, string>)column[0]);
        }
Esempio n. 5
0
 public static ColumnDictionary <TKey, TValue> Get(DictionaryColumn <TKey, TValue> column, int index)
 {
     if (index < 0)
     {
         throw new IndexOutOfRangeException(nameof(index));
     }
     return(column?._pairs?[index] == null ? null : new ColumnDictionary <TKey, TValue>(column, index));
 }
Esempio n. 6
0
        public void DictionaryColumn_Basics()
        {
            DictionaryColumn <string, string> scratch      = new DictionaryColumn <string, string>(new StringColumn(), new StringColumn(), Nullability.DefaultToEmpty);
            ColumnDictionary <string, string> defaultValue = ColumnDictionary <string, string> .Empty;

            ColumnDictionary <string, string> otherValue = SampleRow();
            Dictionary <string, string>       model      = new Dictionary <string, string>()
            {
                ["Name"] = "Scott",
                ["City"] = "Redmond"
            };

            otherValue.SetTo(model);

            // Test ColumnDictionary.Equals against non-ColumnDictionary IDictionary (slower compare path)
            Assert.True(otherValue.Equals(model));
            model["City"] = "Bellevue";
            Assert.False(otherValue.Equals(model));

            Column.Basics <IDictionary <string, string> >(
                () => new DictionaryColumn <string, string>(
                    new DistinctColumn <string>(new StringColumn()),
                    new StringColumn(),
                    Nullability.DefaultToEmpty),
                defaultValue,
                otherValue,
                (i) =>
            {
                if (scratch[i].Count == 0)
                {
                    scratch[i][(i % 10).ToString()]       = i.ToString();
                    scratch[i][((i + 1) % 10).ToString()] = i.ToString();
                }

                return(scratch[i]);
            }
                );

            defaultValue = null;
            Column.Basics <IDictionary <string, string> >(
                () => new DictionaryColumn <string, string>(
                    new DistinctColumn <string>(new StringColumn()),
                    new StringColumn(),
                    Nullability.DefaultToNull),
                defaultValue,
                otherValue,
                (i) =>
            {
                if (scratch[i].Count == 0)
                {
                    scratch[i][(i % 10).ToString()]       = i.ToString();
                    scratch[i][((i + 1) % 10).ToString()] = i.ToString();
                }

                return(scratch[i]);
            }
                );
        }
Esempio n. 7
0
 public ColumnDictionary(DictionaryColumn <TKey, TValue> column, int index)
 {
     if (index < 0)
     {
         throw new IndexOutOfRangeException(nameof(index));
     }
     _column   = column;
     _rowIndex = index;
 }
Esempio n. 8
0
        public static ColumnDictionary <TKey, TValue> Get(DictionaryColumn <TKey, TValue> column, int index)
        {
            if (index < 0)
            {
                throw new IndexOutOfRangeException(nameof(index));
            }

            NumberList <int> pairs = column._pairs[index];

            return(pairs == null ? null : new ColumnDictionary <TKey, TValue>(column, index, pairs));
        }
Esempio n. 9
0
        public static ColumnDictionary <string, string> SampleRow()
        {
            DictionaryColumn <string, string> column = new DictionaryColumn <string, string>(
                new DistinctColumn <string>(new StringColumn(), null),
                new StringColumn(),
                nullByDefault: false);

            ColumnDictionary <string, string> first = (ColumnDictionary <string, string>)column[0];

            first["One"] = "One";
            first["Two"] = "Two";

            return((ColumnDictionary <string, string>)column[1]);
        }
Esempio n. 10
0
        public static ColumnDictionary <string, string> SampleRow()
        {
            DictionaryColumn <string, string> column = new DictionaryColumn <string, string>(
                new DistinctColumn <string>(new StringColumn(), null),
                new StringColumn(),
                Nullability.NullsDisallowed);

            ColumnDictionary <string, string> first = (ColumnDictionary <string, string>)column[0];

            first["One"] = "One";
            first.Add("Two", "Two");

            return((ColumnDictionary <string, string>)column[1]);
        }
Esempio n. 11
0
        private void NonStringDictionarySetup()
        {
            _nonStringColumn = new DictionaryColumn <int, int>(new NumberColumn <int>(0), new NumberColumn <int>(0), Nullability.NullsDisallowed);

            for (int row = 0; row < 1000; ++row)
            {
                IDictionary <int, int> dictionary = _nonStringColumn[row];

                for (int key = 0; key < 128; ++key)
                {
                    dictionary[key * 2] = key * 4;
                }
            }
        }
Esempio n. 12
0
 protected ColumnDictionary(DictionaryColumn <TKey, TValue> column, int index, NumberList <int> pairs)
 {
     _column   = column;
     _rowIndex = index;
     _pairs    = pairs;
 }
 public DictionaryEnumerator(DictionaryColumn <TKey, TValue> column, ArraySlice <int> indices)
 {
     _column  = column;
     _indices = indices;
     _index   = -1;
 }
Esempio n. 14
0
        public static List <NamedList> ReadDictionaries(IEnumerable <IReadOnlyDictionary <string, object> > dictionaries)
        {
            Debug.Assert(dictionaries != null);

            // Iterate through the dictionaries, creating header objects that contain the un-cast values
            // and some information about them.
            List <DictionaryColumn> headers = null;

            foreach (IReadOnlyDictionary <string, object> dictionary in dictionaries)
            {
                // From the first row, create the headers list based on key names.
                if (headers == null)
                {
                    headers = new List <DictionaryColumn>(dictionary.Count);
                    foreach (string key in dictionary.Keys)
                    {
                        DictionaryColumn header = new DictionaryColumn()
                        {
                            Name = key, IsNullable = false, Type = null, Data = new List <object>()
                        };
                        headers.Add(header);
                    }
                }

                if (dictionary.Count != headers.Count)
                {
                    throw new InvalidOperationException();
                }

                // For all rows, check for null, record the type if we haven't found it yet, and store the value.
                for (int i = 0; i < headers.Count; i++)
                {
                    DictionaryColumn header = headers[i];
                    object           value  = dictionary[header.Name];
                    if (value == null)
                    {
                        header.IsNullable = true;
                    }
                    else
                    {
                        if (header.Type == null)
                        {
                            header.Type = value.GetType();
                        }
                    }
                    header.Data.Add(value);
                }
            }

            // Arrange the columns into named lists of the appropriate type
            List <NamedList> columns = new List <NamedList>(headers.Count);

            foreach (DictionaryColumn header in headers)
            {
                NamedList column;
                if (header.Type == null)
                {
                    // If no non-null value was ever found, we can't infer a type, so just make an object-column.
                    column = new NamedList <object>(header.Name, header.Data);
                }
                else
                {
                    // Based on null-ability and observed type, create the appropriate storage.
                    Type type = header.Type;
                    if (header.IsNullable && type.GetTypeInfo().IsValueType)
                    {
                        type = typeof(Nullable <>).MakeGenericType(type);
                    }
                    column = NamedList.Create(header.Name, type);
                    // Copy the objects into the storage, which will cast them to the storage type.
                    foreach (object value in header.Data)
                    {
                        column.AddItem(value);
                    }
                }
                columns.Add(column);
            }
            Debug.Assert(columns.Count == headers.Count);

            return(columns);
        }
Esempio n. 15
0
 protected ColumnDictionary(DictionaryColumn <TKey, TValue> column, int index)
 {
     _column   = column;
     _rowIndex = index;
 }