Ejemplo n.º 1
0
        public void ConstructCapacity11()
        {
            var d = new DictionarySlim <ulong, int>(11);

            Assert.Equal(0, d.Count);
            Assert.Equal(16, d.GetCapacity());
        }
Ejemplo n.º 2
0
        public void SingleEntry()
        {
            var d = new DictionarySlim <ulong, int>();

            d.GetOrAddValueRef(7)++;
            d.GetOrAddValueRef(7) += 3;
            Assert.Equal(4, d.GetOrAddValueRef(7));
        }
Ejemplo n.º 3
0
 public void LoadDictionarySlim()
 {
     _dictSlim = new DictionarySlim <int, int>();
     for (int i = 0; i < Size; i++)
     {
         _dictSlim.GetOrAddValueRef(i * 7) = -i;
     }
 }
Ejemplo n.º 4
0
        public void TryGetValue_Missing()
        {
            var d = new DictionarySlim <char, int>();

            d.GetOrAddValueRef('a') = 9;
            d.GetOrAddValueRef('b') = 11;
            d.Remove('b');
            Assert.Equal(false, d.TryGetValue('z', out int value));
            Assert.Equal(default, value);
 private static void Check(DictionarySlim <long, int> dict, ref long rollingKey, byte nb, long mask)
 {
     if (nb == 255)
     {
         return;
     }
     rollingKey = ((rollingKey & mask) << 2) | nb;
     dict.GetOrAddValueRef(rollingKey)++;
 }
        internal List <AssembleError> errors_list;                                         //list of all Assemble time errors

        private void ResetAssembler()                                                      //reset the state of the assembler
        {
            errors_list    = new();
            labels         = new();
            to_be_resolved = new();
            temp_memory    = new byte[ushort.MaxValue + 1];
            line_number    = 1;
            counter        = 0;
            code_addresses = new();
        }
Ejemplo n.º 7
0
        public void ContainKey()
        {
            var d = new DictionarySlim <ulong, int>();

            d.GetOrAddValueRef(7)  = 9;
            d.GetOrAddValueRef(10) = 10;
            Assert.True(d.ContainsKey(7));
            Assert.True(d.ContainsKey(10));
            Assert.False(d.ContainsKey(1));
        }
Ejemplo n.º 8
0
        public void TryGetValue_Present()
        {
            var d = new DictionarySlim <char, int>();

            d.GetOrAddValueRef('a') = 9;
            d.GetOrAddValueRef('b') = 11;
            Assert.Equal(true, d.TryGetValue('a', out int value));
            Assert.Equal(9, value);
            Assert.Equal(true, d.TryGetValue('b', out value));
            Assert.Equal(11, value);
        }
        private string WriteCount(DictionarySlim <long, int> dictionary, string fragment)
        {
            long key = 0;

            for (int i = 0; i < fragment.Length; ++i)
            {
                key = (key << 2) | tonum[fragment[i]];
            }
            dictionary.TryGetValue(key, out int v);
            return(string.Concat(v.ToString(), "\t", fragment));
        }
Ejemplo n.º 10
0
        public DictionarySlim <string, int> Run()
        {
            var registry = new DictionarySlim <string, int>();

            foreach (var instruction in _instructions)
            {
                instruction.Apply(registry);
            }

            return(registry);
        }
Ejemplo n.º 11
0
        // [Fact] // Test too slow
        public void ResizeToCapacity()
        {
            var d = new DictionarySlim <uint, byte>();

            Assert.Throws <InvalidOperationException>(() =>
            {
                for (uint i = 0; i < uint.MaxValue; i++)
                {
                    d.GetOrAddValueRef(i) = (byte)1;
                }
            });
        }
Ejemplo n.º 12
0
            static void AssignBots(IEnumerable <Assignment> assignments, DictionarySlim <int, Bot> bots, Logger logger)
            {
                foreach (var assignment in assignments)
                {
                    ref var bot = ref bots.GetOrAddValueRef(assignment.BotNumber);
                    if (bot is null)
                    {
                        bot = new Bot(assignment.BotNumber);
                    }

                    bot.Set(assignment.Value);
                    logger.AssignValue(bot.Number, assignment.Value);
                }
Ejemplo n.º 13
0
            public DictionarySlim <string, string> DictSlim_GetOrAddValueRef()
            {
                var map = new DictionarySlim <string, string>();

                for (var i = 0; i < Count; ++i)
                {
                    var a = i.ToString();
                    var v = a + Seed;
                    map.GetOrAddValueRef(v + a) = v;
                }

                return(map);
            }
Ejemplo n.º 14
0
        public static IEnumerable <IGrouping <TKey, TSource> > GroupByRef <TSource, TKey>(this IEnumerable <TSource> source, Func <TSource, TKey> keySelector) where TKey : IEquatable <TKey>
        {
            var dict = new DictionarySlim <TKey, Grouping <TKey, TSource> >();

            foreach (var t in source)
            {
                var     k = keySelector(t);
                ref var g = ref dict.GetOrAddValueRef(k);
                if (g == null)
                {
                    g = new Grouping <TKey, TSource>(k);
                }
                g.Add(t);
            }
Ejemplo n.º 15
0
        public int CalculateMaxMemory()
        {
            var registry = new DictionarySlim <string, int>();
            var max      = 0;

            foreach (var instruction in _instructions)
            {
                instruction.Apply(registry);

                max = Math.Max(max, registry.OrderByDescending(x => x.Value).DefaultIfEmpty().First().Value);
            }

            return(max);
        }
Ejemplo n.º 16
0
        public static IIndexed <TKey, TValue> ToIndexed <T, TKey, TValue, TComparer>(
            this IEnumerable <T> enumerable,
            Func <T, TKey> keySelector,
            Func <T, TValue> valueSelector,
            TComparer comparer
            ) where TComparer : IEqualityComparer <TKey>
        {
            var dictionary = new DictionarySlim <TKey, TValue, TComparer>(comparer);

            foreach (var item in enumerable)
            {
                dictionary.AddOrReplace(keySelector(item), valueSelector(item));
            }

            return(dictionary);
        }
Ejemplo n.º 17
0
        public void CompareSCGAndFastDictionaryWithInts()
        {
            var dictionary           = new Dictionary <long, long>();
            var dictionarySlim       = new DictionarySlim <long, long>();
            var fastDictionary       = new FastDictionary <long, long>();
            var concurrentDictionary = new ConcurrentDictionary <long, long>();

            var length = 10000;

            for (int i = 0; i < length; i++)
            {
                dictionary.Add(i, i);
                ref var x = ref dictionarySlim.GetOrAddValueRef(i);
                x = i;
                fastDictionary.Add(i, i);
                concurrentDictionary[i] = i;
            }
Ejemplo n.º 18
0
#pragma warning disable CS8618 // Non-nullable field is uninitialized.
        internal Binary(ImmutableSettings settings, TStream stream)
#pragma warning restore CS8618 // Non-nullable field is uninitialized.
        {
            Settings      = settings;
            _binaryWriter = new BinaryWriter <TStream, TSettingGen>(this);
            _binaryReader = new BinaryReader <TStream, TSettingGen>(this);
            _stream       = stream;
            if (Settings.SerializationMode == Mode.Graph)
            {
                _savedObjectLookup = new DictionarySlim <object, int>(16);
                _loadedObjectRefs  = new List <object>(16);
            }

            if (Settings.SupportSerializationHooks)
            {
                _deserializationHooks = new List <ValueTuple <Action <object, object>, object> >();
            }
        }
        private string WriteFrequencies(DictionarySlim <long, int> freq, int fragmentLength)
        {
            var    sb      = new StringBuilder();
            double percent = 100.0 / freq.Select(x => x.Value).Sum();

            foreach (var kv in freq.OrderByDescending(i => i.Value))
            {
                var keyChars = new char[fragmentLength];
                var key      = kv.Key;
                for (int i = keyChars.Length - 1; i >= 0; --i)
                {
                    keyChars[i] = tochar[key & 0x3];
                    key       >>= 2;
                }
                sb.Append(keyChars);
                sb.Append(" ");
                sb.AppendLine((kv.Value * percent).ToString("F3"));
            }
            return(sb.ToString());
        }
        public void Setup()
        {
            dictionary = new Dictionary <string, bool>(data.Length);
            foreach (var item in data)
            {
                dictionary.Add(item, true);
            }

            dictionarySlim = new DictionarySlim <string, bool>(data.Length);
            foreach (var item in data)
            {
                dictionarySlim.GetOrAddValueRef(item) = true;
            }

            hashset = new HashSet <string>();
            foreach (var item in data)
            {
                hashset.Add(item);
            }

            array[8] = 444;
        }
        private Task <string> Count(int l, long mask, Func <DictionarySlim <long, int>, string> summary)
        {
            return(Task.Run(() =>
            {
                long rollingKey = 0;
                var firstBlock = threeBlocks[0];
                var start = threeStart;
                while (--l > 0)
                {
                    rollingKey = (rollingKey << 2) | firstBlock[start++];
                }
                var dict = new DictionarySlim <long, int>(1024);

                for (int i = start; i < firstBlock.Length; i++)
                {
                    Check(dict, ref rollingKey, firstBlock[i], mask);
                }

                int lastBlockId = threeBlocks.Count - 1;
                for (int bl = 1; bl < lastBlockId; bl++)
                {
                    var bytes = threeBlocks[bl];
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        Check(dict, ref rollingKey, bytes[i], mask);
                    }
                }

                var lastBlock = threeBlocks[lastBlockId];
                for (int i = 0; i < threeEnd; i++)
                {
                    Check(dict, ref rollingKey, lastBlock[i], mask);
                }

                return summary(dict);
            }));
        }
Ejemplo n.º 22
0
 public void BasicTest()
 {
     var     dict  = new DictionarySlim();
     ref var value = ref dict.GetOrAddValueRef("test");
Ejemplo n.º 23
0
 public void CreateDictionarySlim()
 {
     _refDict = new DictionarySlim <KeyWithHashCode, int>(Size / AggCount);
 }
Ejemplo n.º 24
0
    static void Main(string[] args)
    {
        var counts = new DictionarySlim <string, int>();
        var buffer = new char[BufferSize];
        var offset = 0;

        while (true)
        {
            // Read from stdin in chars
            var bytesRead = Console.In.Read(buffer, offset, BufferSize - offset);
            if (bytesRead == 0)
            {
                break;
            }

            // Determine last delimiter position
            var bufferLength       = offset + bytesRead;
            var lastDelimiterIndex = -1;
            for (var k = bufferLength - 1; k >= 0; --k)
            {
                if (buffer[k] <= ' ')
                {
                    lastDelimiterIndex = k;
                    break;
                }
            }

            var count = lastDelimiterIndex >= 0 ? lastDelimiterIndex : bufferLength;
            var i     = 0;

            while (true)
            {
                // Skip whitespaces before each word
                for (; i < count; ++i)
                {
                    var c = buffer[i];
                    if (c > ' ')
                    {
                        break;
                    }
                }

                // Find word boundary and make it lowercase at the same time.
                var start = i;
                for (; i < count; ++i)
                {
                    var c = buffer[i];
                    if (c <= ' ')
                    {
                        break;
                    }

                    // Set 6th bit to make char lowercase
                    buffer[i] = (char)(buffer[i] | 0x20);
                }

                if (i <= start)
                {
                    break;
                }

                // Collect word statistics
                var     word     = new String(buffer, start, i - start);
                ref var curCount = ref counts.GetOrAddValueRef(word);
                curCount++;
            }

            // Copy remaining part to the beginning of the buffer.
            if (lastDelimiterIndex >= 0)
            {
                offset = (offset + bytesRead - 1) - lastDelimiterIndex;
                Array.Copy(buffer, lastDelimiterIndex + 1, buffer, 0, offset);
            }
            else
            {
                offset = 0;
            }
        }
Ejemplo n.º 25
0
 internal Enumerator(DictionarySlim <TKey, TValue> dictionary)
 {
     this.entries = dictionary.entries;
     this.index   = 0;
     this.count   = dictionary.count;
 }
Ejemplo n.º 26
0
 public void CreateDictionarySlim()
 {
     _refDict = new DictionarySlim <string, int>(Size / AggCount);
 }
Ejemplo n.º 27
0
 public Factory()
 {
     _outputs = new Dictionary <int, int>();
     _bots    = new DictionarySlim <int, Bot>();
     Logger   = new Logger();
 }