Advertisement
Guest User

Untitled

a guest
Oct 29th, 2013
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.10 KB | None | 0 0
  1. import std.stdio;
  2.  
  3. struct tableS
  4. {
  5. public:
  6.     this(uint s)
  7.     {
  8.         table = new char[][size = s];
  9.         nItems = 0;
  10.     }
  11.    
  12.     void insert(char[] id)
  13.     {          
  14.         if (find(id) > -1 || id.length > 32) {
  15.             return;
  16.         }
  17.        
  18.         if (nItems < size - 1) {
  19.             writeln("id:", id, " table:", table);
  20.             uint h = hash(id);
  21.             uint h_new;
  22.             uint i = 0;
  23.  
  24.             do {
  25.                 h_new = (h + i) % size;
  26.                 writeln("hash", ++i, ": ", h_new);
  27.             } while (null != table[h_new]);
  28.  
  29.             table[h_new] = id.dup;
  30.             ++nItems;
  31.         } else {
  32.             throw new Exception("table overflow");
  33.         }
  34.     }
  35.  
  36.     int find(char[] id) {      
  37.         uint h = hash(id);
  38.  
  39.         while (null != table[h]) {
  40.             if (id == table[h]) {
  41.                 return h;
  42.             }
  43.  
  44.             h = (h + 1) % size;
  45.         }
  46.  
  47.         return -1;
  48.     }
  49.  
  50.     void print()
  51.     {
  52.         writeln("find(id) index  id");
  53.        
  54.         foreach (i, id; table) {
  55.             writeln(" ", find(id), "\t   ", i, " -> ", id);
  56.         }
  57.     }
  58.    
  59. private:
  60.     uint hash(char[] id)
  61.     {
  62.         return typeid(id).getHash(&id) % size;
  63.     }
  64.  
  65.     char[][] table;
  66.     uint size, nItems;
  67. }
  68.  
  69. struct tableM
  70. {
  71. public:
  72.     this(uint s)
  73.     {
  74.         table = new char[][size = s];
  75.         nItems = 0;
  76.     }
  77.    
  78.     void insert(char[] id)
  79.     {          
  80.         if (find(id) > -1 || id.length > 32) {
  81.             return;
  82.         }
  83.  
  84.         if (nItems < size - 1) {
  85.             uint h = hash(id);
  86.             uint h_new;
  87.             uint i = 1;
  88.  
  89.             writeln("id:", id, h, " table:", table);
  90.  
  91.             do {
  92.                 h_new = (h * i) % size;
  93.                 writeln("hash", i++, ": ", h_new);
  94.             } while (null != table[h_new]);
  95.  
  96.             table[h_new] = id.dup;
  97.             ++nItems;
  98.         } else {
  99.             throw new Exception("table overflow");
  100.         }
  101.     }
  102.  
  103.     int find(char[] id) {      
  104.         uint h = hash(id);
  105.         uint i = 1;
  106.         uint h_new = (h * i++) % size;
  107.  
  108.         while (null != table[h_new]) {
  109.             if (id == table[h_new]) {
  110.                 return h_new;
  111.             }
  112.  
  113.             h_new = (h * i++) % size;
  114.         }
  115.  
  116.         return -1;
  117.     }
  118.  
  119.     void print()
  120.     {
  121.         writeln("find(id) index  id");
  122.        
  123.         foreach (i, id; table) {
  124.             writeln(" ", find(id), "\t   ", i, " -> ", id);
  125.         }
  126.     }
  127.    
  128. private:
  129.     uint hash(char[] id)
  130.     {
  131.         return 1 + (typeid(id).getHash(&id) % (size - 1));
  132.     }
  133.  
  134.     char[][] table;
  135.     uint size, nItems;
  136. }
  137.  
  138. void main(char[][] args)
  139. {
  140.     try {      
  141.         auto size = 7;
  142.         auto f = File("file", "r");
  143.  
  144. //        auto m = tableM(size);
  145.         auto s = tableS(size);
  146.  
  147.        
  148.         foreach (id; f.byLine()) {
  149. //            m.insert(id);
  150.             s.insert(id);
  151.         }
  152.        
  153. //        m.print();
  154.         s.print();
  155.  
  156.     } catch (Exception e) {
  157.         writeln(e);
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement