Exemplo n.º 1
0
        public T this[string key]
        {
            get
            {
                lock (_obj)
                {
                    bool      mbool = false;
                    CKeyValue kv    = new CKeyValue()
                    {
                        Key = key
                    };
                    getContainer((T)kv, (cKeyValue) => {
                        kv.Value   = cKeyValue.Value;
                        kv.other   = cKeyValue.other;
                        kv.index   = cKeyValue.index;
                        kv.isReset = cKeyValue.isReset;
                        mbool      = true;
                    });

                    if (!mbool)
                    {
                        return(null);
                    }
                    return((T)kv);
                }
            }
        }
Exemplo n.º 2
0
        ChildList getContainer(T cKeyValue, Action <T> action)
        {
            ChildList keyValues = null;

            if (0 == cKeyValues.Count)
            {
                return(keyValues);
            }

            Task[] tasks = new Task[cKeyValues.Count];
            int    n     = 0;

            foreach (ChildList item in cKeyValues)
            {
                tasks[n] = new Task((o) => {
                    ChildList cKeys = (ChildList)o;
                    CKeyValue kv    = cKeys[cKeyValue.Key];
                    if (null != kv)
                    {
                        keyValues = cKeys;
                        if (null != action)
                        {
                            action((T)kv);
                        }
                    }
                }, item);
                tasks[n].Start();
                n++;
            }
            Task.WaitAll(tasks, -1);

            return(keyValues);
        }
Exemplo n.º 3
0
 public void Clear(string key)
 {
     lock (_obj)
     {
         CKeyValue kv = new CKeyValue()
         {
             Key = key
         };
         ChildList ts = getContainer((T)kv, null);
         if (null == ts)
         {
             return;
         }
         ts.EClear(key);
     }
 }
Exemplo n.º 4
0
            public void EClear(string key)
            {
                int n = GetIndexByKeyVal(key);

                if (-1 == n)
                {
                    return;
                }

                IList     kvlist = this;
                CKeyValue kv     = (CKeyValue)kvlist[n];

                if (kv != null)
                {
                    kvlist.Remove(kv);
                    removeMyself();
                }
            }
Exemplo n.º 5
0
            public void Add(CKeyValue cKeyValue)
            {
                int m = GetIndexByKeyVal(cKeyValue.Key);

                if (-1 != m)
                {
                    return;
                }

                IList kvlist = this;

                int t1 = (DateTime.Now.Second * 1000) + DateTime.Now.Millisecond;
                int n  = GetIndexByNewKeyVal(cKeyValue);
                int t2 = (DateTime.Now.Second * 1000) + DateTime.Now.Millisecond;
                int t3 = t2 - t1;

                if (findPositionTime < t3)
                {
                    findPositionTime = t3;
                }

                if (-1 == n)
                {
                    kvlist.Add(cKeyValue);
                    return;
                }

                t1 = (DateTime.Now.Second * 1000) + DateTime.Now.Millisecond;
                kvlist.Insert(n, cKeyValue);
                t2 = (DateTime.Now.Second * 1000) + DateTime.Now.Millisecond;
                t3 = t2 - t1;
                if (insertTime < t3)
                {
                    insertTime = t3;
                }
            }
Exemplo n.º 6
0
            public CKeyValue this[string key, bool isLike]
            {
                get
                {
                    CKeyValue kv     = null;
                    CKeyValue kv1    = null;
                    IList     kvlist = this;
                    whileNum = 0;

                    foreach (object item in kvlist)
                    {
                        whileNum++;
                        kv1 = item as CKeyValue;
                        if (kv1 != null)
                        {
                            if (isLike)
                            {
                                if (kv1.Key.IndexOf(key) != -1 || key.IndexOf(kv1.Key) != -1)
                                {
                                    kv = kv1;
                                    break;
                                }
                            }
                            else
                            {
                                if (kv1.Key.Equals(key))
                                {
                                    kv = kv1;
                                    break;
                                }
                            }
                        }
                    }
                    return(kv);
                }
            }
Exemplo n.º 7
0
            IList <int> GetIndexsByContainKey(string key)
            {
                whileNum = 0;

                IList <int> lists = new List <int>();

                IList kvlist = this;

                if (0 == kvlist.Count)
                {
                    return(lists);
                }

                CKeyValue ckv = new CKeyValue()
                {
                    Key = key
                };
                CKeyValue kv  = null;
                int       cmp = 0;

                if (1 == kvlist.Count)
                {
                    kv  = (CKeyValue)kvlist[0];
                    cmp = Compare(kv.index, ckv.index, true);
                    if (0 == cmp)
                    {
                        lists.Add(0);
                    }
                    return(lists);
                }

                int n1 = 0, n2 = kvlist.Count - 1;

                kv = (CKeyValue)kvlist[n1];
                CKeyValue kv1 = (CKeyValue)kvlist[n2];

                cmp = Compare(kv.index, ckv.index);
                int cmp1 = Compare(kv1.index, ckv.index);

                if (1 == cmp)
                {
                    return(lists);
                }

                if (2 == cmp1)
                {
                    return(lists);
                }

                int len = kvlist.Count;

                n1 = 0;
                n2 = (len - 1) / 2;
                int n3 = n2 + 1, n4 = len - 1;
                int n5 = 0, n6 = 0;

                //0 相等, 1 source>target, 2 source<target
                while (true)
                {
                    whileNum++;
                    if (n1 == n2 && n3 == n4)
                    {
                        kv   = (CKeyValue)kvlist[n1];
                        cmp  = Compare(kv.index, ckv.index);
                        kv1  = (CKeyValue)kvlist[n3];
                        cmp1 = Compare(kv1.index, ckv.index);

                        // index = -1;
                        if (0 == cmp)
                        {
                            //index = n2;
                        }
                        else if (0 == cmp1)
                        {
                            //index = n3;
                        }

                        break;
                    }

                    kv1  = (CKeyValue)kvlist[n2];
                    cmp1 = Compare(kv1.index, ckv.index);
                    if (0 == cmp1)
                    {
                        lists.Add(n2);
                    }
                    else if (1 == cmp1)
                    {
                        n4 = n2;
                        n2 = (n2 - n1) / 2 + n1;
                        n3 = n2 + 1;
                    }
                    else
                    {
                        n1 = n3;
                        n2 = (n4 - n3) / 2 + n3;
                        n3 = n2 + 1;
                        n3 = n3 > n4 ? n4 : n3;
                    }
                    break;
                }

                return(lists);
            }
Exemplo n.º 8
0
            /// <summary>
            /// 如果返回 -1 表示key在集合中不存在
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            int GetIndexByKeyVal(string key)
            {
                whileNum = 0;

                int   index  = -1;
                IList kvlist = this;

                if (0 == kvlist.Count)
                {
                    return(-1);
                }

                CKeyValue ckv = new CKeyValue()
                {
                    Key = key
                };
                CKeyValue kv  = null;
                int       cmp = 0;

                if (1 == kvlist.Count)
                {
                    kv  = (CKeyValue)kvlist[0];
                    cmp = Compare(kv.index, ckv.index);
                    if (0 == cmp)
                    {
                        index = 0;
                    }
                    return(index);
                }

                int n1 = 0, n2 = kvlist.Count - 1;

                kv = (CKeyValue)kvlist[n1];
                CKeyValue kv1 = (CKeyValue)kvlist[n2];

                cmp = Compare(kv.index, ckv.index);
                int cmp1 = Compare(kv1.index, ckv.index);

                if (1 == cmp)
                {
                    return(index);
                }

                if (2 == cmp1)
                {
                    return(index);
                }

                int len = kvlist.Count;

                n1 = 0;
                n2 = (len - 1) / 2;
                int n3 = n2 + 1, n4 = len - 1;

                //0 相等, 1 source>target, 2 source<target
                index = -2;
                while (-2 == index)
                {
                    whileNum++;
                    if (n1 == n2 && n3 == n4)
                    {
                        kv   = (CKeyValue)kvlist[n1];
                        cmp  = Compare(kv.index, ckv.index);
                        kv1  = (CKeyValue)kvlist[n3];
                        cmp1 = Compare(kv1.index, ckv.index);

                        index = -1;
                        if (0 == cmp)
                        {
                            index = n2;
                        }
                        else if (0 == cmp1)
                        {
                            index = n3;
                        }

                        break;
                    }

                    kv1  = (CKeyValue)kvlist[n2];
                    cmp1 = Compare(kv1.index, ckv.index);
                    if (0 == cmp1)
                    {
                        index = n2;
                    }
                    else if (1 == cmp1)
                    {
                        n4 = n2;
                        n2 = (n2 - n1) / 2 + n1;
                        n3 = n2 + 1;
                    }
                    else
                    {
                        n1 = n3;
                        n2 = (n4 - n3) / 2 + n3;
                        n3 = n2 + 1;
                        n3 = n3 > n4 ? n4 : n3;
                    }
                }

                return(index);
            }