Recent

Author Topic: from delphi Tdictionary and hashed  (Read 5964 times)

mrspock1

  • New Member
  • *
  • Posts: 29
from delphi Tdictionary and hashed
« on: July 30, 2018, 07:24:50 am »
I have three questions
1)
I have delphi code using Tqueue and Tdictionary. Tdictionary is organised using hash tables so I can have immediate access to the elements. What are all actions to be done to use the code in Lazarus? I need the code to be 64 bit since Tdictionary uses a lot of elements over 4GB memory heap.

2)
What are basic steps to cope with different IDE in Lazarus by comparing with Delphi? Where are they described?

3)
How can I use unicode chars in TRichEdit to be visible, like these ones:
 #$2610;#$25CE;#$23FA;#$2B88;#$2B89;#$2B8A;#$2B8B;

Handoko

  • Hero Member
  • *****
  • Posts: 5159
  • My goal: build my own game engine using Lazarus
Re: from delphi Tdictionary and hashed
« Reply #1 on: July 30, 2018, 07:53:18 am »
Hello mrspock1,
Welcome to the forum.

1)
I have delphi code using Tqueue and Tdictionary. Tdictionary is organised using hash tables so I can have immediate access to the elements. What are all actions to be done to use the code in Lazarus? I need the code to be 64 bit since Tdictionary uses a lot of elements over 4GB memory heap.

TQueue is provided by unit Contnrs:
https://www.freepascal.org/docs-html/3.0.2/fcl/contnrs/tqueue.html

For TDictionary, read here:
https://forum.lazarus.freepascal.org/index.php?topic=40910.0

2)
What are basic steps to cope with different IDE in Lazarus by comparing with Delphi? Where are they described?

Maybe these can be useful for you:

Lazarus IDE tricks:
http://wiki.freepascal.org/New_IDE_features_since

Coming from Delphi:
http://wiki.lazarus.freepascal.org/index.php/Lazarus_Documentation#Coming_from_Delphi

Lazarus Documentation:
http://wiki.freepascal.org/Lazarus_Documentation
« Last Edit: July 30, 2018, 08:03:24 am by Handoko »

Thaddy

  • Hero Member
  • *****
  • Posts: 14393
  • Sensorship about opinions does not belong here.
Re: from delphi Tdictionary and hashed
« Reply #2 on: July 30, 2018, 09:24:01 am »
TQueue is provided by unit Contnrs:
TQueue<T> is provided by generics.collections from rtl-generics.
TDictionary<T> is provided by generics.collections from rtl-generics
Quote
Maybe these can be useful for you:

Lazarus IDE tricks:
http://wiki.freepascal.org/New_IDE_features_since

Coming from Delphi:
http://wiki.lazarus.freepascal.org/index.php/Lazarus_Documentation#Coming_from_Delphi

Lazarus Documentation:
http://wiki.freepascal.org/Lazarus_Documentation
Correct. (partially: mode objfpc is somewhat deprecated to my eyes)

For 3.0.4. rtl-generics can be installed through the online package manager.
For trunk 3.1.1  it is already provided as default.

the rtl-generics package is Delphi compatible. In mode delphi it will compile most of what delphi does regarding generic containers and collections.
« Last Edit: July 30, 2018, 09:25:44 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14393
  • Sensorship about opinions does not belong here.
Re: from delphi Tdictionary and hashed
« Reply #3 on: July 30, 2018, 10:47:04 am »
3)
How can I use unicode chars in TRichEdit to be visible, like these ones:
 #$2610;#$25CE;#$23FA;#$2B88;#$2B89;#$2B8A;#$2B8B;
Lazarus does not provide a RichEdit even not for Windows, but there are alternatives like RichView. (there are more)
« Last Edit: July 30, 2018, 11:27:24 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14393
  • Sensorship about opinions does not belong here.
Re: from delphi Tdictionary and hashed
« Reply #4 on: July 30, 2018, 11:45:20 am »
Note that in Delphi AND FPC the single element size for e.g. arrays is exactly 4G minus some management code in a 64 bit environment.
So apart from streams, you will probably be still stuck if you want to use single variables over 4 G. But you can use as many of them as physical memory allows.
As I wrote: otherwise that needs a stream, possibly a buffered stream.

Can you enlighten us WHY the single variables need to be so big? (with example code we can test) and if you an provide Delphi code that says otherwise?
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

mrspock1

  • New Member
  • *
  • Posts: 29
Re: from delphi Tdictionary and hashed
« Reply #5 on: July 30, 2018, 05:39:51 pm »
I store graph in Tdictionary as nodes, about 50 bytes for each one and there are over 30 million of the nodes. BFS graph searching algorithm takes up a lot of operational memory. So far I managed to solve a small board puzzle called "peg solitaire" with about 3GB RAM, but the large diamond-shape peg solitaire will take much more RAM. It is supposed to be solved with heuristic algorithm and just 250MB RAM but I have not learned it yet.

Part of the code

Code: Pascal  [Select][+][-]
  1. type
  2.   TwartośćPola = 0..2;
  3.   Tkierunek = (lewy, górny, prawy, dolny, żaden);
  4.   TPole = record
  5.             rząd: 1..9;
  6.             kolumna: 1..10
  7.           end;
  8.   TskompresowanyWierzchołek = array[1..9,1..2] of byte;
  9.   Twierzchołek = array[1..9,1..10] of TwartośćPola;
  10.   TcechaWierzchołka = record
  11.                         poprzedniSkompresowanyWierzchołek: TskompresowanyWierzchołek;
  12.                         poprzednieWybranePole: Tpole;
  13.                         poprzedniKierunek: Tkierunek;
  14.                       end;
  15.   Tgraf= TDictionary<TskompresowanyWierzchołek, TcechaWierzchołka>;
  16.   Tkolejka= Tqueue<TskompresowanyWierzchołek>;
« Last Edit: July 30, 2018, 06:13:52 pm by mrspock1 »

Thaddy

  • Hero Member
  • *****
  • Posts: 14393
  • Sensorship about opinions does not belong here.
Re: from delphi Tdictionary and hashed
« Reply #6 on: July 30, 2018, 05:57:39 pm »
So total net data is 1.3 Gb? (30.000.000 * 50 /1024^3)
That should be no problem then... :D even with overhead.
FPC should handle that correctly in normal cases.
You can pack the records,just in case.
« Last Edit: July 30, 2018, 06:03:30 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

mrspock1

  • New Member
  • *
  • Posts: 29
Re: from delphi Tdictionary and hashed
« Reply #7 on: July 30, 2018, 06:57:25 pm »
I managed to solve the puzzle only for diamond-shape board 7x7 within 1,5 GB RAM. For larger boards, even 9x9, memory use increases exponentially as the problem is NP-complete. Without using heuristic algorithms, solving the puzzle requires 64 GB RAM, as you can read from scientific articles.

My article in Polish is here:
http://informatyka-delphi.blogspot.com/2018/06/zagadka-planszowa-peg-solitaire.html

ASerge

  • Hero Member
  • *****
  • Posts: 2250
Re: from delphi Tdictionary and hashed
« Reply #8 on: July 31, 2018, 12:40:16 am »
Note that in Delphi AND FPC the single element size for e.g. arrays is exactly 4G minus some management code in a 64 bit environment.
This work in 64bit Windows and 64bit FPC:
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$APPTYPE CONSOLE}
  3.  
  4. const
  5.   CGigibyte = 1024 * 1024 * 1024;
  6. type
  7.   PLargeArray = ^TLargeArray;
  8.   TLargeArray = array[1..5 * CGigibyte] of Byte;
  9.  
  10. var
  11.   PA: PLargeArray;
  12.   B: Byte;
  13. begin
  14.   Writeln(High(TLargeArray));
  15.   PA := GetMem(SizeOf(TLargeArray)); // New(PA) get compiler error about 2G limit
  16.   try
  17.     PA^[High(PA^)] := 123;
  18.     B := PA^[High(PA^)];
  19.     Writeln(B);
  20.   finally
  21.     FreeMem(PA);
  22.   end;
  23.   Readln;
  24. end.

mrspock1

  • New Member
  • *
  • Posts: 29
Re: from delphi Tdictionary and hashed
« Reply #9 on: July 31, 2018, 03:31:00 am »
How can I use an array since its indexes (TskompresowanyWierzchołek) do not create a set of adjacent values?

Thaddy

  • Hero Member
  • *****
  • Posts: 14393
  • Sensorship about opinions does not belong here.
Re: from delphi Tdictionary and hashed
« Reply #10 on: July 31, 2018, 06:08:44 am »
@ASerge
In your case the single element I refer to is byte, not the array.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

ASerge

  • Hero Member
  • *****
  • Posts: 2250
Re: from delphi Tdictionary and hashed
« Reply #11 on: July 31, 2018, 08:24:22 pm »
In your case the single element I refer to is byte, not the array.
You can declare an array of arrays. Then the array will be a single element. It works too, but it will take a lot of memory. On my computer so much memory is not present, so there is a RunError 203 = RuntimeErrorExitCodes[reOutOfMemory].
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$APPTYPE CONSOLE}
  3.  
  4. const
  5.   CGigibyte = 1024 * 1024 * 1024;
  6. type
  7.   PLargeArray = ^TLargeArray;
  8.   TLargeArray = array[1..2] of array[1..4 * CGigibyte + 1] of Byte;
  9.  
  10. var
  11.   PA, PB: PLargeArray;
  12. begin
  13.   Writeln('Array size is ', SizeOf(TLargeArray));
  14.   Writeln('Allocating first array...');
  15.   PA := GetMem(SizeOf(TLargeArray)); // Long operation
  16.   try
  17.     PA^[2, 2] := 123;
  18.     Writeln('Allocating second...');
  19.     PB := GetMem(SizeOf(TLargeArray)); // Long operation
  20.     try
  21.       Writeln('Copying array...');
  22.       PB^ := PA^; // Very long operation
  23.       Writeln('Result must be 123: ', PB^[2, 2]);
  24.     finally
  25.       FreeMem(PB);
  26.     end;
  27.   finally
  28.     FreeMem(PA);
  29.   end;
  30.   Readln;
  31. end.

mrspock1

  • New Member
  • *
  • Posts: 29
Re: from delphi Tdictionary and hashed
« Reply #12 on: August 01, 2018, 12:45:51 pm »
I have forgotten to ask. Is Tdictionary hashed to allow an immediate access to its elements? I have heard it was not hashed some time ago.

Coul someone verify that my characters
#$2610;#$25CE;#$23FA;#$2B88;#$2B89;#$2B8A;#$2B8B;
are visible in a RichView or a similar components allowing to put tex in it with "add" or "insert"? There also may be problem with unicode fonts that need to be installed to achieve that. Windows (and Lazarus) should be designed in a different way like pdf is, to include fonts in the application without having to use special coding to do that or otherwise the end user may not see the characters.

 

TinyPortal © 2005-2018