Пример #1
0
        internal int AddObjectKey(object value, out bool existing)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if ((object)value == (object)rootObject) // (object) here is no-op, but should be
            {                                        // preserved even if this was typed - needs ref-check
                existing = true;
                return(Root);
            }

            string    s    = value as string;
            BasicList list = List;
            int       index;

#if NO_GENERICS
            if (s == null)
            {
                if (objectKeys == null)
                {
                    objectKeys = new ReferenceHashtable();
                    index      = -1;
                }
                else
                {
                    object tmp = objectKeys[value];
                    index = tmp == null ? -1 : (int)tmp;
                }
            }
            else
            {
                if (stringKeys == null)
                {
                    stringKeys = new Hashtable();
                    index      = -1;
                }
                else
                {
                    object tmp = stringKeys[s];
                    index = tmp == null ? -1 : (int)tmp;
                }
            }
#else
            if (s == null)
            {
#if CF || PORTABLE // CF has very limited proper object ref-tracking; so instead, we'll search it the hard way
                index = list.IndexOfReference(value);
#else
                if (objectKeys == null)
                {
                    objectKeys = new System.Collections.Generic.Dictionary <object, int>(ReferenceComparer.Default);
                    index      = -1;
                }
                else
                {
                    if (!objectKeys.TryGetValue(value, out index))
                    {
                        index = -1;
                    }
                }
#endif
            }
            else
            {
                if (stringKeys == null)
                {
                    stringKeys = new System.Collections.Generic.Dictionary <string, int>();
                    index      = -1;
                }
                else
                {
                    if (!stringKeys.TryGetValue(s, out index))
                    {
                        index = -1;
                    }
                }
            }
#endif

            if (!(existing = index >= 0))
            {
                index = list.Add(value);

                if (s == null)
                {
#if !CF && !PORTABLE // CF can't handle the object keys very well
                    objectKeys.Add(value, index);
#endif
                }
                else
                {
                    stringKeys.Add(s, index);
                }
            }
            return(index + 1);
        }
Пример #2
0
        internal int AddObjectKey(object value, out bool existing)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if ((object)value == (object)rootObject) // (object) here is no-op, but should be
            {                                        // preserved even if this was typed - needs ref-check
                existing = true;
                return(Root);
            }

            string    s    = value as string;
            BasicList list = List;
            int       index;

#if NO_GENERICS
            //index = s == null ? list.IndexOfReference(value) : list.IndexOf(new StringMatch(s));
            if (s == null)
            {
                if (objectKeys == null)
                {
                    objectKeys = new ReferenceHashtable();
                    index      = -1;
                }
                else
                {
                    object tmp = objectKeys[value];
                    index = tmp == null ? -1 : (int)tmp;
                }
            }
            else
            {
                if (stringKeys == null)
                {
                    stringKeys = new Hashtable();
                    index      = -1;
                }
                else
                {
                    object tmp = stringKeys[s];
                    index = tmp == null ? -1 : (int)tmp;
                }
            }
#else
            if (s == null)
            {
                if (objectKeys == null)
                {
                    objectKeys = new System.Collections.Generic.Dictionary <object, int>(ReferenceComparer.Default);
                    index      = -1;
                }
                else
                {
                    if (!objectKeys.TryGetValue(value, out index))
                    {
                        index = -1;
                    }
                }
            }
            else
            {
                if (stringKeys == null)
                {
                    stringKeys = new System.Collections.Generic.Dictionary <string, int>();
                    index      = -1;
                }
                else
                {
                    if (!stringKeys.TryGetValue(s, out index))
                    {
                        index = -1;
                    }
                }
            }
#endif

            if (!(existing = index >= 0))
            {
                index = list.Add(value);

                if (s == null)
                {
                    objectKeys.Add(value, index);
                }
                else
                {
                    stringKeys.Add(s, index);
                }
            }
            return(index + 1);
        }