Exemple #1
0
        void Remove(int index, UniqueId uid)
        {
            if (uid == ranges[index].Min)
            {
                // remove the first item in the range
                if (ranges[index].Min < ranges[index].Max)
                {
                    ranges[index].Min = new UniqueId(uid.Id + 1);
                }
                else
                {
                    ranges.RemoveAt(index);
                }
            }
            else if (uid == ranges[index].Max)
            {
                // remove the last item in the range
                ranges[index].Max = new UniqueId(uid.Id - 1);
            }
            else
            {
                // remove a uid from the middle of the range
                var min = new UniqueId(uid.Id + 1);
                var max = new UniqueId(uid.Id - 1);

                var range = new UniqueIdRange(ranges[index].Min, max);
                ranges.Insert(index, range);
                ranges[index + 1].Min = min;
            }

            count--;
        }
Exemple #2
0
        /// <summary>
        /// Attempt to parse the specified token as a set of unique identifiers.
        /// </summary>
        /// <remarks>
        /// Attempts to parse the specified token as a set of unique identifiers.
        /// </remarks>
        /// <returns><c>true</c> if the set of unique identifiers were successfully parsed; otherwise, <c>false</c>.</returns>
        /// <param name="token">The token containing the set of unique identifiers.</param>
        /// <param name="validity">The UIDVALIDITY value.</param>
        /// <param name="uids">The set of unique identifiers.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="token"/> is <c>null</c>.
        /// </exception>
        public static bool TryParse(string token, uint validity, out UniqueIdSet uids)
        {
            if (token == null)
            {
                throw new ArgumentNullException("token");
            }

            uids = new UniqueIdSet {
                sorted = false
            };

            UniqueId start, end;
            int      index = 0;

            do
            {
                if (!UniqueId.TryParse(token, ref index, validity, out start))
                {
                    return(false);
                }

                if (index >= token.Length)
                {
                    uids.ranges.Add(new UniqueIdRange(start, start));
                    uids.count++;
                    return(true);
                }

                if (token[index] == ':')
                {
                    index++;

                    if (!UniqueId.TryParse(token, ref index, validity, out end))
                    {
                        return(false);
                    }

                    var range = new UniqueIdRange(start, end);
                    uids.count += range.Count;
                    uids.ranges.Add(range);
                }
                else
                {
                    uids.ranges.Add(new UniqueIdRange(start, start));
                    uids.count++;
                }

                if (index >= token.Length)
                {
                    return(true);
                }

                if (token[index++] != ',')
                {
                    return(false);
                }
            } while (true);
        }
        /// <summary>
        /// Attempt to parse a unique identifier range.
        /// </summary>
        /// <remarks>
        /// Attempts to parse a unique identifier range.
        /// </remarks>
        /// <returns><c>true</c> if the unique identifier range was successfully parsed; otherwise, <c>false.</c>.</returns>
        /// <param name="token">The token to parse.</param>
        /// <param name="validity">The UIDVALIDITY value.</param>
        /// <param name="range">The unique identifier range.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="token"/> is <c>null</c>.
        /// </exception>
        public static bool TryParse(string token, uint validity, out UniqueIdRange range)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            uint start, end;
            int  index = 0;

            if (!UniqueId.TryParse(token, ref index, out start) || index + 2 > token.Length || token[index++] != ':')
            {
                range = Invalid;
                return(false);
            }

            if (token[index] != '*')
            {
                if (!UniqueId.TryParse(token, ref index, out end) || index < token.Length)
                {
                    range = Invalid;
                    return(false);
                }
            }
            else if (index + 1 != token.Length)
            {
                range = Invalid;
                return(false);
            }
            else
            {
                end = uint.MaxValue;
            }

            range = new UniqueIdRange(validity, start, end);

            return(true);
        }
 /// <summary>
 /// Attempt to parse a unique identifier range.
 /// </summary>
 /// <remarks>
 /// Attempts to parse a unique identifier range.
 /// </remarks>
 /// <returns><c>true</c> if the unique identifier range was successfully parsed; otherwise, <c>false.</c>.</returns>
 /// <param name="token">The token to parse.</param>
 /// <param name="range">The unique identifier range.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="token"/> is <c>null</c>.
 /// </exception>
 public static bool TryParse(string token, out UniqueIdRange range)
 {
     return(TryParse(token, 0, out range));
 }
Exemple #5
0
        void BinaryInsert(UniqueId uid)
        {
            int min = 0, max = ranges.Count;
            int i;

            if (max == 0)
            {
                ranges.Add(new UniqueIdRange(uid, uid));
                count++;
                return;
            }

            do
            {
                i = min + ((max - min) / 2);

                if (uid >= ranges[i].Min)
                {
                    if (uid <= ranges[i].Max)
                    {
                        return;
                    }

                    if (uid.Id == ranges[i].Max.Id + 1)
                    {
                        if (i + 1 < ranges.Count && uid.Id + 1 >= ranges[i + 1].Min.Id)
                        {
                            // merge the 2 ranges together
                            ranges[i].Max = ranges[i + 1].Max;
                            ranges.RemoveAt(i + 1);
                            count++;
                            return;
                        }

                        ranges[i].Max = uid;
                        count++;
                        return;
                    }

                    min = i + 1;
                    i   = min;
                }
                else
                {
                    if (uid.Id == ranges[i].Min.Id - 1)
                    {
                        if (i > 0 && uid.Id - 1 <= ranges[i - 1].Max.Id)
                        {
                            // merge the 2 ranges together
                            ranges[i - 1].Max = ranges[i].Max;
                            ranges.RemoveAt(i);
                            count++;
                            return;
                        }

                        ranges[i].Min = uid;
                        count++;
                        return;
                    }

                    max = i;
                }
            } while (min < max);

            var range = new UniqueIdRange(uid, uid);

            if (i < ranges.Count)
            {
                ranges.Insert(i, range);
            }
            else
            {
                ranges.Add(range);
            }

            count++;
        }