about summary refs log tree commit diff
path: root/src/rt/util
diff options
context:
space:
mode:
authorJesse Jones <jesse9jones@gmail.com>2012-12-08 20:10:39 -0800
committerBrian Anderson <banderson@mozilla.com>2012-12-15 22:38:19 -0800
commitfc740a729721d5063be367de99b5fcb62a99f2d7 (patch)
tree63ce2f13270844ce033d4a32bec4d9a79225430b /src/rt/util
parentd4daa78aa374c3f129e2fbff3ad57484e9d4b8e1 (diff)
downloadrust-fc740a729721d5063be367de99b5fcb62a99f2d7.tar.gz
rust-fc740a729721d5063be367de99b5fcb62a99f2d7.zip
Improvements to array_list, hash_map, and indexed_list:
* Disabled copying.
* Added const where appropiate.
Diffstat (limited to 'src/rt/util')
-rw-r--r--src/rt/util/array_list.h31
-rw-r--r--src/rt/util/hash_map.h18
-rw-r--r--src/rt/util/indexed_list.h12
3 files changed, 45 insertions, 16 deletions
diff --git a/src/rt/util/array_list.h b/src/rt/util/array_list.h
index 41cb6426691..a62a8c5c8db 100644
--- a/src/rt/util/array_list.h
+++ b/src/rt/util/array_list.h
@@ -16,25 +16,32 @@
 #include <stddef.h>
 
 /**
- * A simple, resizable array list.
+ * A simple, resizable array list. Note that this only works with POD types
+ * (because data is grown via realloc).
  */
 template<typename T> class array_list {
     static const size_t INITIAL_CAPACITY = 8;
     size_t _size;
     T * _data;
     size_t _capacity;
+private:
+    // private and left undefined to disable copying
+    array_list(const array_list& rhs);
+    array_list& operator=(const array_list& rhs);
 public:
     array_list();
     ~array_list();
-    size_t size();
+    size_t size() const;
     int32_t append(T value);
     int32_t push(T value);
     bool pop(T *value);
     bool replace(T old_value, T new_value);
-    int32_t index_of(T value);
-    bool is_empty();
+    int32_t index_of(T value) const;
+    bool is_empty() const;
     T* data();
+    const T* data() const;
     T & operator[](size_t index);
+    const T & operator[](size_t index) const;
 };
 
 template<typename T>
@@ -50,7 +57,7 @@ array_list<T>::~array_list() {
 }
 
 template<typename T> size_t
-array_list<T>::size() {
+array_list<T>::size() const {
     return _size;
 }
 
@@ -97,7 +104,7 @@ array_list<T>::replace(T old_value, T new_value) {
 }
 
 template<typename T> int32_t
-array_list<T>::index_of(T value) {
+array_list<T>::index_of(T value) const {
     for (size_t i = 0; i < _size; i++) {
         if (_data[i] == value) {
             return i;
@@ -111,8 +118,13 @@ array_list<T>::operator[](size_t index) {
     return _data[index];
 }
 
+template<typename T> const T &
+array_list<T>::operator[](size_t index) const {
+    return _data[index];
+}
+
 template<typename T> bool
-array_list<T>::is_empty() {
+array_list<T>::is_empty() const {
     return _size == 0;
 }
 
@@ -121,4 +133,9 @@ array_list<T>::data() {
     return _data;
 }
 
+template<typename T> const T*
+array_list<T>::data() const {
+    return _data;
+}
+
 #endif /* ARRAY_LIST_H */
diff --git a/src/rt/util/hash_map.h b/src/rt/util/hash_map.h
index 6e8afbece2d..253a7a06fb7 100644
--- a/src/rt/util/hash_map.h
+++ b/src/rt/util/hash_map.h
@@ -26,6 +26,10 @@ template<typename K, typename V> class hash_map {
         UT_hash_handle hh;
     };
     map_entry * _head;
+private:
+    // private and left undefined to disable copying
+    hash_map(const hash_map& rhs);
+    hash_map& operator=(const hash_map& rhs);
 public:
     hash_map();
     ~hash_map();
@@ -54,7 +58,7 @@ public:
      * true if the value was found and updates the specified *value parameter
      * with the associated value, or false otherwise.
      */
-    bool get(K key, V *value);
+    bool get(K key, V *value) const;
 
     /**
      * Removes a key-value pair from this hash map.
@@ -71,7 +75,7 @@ public:
      * returns:
      * true if the specified key exists in this hash map, or false otherwise.
      */
-    bool contains(K key);
+    bool contains(K key) const;
 
     /**
      * Removes the value associated with the specified key from this hash map.
@@ -86,9 +90,9 @@ public:
     /**
      * Returns the number of key-value pairs in this hash map.
      */
-    size_t count();
+    size_t count() const;
 
-    bool is_empty() {
+    bool is_empty() const {
         return count() == 0;
     }
 
@@ -124,7 +128,7 @@ hash_map<K,V>::put(K key, V value) {
 }
 
 template<typename K, typename V> bool
-hash_map<K,V>::get(K key, V *value) {
+hash_map<K,V>::get(K key, V *value) const {
     map_entry *entry = NULL;
     HASH_FIND(hh, _head, &key, sizeof(K), entry);
     if (entry == NULL) {
@@ -146,7 +150,7 @@ hash_map<K,V>::set(K key, V value) {
 }
 
 template<typename K, typename V> bool
-hash_map<K,V>::contains(K key) {
+hash_map<K,V>::contains(K key) const {
     V value;
     return get(key, &value);
 }
@@ -184,7 +188,7 @@ hash_map<K,V>::remove(K key) {
 }
 
 template<typename K, typename V> size_t
-hash_map<K,V>::count() {
+hash_map<K,V>::count() const {
     return HASH_CNT(hh, _head);
 }
 
diff --git a/src/rt/util/indexed_list.h b/src/rt/util/indexed_list.h
index aae6ecb8a78..51e94b8ba26 100644
--- a/src/rt/util/indexed_list.h
+++ b/src/rt/util/indexed_list.h
@@ -45,14 +45,15 @@ public:
      * Same as pop(), except that it returns NULL if the list is empty.
      */
     virtual T* pop_value();
-    virtual size_t length() {
+    virtual size_t length() const {
         return list.size();
     }
-    virtual bool is_empty() {
+    virtual bool is_empty() const {
         return list.is_empty();
     }
     virtual int32_t remove(T* value);
     virtual T * operator[](int32_t index);
+    virtual const T * operator[](int32_t index) const;
     virtual ~indexed_list() {}
 };
 
@@ -104,4 +105,11 @@ indexed_list<T>::operator[](int32_t index) {
     return value;
 }
 
+template <typename T> const T *
+indexed_list<T>::operator[](int32_t index) const {
+    T *value = list[index];
+    assert(value->list_index == index);
+    return value;
+}
+
 #endif /* INDEXED_LIST_H */