dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnetlib/System/Collections/Specialized BitVec


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/System/Collections/Specialized BitVector32.cs,NONE,1.1 CollectionsUtil.cs,NONE,1.1 HybridDictionary.cs,NONE,1.1 NameObjectCollectionBase.cs,1.1,1.2
Date: Tue, 19 Nov 2002 00:04:41 -0500

Update of /cvsroot/dotgnu-pnet/pnetlib/System/Collections/Specialized
In directory subversions:/tmp/cvs-serv28138/System/Collections/Specialized

Modified Files:
        NameObjectCollectionBase.cs 
Added Files:
        BitVector32.cs CollectionsUtil.cs HybridDictionary.cs 
Log Message:


Complete the implementation of the "System.Collections.Specialized" namespace.


--- NEW FILE ---
/*
 * BitVector32.cs - Implementation of
 *              "System.Collections.Specialized.BitVector32".
 *
 * Copyright (C) 2002  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Collections.Specialized
{

#if !ECMA_COMPAT

using System;
using System.Text;

public struct BitVector32
{
        // Internal state.
        private int data;

        // Constructors.
        public BitVector32(int data)
                        {
                                this.data = data;
                        }
        public BitVector32(BitVector32 value)
                        {
                                this.data = value.data;
                        }

        // Create masks.
        public static int CreateMask()
                        {
                                return 1;
                        }
        public static int CreateMask(int previous)
                        {
                                if(previous == 0)
                                {
                                        return 1;
                                }
                                else if(previous == unchecked((int)0x80000000))
                                {
                                        throw new 
Exception(S._("Arg_MaskDone"));
                                }
                                else
                                {
                                        return (previous << 1);
                                }
                        }

        // Get the number of bits in a value.
        private static int GetNumBits(int value)
                        {
                                int bits = 0;
                                value &= 0xFFFF;
                                while(value != 0)
                                {
                                        ++bits;
                                        value >>= 1;
                                }
                                return bits;
                        }

        // Create a bit vector section.
        public static Section CreateSection(short maxValue)
                        {
                                return new Section(GetNumBits(maxValue), 0);
                        }
        public static Section CreateSection(short maxValue, Section section)
                        {
                                return new Section(GetNumBits(maxValue),
                                                                   
section.offset + GetNumBits(section.mask));
                        }

        // Return the full data for this bit vector.
        public int Data
                        {
                                get
                                {
                                        return data;
                                }
                        }

        // Get or set a bit.
        public bool this[int bit]
                        {
                                get
                                {
                                        return ((data & bit) == bit);
                                }
                                set
                                {
                                        if(value)
                                                data |= bit;
                                        else
                                                data &= ~bit;
                                }
                        }
        public int this[Section section]
                        {
                                get
                                {
                                        return (data >> section.offset) & 
section.mask;
                                }
                                set
                                {
                                        int mask = section.mask;
                                        int offset = section.offset;
                                        data = (data & ~(mask << offset)) |
                                                   ((value & mask) << offset);
                                }
                        }

        // Determine if two bit vectors are equal.
        public override bool Equals(Object obj)
                        {
                                if(obj is BitVector32)
                                {
                                        return (data == 
((BitVector32)obj).data);
                                }
                                else
                                {
                                        return false;
                                }
                        }

        // Get the hash code for this bit vector.
        public override int GetHashCode()
                        {
                                return data;
                        }

        // Convert a bit vector into a string.
        public static String ToString(BitVector32 value)
                        {
                                StringBuilder builder = new StringBuilder();
                                builder.Append("BitVector32{");
                                uint data = (uint)(value.data);
                                uint mask = 0x80000000;
                                int bit;
                                for(bit = 0; bit < 32; ++bit)
                                {
                                        if((data & mask) != 0)
                                        {
                                                builder.Append('1');
                                        }
                                        else
                                        {
                                                builder.Append('0');
                                        }
                                        mask >>= 1;
                                }
                                builder.Append('}');
                                return builder.ToString();
                        }
        public override String ToString()
                        {
                                return ToString(this);
                        }

        // Structure that represents a bit vector section.
        public struct Section
        {
                // Internal state.
                internal short mask, offset;

                // Constructor.
                internal Section(int bits, int offset)
                                {
                                        if((bits + offset) > 32)
                                        {
                                                throw new 
Exception(S._("Arg_MaskDone"));
                                        }
                                        this.mask = (short)(bits != 0 ? (1 << 
(bits - 1)) : 0);
                                        this.offset = (short)offset;
                                }

                // Properties.
                public short Mask
                                {
                                        get
                                        {
                                                return mask;
                                        }
                                }
                public short Offset
                                {
                                        get
                                        {
                                                return offset;
                                        }
                                }

                // Determine if two sections are equal.
                public override bool Equals(Object obj)
                                {
                                        if(obj is Section)
                                        {
                                                Section sect = (Section)obj;
                                                return (mask == sect.mask && 
offset == sect.offset);
                                        }
                                        else
                                        {
                                                return false;
                                        }
                                }

                // Get the hash code for this section.
                public override int GetHashCode()
                                {
                                        return mask + offset;
                                }

                // Convert a section into a string.
                public static String ToString(Section value)
                                {
                                        return "Section{0x" + 
Convert.ToString(value.mask, 16) +
                                                        ", 0x" + 
Convert.ToString(value.offset, 16);
                                }
                public override String ToString()
                                {
                                        return ToString(this);
                                }

        }; // struct Section

}; // struct BitVector32

#endif // !ECMA_COMPAT

}; // namespace System.Collections.Specialized

--- NEW FILE ---
/*
 * CollectionsUtil.cs - Implementation of
 *              "System.Collections.Specialized.CollectionsUtil".
 *
 * Copyright (C) 2002  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Collections.Specialized
{

#if !ECMA_COMPAT

using System;
using System.Collections;

public class CollectionsUtil
{

        // Create a case-insensitive hash table.
        public static Hashtable CreateCaseInsensitiveHashtable()
                        {
                                return new 
Hashtable(CaseInsensitiveHashCodeProvider.Default,
                                                                         
CaseInsensitiveComparer.Default);
                        }

        // Create a case-insensitive hash table and add the contents
        // of a dictionary.
        public static Hashtable CreateCaseInsensitiveHashtable(IDictionary d)
                        {
                                return new Hashtable
                                                (d, 
CaseInsensitiveHashCodeProvider.Default,
                                                 
CaseInsensitiveComparer.Default);
                        }

        // Create a case-insensitive hash table with a specific initial 
capacity.
        public static Hashtable CreateCaseInsensitiveHashtable(int capacity)
                        {
                                return new Hashtable
                                                (capacity, 
CaseInsensitiveHashCodeProvider.Default,
                                                 
CaseInsensitiveComparer.Default);
                        }

        // Create a case-insensitive sorted list.
        public static SortedList CreateCaseInsensitiveSortedList()
                        {
                                return new 
SortedList(CaseInsensitiveComparer.Default);
                        }

}; // struct CollectionsUtil

#endif // !ECMA_COMPAT

}; // namespace System.Collections.Specialized

--- NEW FILE ---
/*
 * HybridDictionary.cs - Implementation of
 *              "System.Collections.Specialized.HybridDictionary".
 *
 * Copyright (C) 2002  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Collections.Specialized
{

#if !ECMA_COMPAT

using System;
using System.Collections;
using System.Globalization;

public class HybridDictionary : IDictionary, ICollection, IEnumerable
{

        // Internal state.  The object starts off as a list dictionary
        // and then switches to a hash table when the number of elements
        // exceeds "SwitchOverSize".
        private ListDictionary list;
        private Hashtable hash;
        private bool caseInsensitive;

        // The size at which we switch over.
        private const int SwitchOverSize = 8;

        // Constructors.
        public HybridDictionary() : this(0, false) {}
        public HybridDictionary(bool caseInsensitive)
                        : this(0, caseInsensitive) {}
        public HybridDictionary(int initialSize)
                        : this(initialSize, false) {}
        public HybridDictionary(int initialSize, bool caseInsensitive)
                        {
                                this.caseInsensitive = caseInsensitive;
                                if(initialSize > SwitchOverSize)
                                {
                                        SwitchOver(initialSize);
                                }
                                else
                                {
                                        list = new ListDictionary();
                                }
                        }

        // Switch over to the hash table implementation.
        private void SwitchOver(int size)
                        {
                                if(caseInsensitive)
                                {
                                        hash = new Hashtable
                                                (size, new 
CaseInsensitiveHashCodeProvider
                                                                
(CultureInfo.InvariantCulture),
                                                 new CaseInsensitiveComparer
                                                                
(CultureInfo.InvariantCulture));
                                }
                                else
                                {
                                        hash = new Hashtable(size);
                                }
                                if(list != null)
                                {
                                        IDictionaryEnumerator e = 
list.GetEnumerator();
                                        while(e.MoveNext())
                                        {
                                                hash.Add(e.Key, e.Value);
                                        }
                                        list = null;
                                }
                        }

        // Implement the ICollection interface.
        public void CopyTo(Array array, int index)
                        {
                                if(hash != null)
                                {
                                        hash.CopyTo(array, index);
                                }
                                else
                                {
                                        list.CopyTo(array, index);
                                }
                        }
        public int Count
                        {
                                get
                                {
                                        if(hash != null)
                                        {
                                                return hash.Count;
                                        }
                                        else
                                        {
                                                return list.Count;
                                        }
                                }
                        }
        public bool IsSynchronized
                        {
                                get
                                {
                                        return false;
                                }
                        }
        public Object SyncRoot
                        {
                                get
                                {
                                        return this;
                                }
                        }

        // Implement the IDictionary interface.
        public void Add(Object key, Object value)
                        {
                                if(hash != null)
                                {
                                        hash.Add(key, value);
                                }
                                else if(list.Count < SwitchOverSize)
                                {
                                        list.Add(key, value);
                                }
                                else
                                {
                                        SwitchOver(SwitchOverSize + 1);
                                        hash.Add(key, value);
                                }
                        }
        public void Clear()
                        {
                                if(hash != null)
                                {
                                        hash.Clear();
                                        hash = null;
                                        list = new ListDictionary();
                                }
                                else if(list != null)
                                {
                                        list.Clear();
                                }
                        }
        public bool Contains(Object key)
                        {
                                if(hash != null)
                                {
                                        return hash.Contains(key);
                                }
                                else
                                {
                                        return list.Contains(key);
                                }
                        }
        public IDictionaryEnumerator GetEnumerator()
                        {
                                if(hash != null)
                                {
                                        return hash.GetEnumerator();
                                }
                                else
                                {
                                        return list.GetEnumerator();
                                }
                        }
        public void Remove(Object key)
                        {
                                if(hash != null)
                                {
                                        hash.Remove(key);
                                }
                                else
                                {
                                        list.Remove(key);
                                }
                        }
        public bool IsFixedSize
                        {
                                get
                                {
                                        return false;
                                }
                        }
        public bool IsReadOnly
                        {
                                get
                                {
                                        return false;
                                }
                        }
        public Object this[Object key]
                        {
                                get
                                {
                                        if(hash != null)
                                        {
                                                return hash[key];
                                        }
                                        else
                                        {
                                                return list[key];
                                        }
                                }
                                set
                                {
                                        if(hash != null)
                                        {
                                                hash[key] = value;
                                        }
                                        else
                                        {
                                                list[key] = value;
                                        }
                                }
                        }
        public ICollection Keys
                        {
                                get
                                {
                                        if(hash != null)
                                        {
                                                return hash.Keys;
                                        }
                                        else
                                        {
                                                return list.Keys;
                                        }
                                }
                        }
        public ICollection Values
                        {
                                get
                                {
                                        if(hash != null)
                                        {
                                                return hash.Values;
                                        }
                                        else
                                        {
                                                return list.Values;
                                        }
                                }
                        }

        // Implement the IEnumerable interface.
        IEnumerator IEnumerable.GetEnumerator()
                        {
                                if(hash != null)
                                {
                                        return 
((IEnumerable)hash).GetEnumerator();
                                }
                                else
                                {
                                        return 
((IEnumerable)list).GetEnumerator();
                                }
                        }

}; // class HybridDictionary

#endif // !ECMA_COMPAT

}; // namespace System.Collections.Specialized

Index: NameObjectCollectionBase.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/System/Collections/Specialized/NameObjectCollectionBase.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** NameObjectCollectionBase.cs 18 Nov 2002 07:13:19 -0000      1.1
--- NameObjectCollectionBase.cs 19 Nov 2002 05:04:38 -0000      1.2
***************
*** 96,101 ****
                                get
                                {
!                                       // TODO
!                                       return null;
                                }
                        }
--- 96,100 ----
                                get
                                {
!                                       return new KeysCollection(this);
                                }
                        }





reply via email to

[Prev in Thread] Current Thread [Next in Thread]