public T FindValue(Func <T, int> CompareResult)
        {
            RWLock.EnterReadLock();
            try
            {
                int a = 0;
                int b = base.Count;
                while (true)
                {
                    if (a == b)
                    {
                        return(default(T));
                    }

                    int mid = a + ((b - a) / 2);
                    switch (CompareResult(base[mid]))
                    {
                    case -1:
                        a = mid + 1;
                        break;

                    case 1:
                        b = mid;
                        break;

                    case 0:
                        return(base[mid]);
                    }
                }
            }
            finally  {
                RWLock.ExitReadLock();
            }
        }
        /// <summary>
        /// This always returns a value, no bounds checks required
        /// Except when the list is empty, returns default(T)
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public T FindSafe(T search)
        {
            RWLock.EnterReadLock();

            try
            {
                if (base.Count == 0)
                {
                    return(default(T));
                }

                EnsureComparer();

                int a = 0;
                int b = base.Count;
                while (b - a > 1)
                {
                    int mid = (a + b) / 2;
                    if (Comparer.Compare(base[mid], search) > 0)
                    {
                        b = mid;
                    }
                    else
                    {
                        a = mid;
                    }
                }

                return(base[a]);
            }
            finally {
                RWLock.ExitReadLock();
            }
        }
 /*** ***/
 public new T[] ToArray()
 {
     RWLock.EnterReadLock();
     try {
         return(base.ToArray());
     }
     finally {
         RWLock.ExitReadLock();
     }
 }
 public int FirstIndexOf(T item)
 {
     RWLock.EnterReadLock();
     try {
         return(InternalFirstIndexOf(item));
     }
     finally {
         RWLock.ExitReadLock();
     }
 }
 public int IndexOfElementOrSuccessor(T item)
 {
     RWLock.EnterReadLock();
     try {
         return(InternalIndexOfElementOrSuccessor(item));
     }
     finally {
         RWLock.ExitReadLock();
     }
 }
 public new T this[int index]
 {
     get{
         RWLock.EnterReadLock();
         try {
             return(base[index]);
         }
         finally {
             RWLock.ExitReadLock();
         }
     }
 }
Пример #7
0
        public new ThreadsafeEnumerator <T> GetEnumerator()
        {
            ThreadsafeEnumerator <T> enu;

            RWLock.EnterReadLock();
            try {
                enu = new ThreadsafeEnumerator <T>(this.ToArray(), Count);
            } finally {
                RWLock.ExitReadLock();
            }
            return(enu);
        }
 /// <summary>
 /// returns IndexOf or -1
 /// </summary>
 /// <param name="item"></param>
 /// <returns></returns>
 public new int IndexOf(T item)
 {
     RWLock.EnterReadLock();
     try {
         int idx = InternalIndexOf(item);
         if (idx < 0 || idx > base.Count)
         {
             return(-1);
         }
         return(idx);
     }
     finally {
         RWLock.ExitReadLock();
     }
 }
 public T FindElementOrSuccessor(T item)
 {
     RWLock.EnterReadLock();
     try {
         int index = InternalIndexOfElementOrSuccessor(item);
         if (index >= base.Count)
         {
             return(default(T));
         }
         else
         {
             return(base[index]);
         }
     }
     finally {
         RWLock.ExitReadLock();
     }
 }
        /// <summary>
        /// This always returns a value, no bounds checks required
        /// Except when the list is empty, returns default(T)
        /// </summary>
        /// <param name="elem"></param>
        /// <returns></returns>
        public T Find(T elem)
        {
            RWLock.EnterReadLock();

            try
            {
                int i = InternalIndexOf(elem);
                if (i >= 0 && i < base.Count)
                {
                    return(base[i]);
                }

                return(default(T));
            }
            finally {
                RWLock.ExitReadLock();
            }
        }
Пример #11
0
            //try to read
            private void try_read()
            {
                RWLock.EnterReadLock();

                check_pause();//system call pause;
                status = (int)global.StatusType.run;

                critical();

                RWLock.ExitReadLock();

                check_pause();
                //add to done
                global.mutexR.WaitOne();
                global.done_reader++;
                global.mutexR.ReleaseMutex();

                status = (int)global.StatusType.done;
                Console.WriteLine(ID.ToString() + " finish the read_job");
            }
        public new int LastIndexOf(T item)
        {
            RWLock.EnterReadLock();
            try {
                EnsureComparer();

                int a = 0;
                int b = base.Count - 1;
                while (a <= b)
                {
                    int mid = a + ((b - a) / 2);
                    switch (Comparer.Compare(base[mid], item))
                    {
                    case 1:
                        b = mid - 1;
                        break;

                    default:
                        a = mid + 1;
                        break;
                    }
                }

                if (b < 0)
                {
                    return(~(b + 1));
                }
                else if (a > b)
                {
                    return(~a);
                }


                return(b);
            }
            finally {
                RWLock.ExitReadLock();
            }
        }