Ada_Ru форум

Обсуждение языка Ада

[ada_ru] Пример Containers.Hashed_Maps

Оставить новое сообщение

Сообщения

Sergey Dukov
[ada_ru] Пример Containers.Hashed_Maps
2018-07-12 20:06:39
Здравствуйте!
 Кто может предложить пример построения и использования объекта Containers.Hashed Maps с типом
ключа и значения Unbounded Wide Wide String или Wide Wide String?
 Заранее благодарен!
 Сергей Дюков,
 Волжский.


[Non-text portions of this message have been removed]
Иван Леваше
[ada_ru] Re: Пример Containers.Hashed_Maps
2018-07-12 22:47:02
Здравствуйте, Сергей и другие участники конференции!

Предлагаю такой пример:

with Ada.Strings.Wide_Wide_Hash;
with Ada.Strings.Wide_Wide_Unbounded.Wide_Wide_Hash;
with Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
with Ada.Containers.Hashed_Maps;
with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Text_IO;

with GNATCOLL.JSON;
with GNAT.Decode_UTF8_String;

procedure Hello_Unbounded_Wide_Wide_Hashed_Hopelessly_Screwed_World is

    use all type GNATCOLL.JSON.JSON_Value_Type;

    package Wide_Wide_Dictionaries is new
      Ada.Containers.Indefinite_Hashed_Maps
        (Key_Type => Standard.Wide_Wide_String,
         Element_Type => Standard.Wide_Wide_String,
         Hash => Ada.Strings.Wide_Wide_Hash,
         Equivalent_Keys => Standard."=",
         "=" => Standard."=");

    package Unbounded_Wide_Wide_Dictionaries is new
      Ada.Containers.Hashed_Maps
        (Key_Type =>
           Ada.Strings.Wide_Wide_Unbounded.Unbounded_Wide_Wide_String,
         Element_Type =>
           Ada.Strings.Wide_Wide_Unbounded.Unbounded_Wide_Wide_String,
         Hash => Ada.Strings.Wide_Wide_Unbounded.Wide_Wide_Hash,
         Equivalent_Keys => Ada.Strings.Wide_Wide_Unbounded."=",
         "=" => Ada.Strings.Wide_Wide_Unbounded."=");

    function To_JSON
      (Dictionary : Wide_Wide_Dictionaries.Map)
      return GNATCOLL.JSON.JSON_Value is
    begin
       return Result : GNATCOLL.JSON.JSON_Value :=
         GNATCOLL.JSON.Create_Object
       do
          declare
             Position : Wide_Wide_Dictionaries.Cursor :=
               Dictionary.First;
          begin
             while Wide_Wide_Dictionaries.Has_Element (Position) loop
                Result.Set_Field
                  (Ada.Strings.UTF_Encoding.Wide_Wide_Strings.Encode
                     (Wide_Wide_Dictionaries.Key (Position)),
                   Ada.Strings.UTF_Encoding.Wide_Wide_Strings.Encode
                     (Dictionary.Constant_Reference (Position)));
                Wide_Wide_Dictionaries.Next (Position);
             end loop;
          end;
       end return;
    end To_JSON;

    function To_JSON
      (Dictionary : Unbounded_Wide_Wide_Dictionaries.Map)
      return GNATCOLL.JSON.JSON_Value is
    begin
       return Result : GNATCOLL.JSON.JSON_Value :=
         GNATCOLL.JSON.Create_Object
       do
          declare
             Position : Unbounded_Wide_Wide_Dictionaries.Cursor :=
               Dictionary.First;
          begin
             while Unbounded_Wide_Wide_Dictionaries.Has_Element
               (Position)
             loop
                Result.Set_Field
                  (Ada.Strings.UTF_Encoding.Wide_Wide_Strings.Encode
                     (Ada.Strings.Wide_Wide_Unbounded.To_Wide_Wide_String
                        (Unbounded_Wide_Wide_Dictionaries.Key
                           (Position))),
                   Ada.Strings.UTF_Encoding.Wide_Wide_Strings.Encode
                     (Ada.Strings.Wide_Wide_Unbounded.To_Wide_Wide_String
                        (Dictionary.Constant_Reference (Position))));
                Unbounded_Wide_Wide_Dictionaries.Next (Position);
             end loop;
          end;
       end return;
    end To_JSON;

    function From_JSON
      (Value : GNATCOLL.JSON.JSON_Value)
      return Wide_Wide_Dictionaries.Map
      with Pre => Value.Kind = JSON_Object_Type is
    begin
       return Result : Wide_Wide_Dictionaries.Map do
          declare
             procedure Process_Pair
               (Key : String;
                Element : GNATCOLL.JSON.JSON_Value) is
             begin
                if Element.Kind /= JSON_String_Type then
                   raise Standard.Constraint_Error with
                     GNATCOLL.JSON.JSON_Value_Type'Image
                       (JSON_String_Type) &
                     " expected, element " &
                     GNATCOLL.JSON.JSON_Value_Type'Image
                       (Element.Kind) &
                     " found with key " & Key;
                end if;

                Result.Insert
                  (GNAT.Decode_UTF8_String.Decode_Wide_Wide_String
                     (Key),
                   GNAT.Decode_UTF8_String.Decode_Wide_Wide_String
                     (Element.Get));
             end Process_Pair;
          begin
             Value.Map_JSON_Object (Process_Pair'Access);
          end;
       end return;
    end From_JSON;

    function From_JSON
      (Value : GNATCOLL.JSON.JSON_Value)
      return Unbounded_Wide_Wide_Dictionaries.Map
      with Pre => Value.Kind = JSON_Object_Type is
    begin
       return Result : Unbounded_Wide_Wide_Dictionaries.Map do
          declare
             procedure Process_Pair
               (Key : String;
                Element : GNATCOLL.JSON.JSON_Value) is
             begin
                if Element.Kind /= JSON_String_Type then
                   raise Standard.Constraint_Error with
                     GNATCOLL.JSON.JSON_Value_Type'Image
                       (JSON_String_Type) &
                     " expected, element " &
                     GNATCOLL.JSON.JSON_Value_Type'Image
                       (Element.Kind) &
                     " found with key " & Key;
                end if;

                declare
                   Position : Unbounded_Wide_Wide_Dictionaries.Cursor;
                   Inserted : Boolean;
                begin
                   Result.Insert
                     (Key => Ada.Strings.Wide_Wide_Unbounded.
                               To_Unbounded_Wide_Wide_String
                                 (GNAT.Decode_UTF8_String.
                                    Decode_Wide_Wide_String
                                      (Key)),
                      Position => Position,
                      Inserted => Inserted);
                   pragma Assert (Inserted);
                   Ada.Strings.Wide_Wide_Unbounded.
                     Set_Unbounded_Wide_Wide_String
                       (Result.Reference (Position),
                        GNAT.Decode_UTF8_String.Decode_Wide_Wide_String
                          (Element.Get));
                end;
             end Process_Pair;
          begin
             Value.Map_JSON_Object (Process_Pair'Access);
          end;
       end return;
    end From_JSON;

    Sample_1 : Wide_Wide_Dictionaries.Map;

begin
    Sample_1.Insert ("Привет", "мир");
    Ada.Text_IO.Put_Line (To_JSON (Sample_1).Write);
    declare
       Sample_2 : constant Unbounded_Wide_Wide_Dictionaries.Map :=
         From_JSON (To_JSON (Sample_1));
    begin
       Ada.Text_IO.Put_Line (To_JSON (Sample_2).Write);
    end;
end Hello_Unbounded_Wide_Wide_Hashed_Hopelessly_Screwed_World;

С уважением,
Левашев Иван,
Барнаул

http://forum.pascal.net.ru/

-- 
If you want to get to the top, you have to start at the bottom
Sergey Dukov
[ada_ru] Re: Пример Conta iners.Hashed_Maps
2018-07-12 23:30:02
Здравствуйте, Иван!
 Большущее спасибо!
 Я не знал где взять функцию Hash. Ваш пример мне очень помог!
 С уважением и наилучшими пожеланиями,
 Сергей Дюков,
 Волжский.
 



[Non-text portions of this message have been removed]
Новое сообщение:
Страницы: 1

Чтобы оставить новое сообщение необходимо Зарегистрироваться и Войти