ch_map.h
上传用户:gzelex
上传日期:2007-01-07
资源大小:707k
文件大小:3k
开发平台:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  ch_map.h
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. #ifndef LEDA_CH_MAP_H
  12. #define LEDA_CH_MAP_H
  13. //------------------------------------------------------------------------------
  14. // Hashing Map with Chaining & Table Doubling
  15. //
  16. // S. Naeher  (1994)
  17. //
  18. //------------------------------------------------------------------------------
  19. #include <LEDA/basic.h>
  20.  
  21. //------------------------------------------------------------------------------
  22. // class ch_map_elem  
  23. //------------------------------------------------------------------------------
  24. class ch_map_elem 
  25. {
  26.   friend class ch_map;
  27.   unsigned long    k;
  28.   GenPtr           i;
  29.   ch_map_elem*  succ;
  30. };
  31. typedef ch_map_elem*  ch_map_item;
  32. //--------------------------------------------------------------------
  33. // class ch_map
  34. //--------------------------------------------------------------------
  35. class ch_map 
  36. {
  37.    static ch_map_elem STOP;
  38.    ch_map_elem* table;
  39.    ch_map_elem* table_end;
  40.    ch_map_elem* free;
  41.    ch_map_elem* iterator;
  42.    int table_size;           
  43.    int table_size_1;           
  44.    int shift;
  45.    virtual void clear_inf(GenPtr&)  const { }
  46.    virtual void copy_inf(GenPtr&)   const { }
  47.    virtual void init_inf(GenPtr&)   const { }
  48. /*
  49.    ch_map_elem*  HASH(unsigned long x)  const
  50.    { return table + ((x>>shift) & table_size_1);  }
  51. */
  52.    ch_map_elem*  HASH(unsigned long x)  const
  53.    { return table + (x & table_size_1);  }
  54.    void init_table(int);
  55.    void rehash();
  56.    GenPtr& access1(ch_map_item, unsigned long);
  57.    public:
  58.    void clear_entries();
  59.    GenPtr& access(unsigned long);
  60.    GenPtr  access(unsigned long) const;
  61.    GenPtr  lookup(unsigned long) const;
  62.    void print();
  63.    void start_iteration();
  64.    bool next_index(unsigned long&);
  65.    ch_map_item first_item() const { return 0; }
  66.    ch_map_item next_item(ch_map_item) const { return 0; }
  67.    ch_map& operator=(const ch_map&);
  68.    ch_map(const ch_map&);
  69.    ch_map(int s = 0, int n=1024); 
  70.    virtual ~ch_map() { delete[] table; }
  71. };
  72. inline GenPtr& ch_map::access(unsigned long x)
  73.   ch_map_item p = HASH(x);
  74.   if (p->k == x) 
  75.     return p->i; 
  76.   else
  77.     if (p->k == 0xFFFFFFFF) // NIL
  78.        { p->k = x;
  79.          init_inf(p->i);
  80.          return p->i;
  81.         }
  82.      else
  83.        return access1(p,x);
  84.  }
  85. #endif