Ir para o conteúdo

Código Natural/Notação posicional: mudanças entre as edições

m
mSem resumo de edição
Linha 366: Linha 366:
O "encode" é a conversão "vbit to base-h", o "decode" é a conversão "base-h to vbit", daí os nomes de função <code>vbit_to_baseh()</code> e  <code>baseh_to_vbit()</code>, no contexto do [https://git.osm.codes/NaturalCodes/tree/main/src-sql SQL-schema NatCode].
O "encode" é a conversão "vbit to base-h", o "decode" é a conversão "base-h to vbit", daí os nomes de função <code>vbit_to_baseh()</code> e  <code>baseh_to_vbit()</code>, no contexto do [https://git.osm.codes/NaturalCodes/tree/main/src-sql SQL-schema NatCode].


Função geral de "encode bitstring". Converte uma cadeia de bits em texto base2h, base4h, base8h ou base16h.
A seguir algoritmos simplificados. A função de "encode bitstring" converte uma cadeia de bits em texto base2h, base4h, base8h ou base16h.
   
   
<syntaxhighlight lang="sql">
<syntaxhighlight lang="sql">
CREATE FUNCTION natcod.vbit_to_baseh(
CREATE FUNCTION natcod.vbit_to_baseh(
   p_val varbit,  -- input
   p_val varbit,  -- input
   p_base int DEFAULT 4, -- selecting base2h, base4h, base8h, or base16h.
   p_base int DEFAULT 16 -- 16 for base16h (default), 8 for base8h, 4 for base4h or 2 for base2h.
  p_nhDigits_upper boolean default false  -- represent non-hierarchical digits in upper case
) RETURNS text AS $f$
) RETURNS text AS $f$
DECLARE
DECLARE
Linha 385: Linha 384:
     trtypes JSONb := '{"2":[1,1], "4":[1,2], "8":[2,3], "16":[3,4]}'::JSONb; -- TrPos,bits. Can optimize? by sparse array.
     trtypes JSONb := '{"2":[1,1], "4":[1,2], "8":[2,3], "16":[3,4]}'::JSONb; -- TrPos,bits. Can optimize? by sparse array.
     trpos int;
     trpos int;
     baseh "char"[] := array[ -- new 2023 standard for Baseh:
     baseh "char"[] := array[ -- the standards for Baseh:
      '[0:15]={g,q,x,x,x,x,x,x,x,x,x,x,x,x,x,x}'::"char"[], --1. 1 bit in 4h,8h,16h
      '[0:15]={0,1,2,3,x,x,x,x,x,x,x,x,x,x,x,x}'::"char"[], --2. 2 bits in 4h
      '[0:15]={h,m,r,v,x,x,x,x,x,x,x,x,x,x,x,x}'::"char"[], --3. 2 bits 8h,16h
      '[0:15]={0,1,2,3,4,5,6,7,x,x,x,x,x,x,x,x}'::"char"[], --4. 3 bits in 8h
      '[0:15]={j,k,n,p,s,t,z,y,x,x,x,x,x,x,x,x}'::"char"[], --5. 3 bits in 16h
      '[0:15]={0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f}'::"char"[]  --6. 4 bits in 16h
    ]; -- jumpping I,O and L,U,W,X letters!
      -- the standard alphabet is https://tools.ietf.org/html/rfc4648#section-6
BEGIN
  IF (p_nhDigits_upper) THEN
    baseh := array[
       '[0:15]={G,Q,x,x,x,x,x,x,x,x,x,x,x,x,x,x}'::"char"[], --1. 1 bit in 4h,8h,16h
       '[0:15]={G,Q,x,x,x,x,x,x,x,x,x,x,x,x,x,x}'::"char"[], --1. 1 bit in 4h,8h,16h
       '[0:15]={0,1,2,3,x,x,x,x,x,x,x,x,x,x,x,x}'::"char"[], --2. 2 bits in 4h
       '[0:15]={0,1,2,3,x,x,x,x,x,x,x,x,x,x,x,x}'::"char"[], --2. 2 bits in 4h
Linha 403: Linha 391:
       '[0:15]={J,K,N,P,S,T,Z,Y,x,x,x,x,x,x,x,x}'::"char"[], --5. 3 bits in 16h
       '[0:15]={J,K,N,P,S,T,Z,Y,x,x,x,x,x,x,x,x}'::"char"[], --5. 3 bits in 16h
       '[0:15]={0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f}'::"char"[]  --6. 4 bits in 16h
       '[0:15]={0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f}'::"char"[]  --6. 4 bits in 16h
     ];
      -- Hexadecimals by standard alphabet of https://tools.ietf.org/html/rfc4648#section-6
  END IF;
     ]; -- jumpping I,O and L,U,W,X letters.
BEGIN
   vlen := bit_length(p_val);
   vlen := bit_length(p_val);
   tr_selected := trtypes->(p_base::text);  -- can be array instead of JSON
   tr_selected := trtypes->(p_base::text);  -- can be array instead of JSON
Linha 429: Linha 418:
   RETURN ret;
   RETURN ret;
END
END
$f$ LANGUAGE plpgsql IMMUTABLE;
$f$ LANGUAGE PLpgSQL IMMUTABLE;
</syntaxhighlight>
</syntaxhighlight>


2 402

edições