home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_se / id_provider.e < prev    next >
Text File  |  1999-06-05  |  4KB  |  152 lines

  1. --          This file is part of SmallEiffel The GNU Eiffel Compiler.
  2. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  3. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr
  4. --                       http://SmallEiffel.loria.fr
  5. -- SmallEiffel is  free  software;  you can  redistribute it and/or modify it
  6. -- under the terms of the GNU General Public License as published by the Free
  7. -- Software  Foundation;  either  version  2, or (at your option)  any  later
  8. -- version. SmallEiffel is distributed in the hope that it will be useful,but
  9. -- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. -- or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU General Public License
  11. -- for  more  details.  You  should  have  received a copy of the GNU General
  12. -- Public  License  along  with  SmallEiffel;  see the file COPYING.  If not,
  13. -- write to the  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. -- Boston, MA 02111-1307, USA.
  15. --
  16. class ID_PROVIDER
  17.    --
  18.    -- Unique object in charge of some id providing.
  19.    --
  20.  
  21. inherit GLOBALS;
  22.  
  23. creation make
  24.  
  25. feature {NONE}
  26.  
  27.    mem_id: FIXED_ARRAY[INTEGER];
  28.  
  29.    mem_str: FIXED_ARRAY[STRING];
  30.  
  31. feature {NONE}
  32.  
  33.    make is
  34.       do
  35.          !!mem_id.with_capacity(1024);
  36.          !!mem_str.with_capacity(1024);
  37.          add2(as_array,0);
  38.          add2(as_general,1);
  39.          add2(as_integer,2);
  40.          add2(as_character,3);
  41.          add2(as_real,4);
  42.          add2(as_double,5);
  43.          add2(as_boolean,6);
  44.          add2(as_string,7);
  45.          add2(as_pointer,8);
  46.          add2(as_native_array_character,9);
  47.       end;
  48.  
  49. feature {SMALL_EIFFEL}
  50.  
  51.    max_id: INTEGER is
  52.       local
  53.          i: INTEGER;
  54.       do
  55.          from
  56.             i := mem_id.upper;
  57.          until
  58.             i < 0
  59.          loop
  60.             if mem_id.item(i) > Result then
  61.                Result := mem_id.item(i);
  62.             end;
  63.             i := i - 1;
  64.          end;
  65.       end;
  66.  
  67. feature {BASE_CLASS,RUN_CLASS}
  68.  
  69.    item(str: STRING): INTEGER is
  70.       require
  71.          str = string_aliaser.item(str)
  72.       local
  73.          index: INTEGER;
  74.       do
  75.          index := mem_str.fast_index_of(str);
  76.          if index <= mem_str.upper then
  77.             Result := mem_id.item(index);
  78.          elseif run_control.boost then
  79.             Result := max_id + 1;
  80.             add2(str,Result);
  81.          else
  82.             Result := hash_code(str);
  83.             if mem_id.fast_has(Result) then
  84.                from
  85.                   Result := 10;
  86.                until
  87.                   not mem_id.fast_has(Result)
  88.                loop
  89.                   Result := Result + 1;
  90.                end;
  91.             end;
  92.             add2(str,Result);
  93.          end;
  94.       end;
  95.  
  96. feature {NONE}
  97.  
  98.    add2(str: STRING; id: INTEGER) is
  99.       do
  100.          mem_str.add_last(str);
  101.          mem_id.add_last(id);
  102.       end;
  103.  
  104. feature {NONE}
  105.  
  106.    hash_code(idf: STRING): INTEGER is
  107.          -- Specific because `idf' is not so long and because it is
  108.          -- often a class name identifier.
  109.          -- To avoid many C recompilation, it is important to avoid
  110.          -- hash-code clashes.
  111.       local
  112.          i: INTEGER;
  113.          c: CHARACTER;
  114.          val: INTEGER;
  115.          max: INTEGER;
  116.       do
  117.          from
  118.             i := 1;
  119.          until
  120.             i > idf.count
  121.          loop
  122.             c := idf.item(i).to_upper;
  123.             inspect
  124.                c
  125.             when 'A' .. 'Z' then
  126.                val := c.code - ('A').code;
  127.             when '0' .. '9' then
  128.                val := 25 + c.code - ('0').code;
  129.             else
  130.                val := 0;
  131.             end;
  132.             Result := Result + (i * val);
  133.             i := i + 1;
  134.          end;
  135.          if Result < 0 then
  136.             Result := - Result;
  137.          end;
  138.          if run_control.boost then
  139.             max := 2048;
  140.          else
  141.             max := 8192;
  142.          end;
  143.          from
  144.          until
  145.             Result <= max
  146.          loop
  147.             Result := Result - 999;
  148.          end;
  149.       end;
  150.  
  151. end -- ID_PROVIDER
  152.