Пример #1
0
		private bool Set(ArrayList AL, object objectAsKey, bool overwrite, out Exception error)
		{
			bool result = false;
			error = null;
			if (this.KeepUnsorted == false)
			{
				int cmp = 0;
				int middle = 0;
				int left = 0;
				int right = AL.Count;
				object middleObj;
				bool collision = false;

				try
				{
					while (left <= right)
					{
						if (AL.Count == 0)
						{
							middle = 0;
							cmp = 1;
							break;
						}

						int offset = (right - left) / 2; 
						middle = left + offset;

						middleObj = AL[middle];

						cmp = this.Comparer.Compare(middleObj, objectAsKey);

						if ((left == right) || (left == right-1))
						{
							break;
						}

						if (cmp == 0)
						{
							break;
						}
						else if (cmp > 0)
						{
							// middleObj should come after insertThis
							right = middle;
						}
						else if (cmp < 0)
						{
							// middleObj should come before insertThis
							left = middle;
						}
					}

					if (middle >= AL.Count)
					{
						AL.Add(objectAsKey);
					}
					else if (cmp > 0)
					{
						// insert objectAsKey before middleObj
						AL.Insert(middle, objectAsKey);
					}
					else if (cmp < 0)
					{
						// insert objectAsKey after middleObj
						AL.Insert(middle+1, objectAsKey);
					}
					else
					{
						if (this.AllowKeyCollisions)
						{
							// insert objectAsKey before middleObj
							AL.Insert(middle, objectAsKey);
						}
						else
						{
							if (overwrite)
							{
								if (objectAsKey == null)
								{
									AL.RemoveAt(middle);
								}
								else
								{
									AL[middle] = objectAsKey;
								}
								result = true;
							}
							else
							{
								collision = true;
							}
						}
					}
				}
				catch (Exception e)
				{
					error = e;
					return result;
				}

				if (collision)
				{
					error = new KeyCollisionException("_SortedList cannot do a Set() with the provided object because of a collision.");
				}
			}
			else
			{
				if ((this.AllowKeyCollisions) && (overwrite == false))
				{
					// we allow collisions but we don't want to overwrite, so simply continue
					AL.Add(objectAsKey);
					result = true;
				}
				else
				{
					// iterate linearly until we find the correct index to overwrite
					for (int i=0; i < AL.Count; i++)
					{
						object obj = AL[i];
						int cmp = this.Comparer.Compare(obj, objectAsKey);

						if (cmp == 0)
						{
							if (overwrite)
							{
								//items match and we want to overwrite
								AL[i] = objectAsKey;
								result = true;
								break;
							}
							else
							{
								if (this.AllowKeyCollisions == false)
								{
									//items match and we don't allow collisions
									error = new KeyCollisionException("_SortedList cannot do a Set() with the provided object because of a collision.");
								}
							}
						}
						else
						{
							//items don't match so continue searching linearly
						}
					}
					//if we get here and result==false, then we never found
					//a collision so go ahead and add the object
					if (result == false)
					{
						AL.Add(objectAsKey);
						result = true;
					}
				}
			}

			return result;
		}
Пример #2
0
        private bool Set(ArrayList AL, object objectAsKey, bool overwrite, out Exception error)
        {
            bool result = false;

            error = null;
            if (this.KeepUnsorted == false)
            {
                int    cmp    = 0;
                int    middle = 0;
                int    left   = 0;
                int    right  = AL.Count;
                object middleObj;
                bool   collision = false;

                try
                {
                    while (left <= right)
                    {
                        if (AL.Count == 0)
                        {
                            middle = 0;
                            cmp    = 1;
                            break;
                        }

                        int offset = (right - left) / 2;
                        middle = left + offset;

                        middleObj = AL[middle];

                        cmp = this.Comparer.Compare(middleObj, objectAsKey);

                        if ((left == right) || (left == right - 1))
                        {
                            break;
                        }

                        if (cmp == 0)
                        {
                            break;
                        }
                        else if (cmp > 0)
                        {
                            // middleObj should come after insertThis
                            right = middle;
                        }
                        else if (cmp < 0)
                        {
                            // middleObj should come before insertThis
                            left = middle;
                        }
                    }

                    if (middle >= AL.Count)
                    {
                        AL.Add(objectAsKey);
                    }
                    else if (cmp > 0)
                    {
                        // insert objectAsKey before middleObj
                        AL.Insert(middle, objectAsKey);
                    }
                    else if (cmp < 0)
                    {
                        // insert objectAsKey after middleObj
                        AL.Insert(middle + 1, objectAsKey);
                    }
                    else
                    {
                        if (this.AllowKeyCollisions)
                        {
                            // insert objectAsKey before middleObj
                            AL.Insert(middle, objectAsKey);
                        }
                        else
                        {
                            if (overwrite)
                            {
                                if (objectAsKey == null)
                                {
                                    AL.RemoveAt(middle);
                                }
                                else
                                {
                                    AL[middle] = objectAsKey;
                                }
                                result = true;
                            }
                            else
                            {
                                collision = true;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    error = e;
                    return(result);
                }

                if (collision)
                {
                    error = new KeyCollisionException("_SortedList cannot do a Set() with the provided object because of a collision.");
                }
            }
            else
            {
                if ((this.AllowKeyCollisions) && (overwrite == false))
                {
                    // we allow collisions but we don't want to overwrite, so simply continue
                    AL.Add(objectAsKey);
                    result = true;
                }
                else
                {
                    // iterate linearly until we find the correct index to overwrite
                    for (int i = 0; i < AL.Count; i++)
                    {
                        object obj = AL[i];
                        int    cmp = this.Comparer.Compare(obj, objectAsKey);

                        if (cmp == 0)
                        {
                            if (overwrite)
                            {
                                //items match and we want to overwrite
                                AL[i]  = objectAsKey;
                                result = true;
                                break;
                            }
                            else
                            {
                                if (this.AllowKeyCollisions == false)
                                {
                                    //items match and we don't allow collisions
                                    error = new KeyCollisionException("_SortedList cannot do a Set() with the provided object because of a collision.");
                                }
                            }
                        }
                        else
                        {
                            //items don't match so continue searching linearly
                        }
                    }
                    //if we get here and result==false, then we never found
                    //a collision so go ahead and add the object
                    if (result == false)
                    {
                        AL.Add(objectAsKey);
                        result = true;
                    }
                }
            }

            return(result);
        }