| OLD | NEW |
| 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 1868 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1879 // information by subclasses. | 1879 // information by subclasses. |
| 1880 | 1880 |
| 1881 template<typename Shape, typename Key> | 1881 template<typename Shape, typename Key> |
| 1882 class HashTable: public FixedArray { | 1882 class HashTable: public FixedArray { |
| 1883 public: | 1883 public: |
| 1884 // Returns the number of elements in the hash table. | 1884 // Returns the number of elements in the hash table. |
| 1885 int NumberOfElements() { | 1885 int NumberOfElements() { |
| 1886 return Smi::cast(get(kNumberOfElementsIndex))->value(); | 1886 return Smi::cast(get(kNumberOfElementsIndex))->value(); |
| 1887 } | 1887 } |
| 1888 | 1888 |
| 1889 // Returns the number of deleted elements in the hash table. |
| 1890 int NumberOfDeletedElements() { |
| 1891 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value(); |
| 1892 } |
| 1893 |
| 1889 // Returns the capacity of the hash table. | 1894 // Returns the capacity of the hash table. |
| 1890 int Capacity() { | 1895 int Capacity() { |
| 1891 return Smi::cast(get(kCapacityIndex))->value(); | 1896 return Smi::cast(get(kCapacityIndex))->value(); |
| 1892 } | 1897 } |
| 1893 | 1898 |
| 1894 // ElementAdded should be called whenever an element is added to a | 1899 // ElementAdded should be called whenever an element is added to a |
| 1895 // hash table. | 1900 // hash table. |
| 1896 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); } | 1901 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); } |
| 1897 | 1902 |
| 1898 // ElementRemoved should be called whenever an element is removed from | 1903 // ElementRemoved should be called whenever an element is removed from |
| 1899 // a hash table. | 1904 // a hash table. |
| 1900 void ElementRemoved() { SetNumberOfElements(NumberOfElements() - 1); } | 1905 void ElementRemoved() { |
| 1901 void ElementsRemoved(int n) { SetNumberOfElements(NumberOfElements() - n); } | 1906 SetNumberOfElements(NumberOfElements() - 1); |
| 1907 SetNumberOfDeletedElements(NumberOfDeletedElements() + 1); |
| 1908 } |
| 1909 void ElementsRemoved(int n) { |
| 1910 SetNumberOfElements(NumberOfElements() - n); |
| 1911 SetNumberOfDeletedElements(NumberOfDeletedElements() + n); |
| 1912 } |
| 1902 | 1913 |
| 1903 // Returns a new HashTable object. Might return Failure. | 1914 // Returns a new HashTable object. Might return Failure. |
| 1904 static Object* Allocate(int at_least_space_for); | 1915 static Object* Allocate(int at_least_space_for); |
| 1905 | 1916 |
| 1906 // Returns the key at entry. | 1917 // Returns the key at entry. |
| 1907 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); } | 1918 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); } |
| 1908 | 1919 |
| 1909 // Tells whether k is a real key. Null and undefined are not allowed | 1920 // Tells whether k is a real key. Null and undefined are not allowed |
| 1910 // as keys and can be used to indicate missing or deleted elements. | 1921 // as keys and can be used to indicate missing or deleted elements. |
| 1911 bool IsKey(Object* k) { | 1922 bool IsKey(Object* k) { |
| 1912 return !k->IsNull() && !k->IsUndefined(); | 1923 return !k->IsNull() && !k->IsUndefined(); |
| 1913 } | 1924 } |
| 1914 | 1925 |
| 1915 // Garbage collection support. | 1926 // Garbage collection support. |
| 1916 void IteratePrefix(ObjectVisitor* visitor); | 1927 void IteratePrefix(ObjectVisitor* visitor); |
| 1917 void IterateElements(ObjectVisitor* visitor); | 1928 void IterateElements(ObjectVisitor* visitor); |
| 1918 | 1929 |
| 1919 // Casting. | 1930 // Casting. |
| 1920 static inline HashTable* cast(Object* obj); | 1931 static inline HashTable* cast(Object* obj); |
| 1921 | 1932 |
| 1922 // Compute the probe offset (quadratic probing). | 1933 // Compute the probe offset (quadratic probing). |
| 1923 INLINE(static uint32_t GetProbeOffset(uint32_t n)) { | 1934 INLINE(static uint32_t GetProbeOffset(uint32_t n)) { |
| 1924 return (n + n * n) >> 1; | 1935 return (n + n * n) >> 1; |
| 1925 } | 1936 } |
| 1926 | 1937 |
| 1927 static const int kNumberOfElementsIndex = 0; | 1938 static const int kNumberOfElementsIndex = 0; |
| 1928 static const int kCapacityIndex = 1; | 1939 static const int kNumberOfDeletedElementsIndex = 1; |
| 1929 static const int kPrefixStartIndex = 2; | 1940 static const int kCapacityIndex = 2; |
| 1930 static const int kElementsStartIndex = | 1941 static const int kPrefixStartIndex = 3; |
| 1942 static const int kElementsStartIndex = |
| 1931 kPrefixStartIndex + Shape::kPrefixSize; | 1943 kPrefixStartIndex + Shape::kPrefixSize; |
| 1932 static const int kEntrySize = Shape::kEntrySize; | 1944 static const int kEntrySize = Shape::kEntrySize; |
| 1933 static const int kElementsStartOffset = | 1945 static const int kElementsStartOffset = |
| 1934 kHeaderSize + kElementsStartIndex * kPointerSize; | 1946 kHeaderSize + kElementsStartIndex * kPointerSize; |
| 1935 | 1947 |
| 1936 // Constant used for denoting a absent entry. | 1948 // Constant used for denoting a absent entry. |
| 1937 static const int kNotFound = -1; | 1949 static const int kNotFound = -1; |
| 1938 | 1950 |
| 1939 // Find entry for key otherwise return -1. | 1951 // Find entry for key otherwise return -1. |
| 1940 int FindEntry(Key key); | 1952 int FindEntry(Key key); |
| 1941 | 1953 |
| 1942 protected: | 1954 protected: |
| 1943 | 1955 |
| 1944 // Find the entry at which to insert element with the given key that | 1956 // Find the entry at which to insert element with the given key that |
| 1945 // has the given hash value. | 1957 // has the given hash value. |
| 1946 uint32_t FindInsertionEntry(uint32_t hash); | 1958 uint32_t FindInsertionEntry(uint32_t hash); |
| 1947 | 1959 |
| 1948 // Returns the index for an entry (of the key) | 1960 // Returns the index for an entry (of the key) |
| 1949 static inline int EntryToIndex(int entry) { | 1961 static inline int EntryToIndex(int entry) { |
| 1950 return (entry * kEntrySize) + kElementsStartIndex; | 1962 return (entry * kEntrySize) + kElementsStartIndex; |
| 1951 } | 1963 } |
| 1952 | 1964 |
| 1953 // Update the number of elements in the hash table. | 1965 // Update the number of elements in the hash table. |
| 1954 void SetNumberOfElements(int nof) { | 1966 void SetNumberOfElements(int nof) { |
| 1955 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof)); | 1967 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof)); |
| 1956 } | 1968 } |
| 1957 | 1969 |
| 1970 // Update the number of deleted elements in the hash table. |
| 1971 void SetNumberOfDeletedElements(int nod) { |
| 1972 fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod)); |
| 1973 } |
| 1974 |
| 1958 // Sets the capacity of the hash table. | 1975 // Sets the capacity of the hash table. |
| 1959 void SetCapacity(int capacity) { | 1976 void SetCapacity(int capacity) { |
| 1960 // To scale a computed hash code to fit within the hash table, we | 1977 // To scale a computed hash code to fit within the hash table, we |
| 1961 // use bit-wise AND with a mask, so the capacity must be positive | 1978 // use bit-wise AND with a mask, so the capacity must be positive |
| 1962 // and non-zero. | 1979 // and non-zero. |
| 1963 ASSERT(capacity > 0); | 1980 ASSERT(capacity > 0); |
| 1964 fast_set(this, kCapacityIndex, Smi::FromInt(capacity)); | 1981 fast_set(this, kCapacityIndex, Smi::FromInt(capacity)); |
| 1965 } | 1982 } |
| 1966 | 1983 |
| 1967 | 1984 |
| (...skipping 2915 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4883 } else { | 4900 } else { |
| 4884 value &= ~(1 << bit_position); | 4901 value &= ~(1 << bit_position); |
| 4885 } | 4902 } |
| 4886 return value; | 4903 return value; |
| 4887 } | 4904 } |
| 4888 }; | 4905 }; |
| 4889 | 4906 |
| 4890 } } // namespace v8::internal | 4907 } } // namespace v8::internal |
| 4891 | 4908 |
| 4892 #endif // V8_OBJECTS_H_ | 4909 #endif // V8_OBJECTS_H_ |
| OLD | NEW |