Discussão:Código Natural/Representação interna

De Documentação

Gerador didático

Usar select * from natcod.generate_vbit_series_didactic(4) para destacar a necessidade de uso do lval:

  s   | len | rval | lval | lval_rotated+len | rval_bin | lval_bin | len_bin 
------+-----+------+------+------------------+----------+----------+---------
 0    |   1 |    0 |    0 |                1 | 0000     | 0000     | 0001
 00   |   2 |    0 |    0 |                2 | 0000     | 0000     | 0010
 000  |   3 |    0 |    0 |                3 | 0000     | 0000     | 0011
 0000 |   4 |    0 |    0 |                4 | 0000     | 0000     | 0100
 0001 |   4 |    1 |    1 |               12 | 0001     | 0001     | 0100
 001  |   3 |    1 |    2 |               19 | 0001     | 0010     | 0011
 0010 |   4 |    2 |    2 |               20 | 0010     | 0010     | 0100
 0011 |   4 |    3 |    3 |               28 | 0011     | 0011     | 0100
 01   |   2 |    1 |    4 |               34 | 0001     | 0100     | 0010
 010  |   3 |    2 |    4 |               35 | 0010     | 0100     | 0011
 0100 |   4 |    4 |    4 |               36 | 0100     | 0100     | 0100
 0101 |   4 |    5 |    5 |               44 | 0101     | 0101     | 0100
 011  |   3 |    3 |    6 |               51 | 0011     | 0110     | 0011
 0110 |   4 |    6 |    6 |               52 | 0110     | 0110     | 0100
 0111 |   4 |    7 |    7 |               60 | 0111     | 0111     | 0100
 1    |   1 |    1 |    8 |               65 | 0001     | 1000     | 0001
 10   |   2 |    2 |    8 |               66 | 0010     | 1000     | 0010
 100  |   3 |    4 |    8 |               67 | 0100     | 1000     | 0011
 1000 |   4 |    8 |    8 |               68 | 1000     | 1000     | 0100
 1001 |   4 |    9 |    9 |               76 | 1001     | 1001     | 0100
 101  |   3 |    5 |   10 |               83 | 0101     | 1010     | 0011
 1010 |   4 |   10 |   10 |               84 | 1010     | 1010     | 0100
 1011 |   4 |   11 |   11 |               92 | 1011     | 1011     | 0100
 11   |   2 |    3 |   12 |               98 | 0011     | 1100     | 0010
 110  |   3 |    6 |   12 |               99 | 0110     | 1100     | 0011
 1100 |   4 |   12 |   12 |              100 | 1100     | 1100     | 0100
 1101 |   4 |   13 |   13 |              108 | 1101     | 1101     | 0100
 111  |   3 |    7 |   14 |              115 | 0111     | 1110     | 0011
 1110 |   4 |   14 |   14 |              116 | 1110     | 1110     | 0100
 1111 |   4 |   15 |   15 |              124 | 1111     | 1111     | 0100

Geração dos exemplos

select s, len,  rval, lval, rval_bin, lval_bin FROM natcod.generate_vbit_series_didactic(5) limit 11;
select s, len,  rval, lval, (lval<<2)+len as "(lval<<2)+len" FROM natcod.generate_vbit_series_didactic(3);

CREATE VIEW wiki_print1_hbig AS 
  SELECT len, 
       bitstring, 
       hbig as hbig_dec,
       '0 '||((hbig-len)>>4)::bit(11)::text ||' ' ||len::bit(4)::text internal_16bits,
       hbig>>4 as value_dec,
       len as len_dec
  FROM (
    SELECT row_number() over() AS count, bitstring,
       natcod.vBit_to_hSml( bitstring ) as hbig,
       length(bitstring) as len,
       natcod.vbit_to_hiddenBig( bitstring ) as hidd,
       natcod.vbit_to_baseh(bitstring,4,true) as b4h,
       natcod.vbit_to_baseh(bitstring,16,true) as b16h
    FROM natcod.generate_vbit_series(11) t(bitstring)
  ) t2
;
-- drop VIEW wiki_print2_hcount;
CREATE VIEW wiki_print2_hcount AS 
  SELECT row_number() over() AS count, bitstring,
       natcod.vbit_to_baseh(bitstring,16,true) as b16h, 
       natcod.vBit_to_hCount16c48(bitstring,1::bigint) as hcount1,
       natcod.vBit_to_hCount16c48(bitstring,281474976710655::bigint) as hcount_max
  FROM natcod.generate_vbit_series(11) t(bitstring) -- or 3
;
CREATE VIEW wiki_print3_hcount_explode AS 
  SELECT bitstring, len,
       hcount as hcount_dec,
       '0 '||substring(hcount::bit(64),2,11)::text ||' '||len::bit(4)::text||' '||((hcount<<16)>>16)::bit(48)::text hcount_bin
  FROM (
     SELECT *,length(bitstring) as len, natcod.vBit_to_hCount16c48(bitstring,123::bigint) as hcount
     FROM wiki_print2_hcount
) t;

CREATE VIEW wiki_print4_hcount_human AS 
select *, natcod.hCount16c48_asBeauty(hc,4,true) as hc_human
from (
  select h_path, c, natcod.vBit_to_hCount16c48(h_path,c::bigint) as hc
  from (  SELECT bitstring as h_path,                                                                                                                  
          unnest( array[ random_between(1,3509999999)::bigint, 123::bigint, random_between(1,550999999)::bigint ] ) as c
          FROM wiki_print2_hcount
  ) tt
) tt2 order by 3
;
wiki_print1_hbig:
 len |  bitstring  | hbig_dec |  internal_16bits   | value_dec | len_dec 
 ----+-------------+----------+--------------------+-----------+---------
   1 | 0           |        1 | 0 00000000000 0001 |         0 |       1
   2 | 00          |        2 | 0 00000000000 0010 |         0 |       2
   3 | 000         |        3 | 0 00000000000 0011 |         0 |       3
   4 | 0000        |        4 | 0 00000000000 0100 |         0 |       4
   5 | 00000       |        5 | 0 00000000000 0101 |         0 |       5
   6 | 000000      |        6 | 0 00000000000 0110 |         0 |       6
   7 | 0000000     |        7 | 0 00000000000 0111 |         0 |       7
   8 | 00000000    |        8 | 0 00000000000 1000 |         0 |       8
   9 | 000000000   |        9 | 0 00000000000 1001 |         0 |       9
  10 | 0000000000  |       10 | 0 00000000000 1010 |         0 |      10
  11 | 00000000000 |       11 | 0 00000000000 1011 |         0 |      11
  11 | 00000000001 |       27 | 0 00000000001 1011 |         1 |      11
  10 | 0000000001  |       42 | 0 00000000010 1010 |         2 |      10
  ...

wiki_print2_hcount:
 count |  bitstring  | b16h |       hcount1       |     hcount_max      
 ------+-------------+------+---------------------+---------------------
     1 | 0           | G    |     281474976710657 |     562949953421311
     2 | 00          | H    |     562949953421313 |     844424930131967
     3 | 000         | J    |     844424930131969 |    1125899906842623
     4 | 0000        | 0    |    1125899906842625 |    1407374883553279
     5 | 00000       | 0G   |    1407374883553281 |    1688849860263935
     6 | 000000      | 0H   |    1688849860263937 |    1970324836974591
     7 | 0000000     | 0J   |    1970324836974593 |    2251799813685247
     8 | 00000000    | 00   |    2251799813685249 |    2533274790395903
     9 | 000000000   | 00G  |    2533274790395905 |    2814749767106559
    10 | 0000000000  | 00H  |    2814749767106561 |    3096224743817215
    11 | 00000000000 | 00J  |    3096224743817217 |    3377699720527871
    12 | 00000000001 | 00K  |    7599824371187713 |    7881299347898367
    13 | 0000000001  | 00M  |   11821949021847553 |   12103423998558207
   ...

wiki_print3_hcount_explode:
 bitstring | len |     hcount_dec      |                             hcount_bin                              
 ----------+-----+---------------------+---------------------------------------------------------------------
 0         |   1 |     281474976710779 | 0 00000000000 0001 000000000000000000000000000000000000000001111011
 00        |   2 |     562949953421435 | 0 00000000000 0010 000000000000000000000000000000000000000001111011
 000       |   3 |     844424930132091 | 0 00000000000 0011 000000000000000000000000000000000000000001111011
 001       |   3 | 1153765929536979067 | 0 00100000000 0011 000000000000000000000000000000000000000001111011
 ...

Exemplo de comprimento 11

Distribuição dos comprimentos:

select len, count(*) n from wiki_print1 group by 1 order by 2 desc, 1;
 len |  n   
-----+------
  11 | 2048
  10 | 1024
   9 |  512
   8 |  256
   7 |  128
   6 |   64
   5 |   32
   4 |   16
   3 |    8
   2 |    4
   1 |    2
(11 rows)

Distribuição dos valores sem diferenciador cache-length:

select value_dec, count(*) n from wiki_print1 group by 1 order by 2 desc, 1;
 value_dec | n  
-----------+----
         0 | 11
      1024 | 11
       512 | 10
      1536 | 10
       256 |  9
       768 |  9
      1280 |  9
      1792 |  9
       128 |  8
       384 |  8
       640 |  8
       896 |  8
      1152 |  8
      1408 |  8
      1664 |  8
      1920 |  8
        64 |  7
       192 |  7
       320 |  7
       448 |  7
       576 |  7
       ... | ...
      2012 |  3
      2020 |  3
      2028 |  3
      2036 |  3
      2044 |  3
         2 |  2
         6 |  2
        10 |  2
        14 |  2
        18 |  2
        22 |  2
       ... | ...
      2039 |  1
      2041 |  1
      2043 |  1
      2045 |  1
      2047 |  1
(2048 rows)