]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/include/hash-table.hh
release: 1.3.33
[lilypond.git] / flower / include / hash-table.hh
index 3ce52c944ca2a43e066c95282822aca08ce6d8f6..bd437e6e972d7215f9546fcce539d789cbd809b0 100644 (file)
@@ -12,6 +12,7 @@
 
 unsigned int int_hash (int);
 unsigned long prime_list (int idx);
+template<class K, class V> struct Hash_table_iter;
 
 template<class K>
 unsigned int
@@ -60,54 +61,30 @@ public:
   int lookup  (K s, unsigned int initial_hash)
     {
       int sz =dict_arr_.size ();
-      int i = initial_hash % sz;
+      initial_hash = initial_hash % sz; 
+      int i;
       int j = 0;
       while (j <= sz/2) {
+       i = (initial_hash + j*j) % sz;
+       
        if (dict_arr_[i].free_b_)
          return i;
-           
+
        if (dict_arr_[i].key_ == s)
          return i;
 
        j++;
-       i = (i + j*j) % sz;
       }
 
+      
       return -1;
     }
 
   /// remove #s# from the hash table.  
   V remove (K s, unsigned int initial_hash)
     {
-      int sz =dict_arr_.size ();
-      int i = initial_hash % sz;
-      int j = 0;
-      V retval;
-
-      /*
-       duplicate with lookup, but we need value of j
-       */
-      
-      while (j <= sz/2 && dict_arr_[i].key_ != s)
-       {
-         assert (!dict_arr_[i].free_b_);
-
-         j ++;
-         i = (i + j*j) % sz;
-       }
-
-      j++;
-      int nexti = (i + j*j) % sz;
-
-      while (j <= sz/2 && !dict_arr_[i].free_b_)
-       {
-         dict_arr_[i] = dict_arr_[nexti];
-         j++;
-         i = nexti;
-         nexti = (nexti + j*j)%sz;
-       }
-      
-      return retval;
+      // TODO
+      assert (false);
     }
 };
 
@@ -176,8 +153,8 @@ public:
     }
 
   /**
-     Find and return element.  If #s# is not in the table, create an entry in the table, and init
-   */
+     Find and return element.  If #s# is not in the table, create an
+     entry in the table, and init */
   V& elem (K s)
     {
       int l;
@@ -191,6 +168,17 @@ public:
        fixed_p_->dict_arr_[l].key_ = s;
        return fixed_p_->dict_arr_[l].value_;
     }
+  bool try_retrieve (K k, V *v)
+  {
+    int l =  fixed_p_->lookup (k, (*hash_func_)(k));
+    if (l < 0 || fixed_p_->dict_arr_[l].free_b_)
+      return false;
+    else
+      {
+       *v = fixed_p_->dict_arr_[l].value_;
+       return true;
+      }
+  }
   V elem (K s) const
     {
       return const_elem (s);