Пример #1
0
        public object Get(IPAddressCmp ip)
        {
            IPAddressCmp ip_addr = ip.ToIPv6();
            int          l = 0, r = list.Count - 1;

            while (l < r)
            {
                int          m   = (l + r + 1) / 2;
                IPAddressCmp v   = list.GetKey(m) as IPAddressCmp;
                int          cmp = v.CompareTo(ip_addr);
                if (cmp > 0)
                {
                    r = m - 1;
                }
                else if (cmp < 0)
                {
                    l = m;
                }
                else if (cmp == 0)
                {
                    return(list[m]);
                }
            }
            return(list.GetByIndex(l));
        }
Пример #2
0
        public bool insert(IPAddressCmp ipStart, IPAddressCmp ipEnd, object val)
        {
            IPAddressCmp s      = ipStart.ToIPv6();
            IPAddressCmp e      = ipEnd.ToIPv6().Inc();
            object       ed_val = null;

            if (list.Contains(s))
            {
                ed_val  = list[s];
                list[s] = val;
            }
            else
            {
                list[s] = val;
                int index = list.IndexOfKey(s) - 1;
                if (index >= 0)
                {
                    ed_val = list.GetByIndex(index);
                }
            }
            {
                int index = list.IndexOfKey(s);
                while (index > 0)
                {
                    if (val.Equals(list.GetByIndex(index - 1)))
                    {
                        list.RemoveAt(index);
                        --index;
                    }
                    else
                    {
                        break;
                    }
                }
                ++index;
                bool keep = false;
                while (index < list.Count)
                {
                    int cmp = (list.GetKey(index) as IPAddressCmp).CompareTo(e);
                    if (cmp >= 0)
                    {
                        if (cmp == 0)
                        {
                            keep = true;
                        }
                        break;
                    }
                    ed_val = list.GetByIndex(index);
                    list.RemoveAt(index);
                }
                if (!keep)
                {
                    list[e] = ed_val;
                    index   = list.IndexOfKey(e);
                    while (index > 0)
                    {
                        if (ed_val.Equals(list.GetByIndex(index - 1)))
                        {
                            list.RemoveAt(index);
                            --index;
                        }
                        else
                        {
                            break;
                        }
                    }
                    while (index + 1 < list.Count)
                    {
                        if (ed_val.Equals(list.GetByIndex(index + 1)))
                        {
                            list.RemoveAt(index);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            return(true);
        }
Пример #3
0
 public IPAddressCmp(string ip)
     : base(IPAddressCmp.FromString(ip).GetAddressBytes())
 {
 }