Beispiel #1
0
        /*
         *
         * MultiKeyDictionary mkd = new MultiKeyDictionary();
         * //Checking Add, Get, TryGetValue
         *  //mkd.Add("dsfs", "1", 2, 34L, 3m);
         *  mkd.Add("dsfs", 17L, 3L);
         *  //mkd.Add("dsfs", 17L); //Wrong add dimension is checked
         *  mkd.Add("dsfs3", 17L, 5L);
         *  mkd.Add("dsfs32", 17L, 5L);
         *  mkd.Add("apa", 10L, 7L);
         *  mkd.Add(new List<string> { "w1", "w2" }, 11L, 4L);
         *  var mkdRes = mkd.Get<string>(10L, 7L);
         *  var mkdRes1 = mkd.Get<List<string>>(11L, 4L);
         *  var mkdResBool = mkd.TryGetValue<List<string>>(out var dder, 11L, 4L);
         *  //var mkdResBool1 = mkd.TryGetValue<List<string>>(out var dder1, 11L, 6L);
         *  //var mkdResBool1 = mkd.TryGetValue<List<string>>(out var dder1, 11L);  //Wrong get dimension is checked
         *
         * //Checking Ulongs to longs
         *  mkd.Add("w1", ulong.MaxValue, ulong.MinValue);
         *  mkd.Add("w2", (ulong)1, (ulong)0);
         *  mkd.Add("w3", (ulong)100, (ulong)500);
         *  var v4 = mkd.Get<string>(ulong.MaxValue, ulong.MinValue);
         *  var v41 = mkd.Get<string>((ulong)1, (ulong)0);
         *  var v42 = mkd.Get<string>((ulong)100, (ulong)500);
         *  foreach (var el in mkd.GetAll())
         *  {
         *      ulong k1 = MultiKeyDictionary.MapLongToUlong((long)el[0]);
         *      ulong k2 = MultiKeyDictionary.MapLongToUlong((long)el[1]);
         *      string val = (string)el[2];
         *  }
         * //Checking GetAll
         * //mkd.Add("w1", int.MaxValue);
         *  //mkd.Add("w2", (int)1);
         *  //mkd.Add("w3", (int)100);
         *  //mkd.Add("w1", int.MaxValue, int.MinValue);
         *  //mkd.Add("w2", (int)1, (int)0);
         *  //mkd.Add("w3", (int)100, (int)500);
         *  //mkd.Add("w1", int.MaxValue, int.MinValue);
         *  //mkd.Add("w2", (int)1, (int)0);
         *  //mkd.Add("w21", (int)1, (int)1);
         *  //mkd.Add("w21", (int)1, (int)2);
         *  //mkd.Add("w3", (int)100, (int)500);
         *  mkd.Add("w1", int.MaxValue, int.MinValue, 12);
         *  mkd.Add("w1", int.MaxValue, int.MinValue, 14);
         *  mkd.Add("w2", (int)1, (int)0, 44);
         *  mkd.Add("w21", (int)1, (int)1, 12);
         *  mkd.Add("w21", (int)1, (int)1, 13);
         *  mkd.Add("w21", (int)1, (int)2, 22);
         *  mkd.Add("w3", (int)100, (int)500, 15);
         *  mkd.Add("w3", (int)100, (int)500, 16);
         *  //mkd.Add("w1", ulong.MaxValue, ulong.MinValue);
         *  //mkd.Add("w2", (ulong)1, (ulong)0);
         *  //mkd.Add("w3", (ulong)100, (ulong)500);
         *  //var v4 = mkd.Get<string>(ulong.MaxValue, ulong.MinValue);
         *  //var v41 = mkd.Get<string>((ulong)1, (ulong)0);
         *  //var v42 = mkd.Get<string>((ulong)100, (ulong)500);
         *  foreach (var el in mkd.GetAll())
         *  {
         *      //ulong k1 = MultiKeyDictionary.MapLongToUlong((long)el[0]);
         *      //ulong k2 = MultiKeyDictionary.MapLongToUlong((long)el[1]);
         *      //string val = (string)el[2];
         *  }
         */
        #endregion

        /// <summary>
        /// MultiKeySortedDictionary
        /// </summary>
        public MultiKeySortedDictionary()
        {
            _key = default(TKey);

            MultiKeyDictionary.CreateDeconstructDelegate(_key.Length, _key.GetType(), ref this.Impl);
            //MKDHelper.CreateSerializeDelegate(_key.Length, _key.GetType(), this.serSeq);
        }
        /// <summary>
        ///
        /// </summary>
        internal static void CreateDeconstructDelegate <TKey>(int keyLength, Type keyType, ref Func <List <object>, TKey> Impl)
        {
            int q = 8;

            if (keyLength <= 8)
            {
                q = keyLength;
            }

            // MethodInfo[] methodInfos = this.GetType()
            //.GetMethods(BindingFlags.NonPublic | BindingFlags.Static); // | BindingFlags..Instance

            var method = typeof(ValueTupleDeconstructor)//this.GetType()
                         .GetMethods(BindingFlags.NonPublic | BindingFlags.Static).Where(r => r.Name == "Deconstruct" + keyLength && r.ReturnParameter.ParameterType.Name == "ValueTuple`" + q + "").FirstOrDefault();

            Type[] tps = new Type[keyLength];
            int    i   = 0;

            foreach (var el in MultiKeyDictionary.get_type_flds(keyType))
            {
                tps[i] = el;
                i++;
            }

            method = method.MakeGenericMethod(tps);
            var keyValues = Expression.Parameter(typeof(List <object>));
            var call      = Expression.Call(null, method, keyValues);

            Impl = Expression.Lambda <Func <List <object>, TKey> >(call, keyValues).Compile();
        }
 void Init()
 {
     if (this.Impl == null)
     {
         lock (initLock)
         {
             if (this.Impl == null)
             {
                 MultiKeyDictionary.CreateDeconstructDelegate(_key.Length, _key.GetType(), ref this.Impl);
             }
         }
     }
 }
        /// <summary>
        /// MultiKeyDictionary.ByteArrayDeSerializator must be set (once for all instances)
        /// </summary>
        /// <param name="data"></param>
        public void Deserialize(byte[] data)
        {
            this.Clear();


            if (MultiKeyDictionary.ByteArrayDeSerializator == null)
            {
                return;
            }

            foreach (var el in (IEnumerable <(TKey, TValue)>)MultiKeyDictionary.ByteArrayDeSerializator(data, typeof(IEnumerable <(TKey, TValue)>)))
            {
                this.Add(el.Item1, el.Item2);
            }

            //return;


            //    //foreach (var el in (IEnumerable<(TKey, TValue)>)MultiKeyDictionary.ByteArrayDeSerializator(data, typeof(IEnumerable<(TKey, TValue)>)))
            //    //{
            //    //    d.Add(el.Item1, el.Item2);
            //    //}

            //    Biser.Decoder dec = new Biser.Decoder(data);

            //List<object> decres = new List<object>();

            //var totalElements = dec.GetLong();
            //for(int i = 0; i < totalElements; i++)
            //{
            //    for (int ij = 0; ij < _key.Length; ij++)
            //    {
            //        decres.Add(serSeq[ij].Item2(dec));
            //    }

            //    this.Add((TKey)Impl(decres), (TValue)MultiKeyDictionary.ByteArrayDeSerializator(dec.GetByteArray(), typeof(TValue)));

            //    decres.Clear();
            //}
        }