Esempio n. 1
0
 /// <summary>
 /// Adds the vector to the hashtable and returns any previously added tweets with the same hash value
 /// </summary>
 /// <param name="vector"></param>
 /// <returns></returns>
 public List <LSHashTweet> Add(LSHashTweet tweet, out bool anyTrue)
 {
     lock (accessLock)
     {
         CustomBitArray  hash = _hashFunction.CalculateHashScore(tweet.Vector, out anyTrue);
         LSHashTableCell cell;
         if (_values.TryGetValue(hash, out cell))
         {
             List <LSHashTweet> neighbors = cell.GetTweets();
             cell.Add(tweet);
             _lastUpdatedCell = cell;
             if (anyTrue)
             {
                 return(neighbors);
             }
             return(new List <LSHashTweet>());
         }
         else
         {
             cell = new LSHashTableCell(_cellCapacity);
             cell.Add(tweet);
             _values.Add(hash, cell);
             _lastUpdatedCell = cell;
             return(new List <LSHashTweet>());
         }
     }
 }
Esempio n. 2
0
        public void SetNewHashFunction(LSHashFunction hashFunction)
        {
            lock (accessLock)
            {
                Dictionary <CustomBitArray, LSHashTableCell> oldValues = _values;
                _values       = new Dictionary <CustomBitArray, LSHashTableCell>();
                _hashFunction = hashFunction;

                //Re-hash all the old tweets using the new hash function
                foreach (LSHashTableCell oldCell in oldValues.Values)
                {
                    foreach (LSHashTweet tweet in oldCell.GetTweets())
                    {
                        bool            anyTrue = false;
                        CustomBitArray  hash    = _hashFunction.CalculateHashScore(tweet.Vector, out anyTrue);
                        LSHashTableCell cell;
                        if (_values.TryGetValue(hash, out cell))
                        {
                            cell.Add(tweet);
                        }
                        else
                        {
                            cell = new LSHashTableCell(_cellCapacity);
                            cell.Add(tweet);
                            _values.Add(hash, cell);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Function is broken for excessive bits. It only works when those bits are zero.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public uint GetHammingDistance(CustomBitArray value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (this.m_length != value.m_length)
            {
                throw new ArgumentException("ArrayLengthsDiffer");
            }

            uint distance = 0;
            int  num      = (this.m_length + 0x1f) / 0x20;

            for (int i = 0; i < num; i++)
            {
                int val = this.m_array[i] ^ value.m_array[i];

                // Count the number of set bits
                while (val != 0)
                {
                    ++distance;
                    val &= val - 1;
                }
            }

            return(distance);
        }
Esempio n. 4
0
 public CustomBitArray CalculateHashScore(WordVector vector)
 {
     CustomBitArray arr = new CustomBitArray(_hyperPlanes.Count);
     for (int i = _hyperPlanes.Count - 1; i != -1; i--)
         arr[i] = (_hyperPlanes[i] * vector) >= 0;
     return arr;
 }
Esempio n. 5
0
        public object Clone()
        {
            CustomBitArray array = new CustomBitArray(this.m_array);

            array._version = this._version;
            array.m_length = this.m_length;
            return(array);
        }
Esempio n. 6
0
        public CustomBitArray CalculateHashScore(WordVector vector)
        {
            CustomBitArray arr = new CustomBitArray(_hyperPlanes.Count);

            for (int i = _hyperPlanes.Count - 1; i != -1; i--)
            {
                arr[i] = (_hyperPlanes[i] * vector) >= 0;
            }
            return(arr);
        }
Esempio n. 7
0
 public CustomBitArray CalculateHashScore(WordVector vector, out bool anyTrue)
 {
     anyTrue = false;
     CustomBitArray arr = new CustomBitArray(_hyperPlanes.Count);
     for (int i = _hyperPlanes.Count - 1; i != -1; i--)
     {
         anyTrue |= (arr[i] = (_hyperPlanes[i] * vector) > 0);
     }
     return arr;
 }
Esempio n. 8
0
        public CustomBitArray CalculateHashScore(WordVector vector, out bool anyTrue)
        {
            anyTrue = false;
            CustomBitArray arr = new CustomBitArray(_hyperPlanes.Count);

            for (int i = _hyperPlanes.Count - 1; i != -1; i--)
            {
                anyTrue |= (arr[i] = (_hyperPlanes[i] * vector) > 0);
            }
            return(arr);
        }
Esempio n. 9
0
 public CustomBitArray(CustomBitArray bits)
 {
     if (bits == null)
     {
         throw new ArgumentNullException("bits");
     }
     this.m_array  = new int[(bits.m_length + 0x1f) / 0x20];
     this.m_length = bits.m_length;
     Array.Copy(bits.m_array, this.m_array, (int)((bits.m_length + 0x1f) / 0x20));
     this._version = bits._version;
 }
Esempio n. 10
0
        public CustomBitArray Xor(CustomBitArray value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (this.m_length != value.m_length)
            {
                throw new ArgumentException("ArrayLengthsDiffer");
            }
            int num = (this.m_length + 0x1f) / 0x20;

            for (int i = 0; i < num; i++)
            {
                this.m_array[i] ^= value.m_array[i];
            }
            this._version++;
            return(this);
        }
Esempio n. 11
0
        public override bool Equals(object obj)
        {
            if (obj == null || !(obj is CustomBitArray))
            {
                return(false);
            }
            CustomBitArray value = (CustomBitArray)obj;

            if (this.m_length != value.m_length)
            {
                return(false);
            }
            int num = (this.m_length + 0x1f) / 0x20;

            for (int i = 0; i < num; i++)
            {
                if (this.m_array[i] != value.m_array[i])
                {
                    return(false);
                }
            }
            return(true);
        }
Esempio n. 12
0
 public CustomBitArray(CustomBitArray bits)
 {
     if (bits == null)
     {
         throw new ArgumentNullException("bits");
     }
     this.m_array = new int[(bits.m_length + 0x1f) / 0x20];
     this.m_length = bits.m_length;
     Array.Copy(bits.m_array, this.m_array, (int)((bits.m_length + 0x1f) / 0x20));
     this._version = bits._version;
 }
Esempio n. 13
0
 // Methods
 internal BitArrayEnumeratorSimple(CustomBitArray bitarray)
 {
     this.bitarray = bitarray;
     this.index = -1;
     this.version = bitarray._version;
 }
Esempio n. 14
0
 public CustomBitArray Xor(CustomBitArray value)
 {
     if (value == null)
     {
         throw new ArgumentNullException("value");
     }
     if (this.m_length != value.m_length)
     {
         throw new ArgumentException("ArrayLengthsDiffer");
     }
     int num = (this.m_length + 0x1f) / 0x20;
     for (int i = 0; i < num; i++)
     {
         this.m_array[i] ^= value.m_array[i];
     }
     this._version++;
     return this;
 }
Esempio n. 15
0
        /// <summary>
        /// Function is broken for excessive bits. It only works when those bits are zero.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public uint GetHammingDistance(CustomBitArray value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (this.m_length != value.m_length)
            {
                throw new ArgumentException("ArrayLengthsDiffer");
            }

            uint distance = 0;
            int num = (this.m_length + 0x1f) / 0x20;
            for (int i = 0; i < num; i++)
            {
                int val = this.m_array[i] ^ value.m_array[i];

                // Count the number of set bits
                while (val != 0)
                {
                    ++distance;
                    val &= val - 1;
                }
            }

            return distance;
        }
Esempio n. 16
0
 public object Clone()
 {
     CustomBitArray array = new CustomBitArray(this.m_array);
     array._version = this._version;
     array.m_length = this.m_length;
     return array;
 }
Esempio n. 17
0
 // Methods
 internal BitArrayEnumeratorSimple(CustomBitArray bitarray)
 {
     this.bitarray = bitarray;
     this.index    = -1;
     this.version  = bitarray._version;
 }