Recent

Author Topic: fgl documentation  (Read 14337 times)

Borneq

  • Full Member
  • ***
  • Posts: 248
fgl documentation
« on: April 29, 2013, 10:37:38 am »
Where can I fond fgl unit documentation?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12718
  • FPC developer.
Re: fgl documentation
« Reply #1 on: April 29, 2013, 10:53:51 am »
Afaik there is none, it is all very new (with lots of generics improvement in trunk), and the documentation tool doesn't support generics fully yet.

Borneq

  • Full Member
  • ***
  • Posts: 248
Re: fgl documentation
« Reply #2 on: May 01, 2013, 12:25:05 pm »
Is possible iterate over TFPGMap like c++ example:  (to translate c++->pas)
for (p = children.begin(); p != children.end(); p++)


// iterindex.cpp
#include "stdafx.h"
#include <assert.h>
#include <string>
#include <map>
#include <iostream>
using namespace std;
class Elem
{
public:
    string Name;
    Elem(string Name){this->Name = Name;}
};
typedef map<int, Elem*> TElemMap;
typedef TElemMap::iterator TElemlIter;
TElemMap children;
TElemlIter p;
int _tmain(int argc, _TCHAR* argv[])
{
    for (int i=0; i<10; i++)
    {
        Elem* el;
        el = new Elem(to_string((long long)i));
        children[2*i] = el;
    }
    for (p = children.begin(); p != children.end(); p++)
    {
        cout << p->second->Name;
    }
    return 0;
}

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: fgl documentation
« Reply #3 on: May 02, 2013, 09:12:22 am »
Quote
Is possible iterate over TFPGMap
TFPGMap is an extension of TFPSMap, which is turn is an extension of TFPSList. Therefore, you can iterate as if the map is a list. e.g.:
Code: [Select]
var m: TYourFPGMap;
...
for i := 0 to m.Count - 1 do
  // do something with Keys[i] or Data[i]

Borneq

  • Full Member
  • ***
  • Posts: 248
Re: fgl documentation
« Reply #4 on: May 03, 2013, 02:06:24 pm »
Code: [Select]
var m: TYourFPGMap;
...
for i := 0 to m.Count - 1 do
  // do something with Keys[i] or Data[i]
Elements in map can be empty? How skip it, or 'i' means only not empty elements?
I test it - not empty but it is iterator, it works!
« Last Edit: May 03, 2013, 02:27:32 pm by Borneq »

Borneq

  • Full Member
  • ***
  • Posts: 248
Re: fgl documentation
« Reply #5 on: May 03, 2013, 05:12:51 pm »
I try:
======================================
program listEq;

uses
  fgl;
type
  TLexem = record
    line:   integer;
  end;
  TLexemList = specialize TFPGList<TLexem>;
begin
end.
======================================
and get Error: Operator is not overloaded: "TLexem" = "TLexem"
if I change record to class, it compiled but equal operator compares pointers, not content

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: fgl documentation
« Reply #6 on: May 03, 2013, 05:14:27 pm »
Quote
and get Error: Operator is not overloaded: "TLexem" = "TLexem"
if I change record to class, it compiled but equal operator compares pointers, not content
It's the limitation in the objfpc style generics implementation ATM, you'll need to declare = operator for the record that is known by the generics. maybe Sven can give a solution using delphi style (I'm not too familiar with it).
« Last Edit: May 04, 2013, 02:27:51 am by Leledumbo »

v.denis

  • Guest
Re: fgl documentation
« Reply #7 on: May 03, 2013, 09:54:57 pm »
here's source adapted for fpc (using fpc-stl)

Code: [Select]
program test;

{$mode objfpc}{$H+}

uses
  sysutils,
  gutil,
  gmap;

type

  { TElem }

  TElem = class
  public
    Name: string;
    constructor Create(const aName: string);
  end;

  TElemMapCmp = specialize TLess<integer>;
  TElemMap = specialize TMap<integer, TElem, TElemMapCmp>;

{ TElem }

constructor TElem.Create(const aName: string);
begin
  self.Name := aName;
end;


var
  children: TElemMap;
  el: TElem;
  i: integer;
  it: TElemMap.TIterator;

begin
  children := TElemMap.Create;
  try
    for i := 0 to 9 do
    begin
      el := TElem.Create(IntToStr(i));
      children[2*i] := el;
    end;

    it := children.Min;
    if it <> nil then
    repeat
      writeln(format('%d -> "%s"', [it.key, it.value.name]));
    until not it.next;
    it.free;

    // ... free elements

  finally
    children.Free;
  end;

  readln;

end.

don't forget to free created classes on shutdown
« Last Edit: May 03, 2013, 10:58:01 pm by v.denis »

Borneq

  • Full Member
  • ***
  • Posts: 248
Re: fgl documentation
« Reply #8 on: May 04, 2013, 05:33:56 pm »
stl.lpr(8,3) Fatal: Can not find unit gutil used by stl.
I add $(LazarusDir)fpc\$(FPCVer)\source\packages\fcl-stl\src to project options.

This class has iterator and gaps, is possible make iterator for TFPGMap?
But TMap is ok; for list I can use TVector ?
If I type:writeln(children[11].Name) - will be exception, how to check, it is gap?
« Last Edit: May 04, 2013, 05:45:31 pm by Borneq »

v.denis

  • Guest
Re: fgl documentation
« Reply #9 on: May 04, 2013, 06:12:52 pm »
For what you need usually there's TryGetValue.
Currently there's no such implementation but, it should be easy to create.

It would look like this:

Code: [Select]
program test;

{$mode objfpc}{$H+}

uses
  sysutils,
  gutil,
  gmap;

type

  { TElem }

  TElem = class
  public
    Name: string;
    constructor Create(const aName: string);
  end;

  TElemMapCmp = specialize TLess<integer>;
  TElemMap = specialize TMap<integer, TElem, TElemMapCmp>;

{ TElem }

constructor TElem.Create(const aName: string);
begin
  self.Name := aName;
end;


var
  children: TElemMap;
  el: TElem;
  i: integer;
  it: TElemMap.TIterator;
  Value: TElem;

begin
  children := TElemMap.Create;
  try
    for i := 0 to 9 do
    begin
      el := TElem.Create(IntToStr(i));
      children[2*i] := el;
    end;

    it := children.Min;
    if it <> nil then
    repeat
      writeln(format('%d -> "%s"', [it.key, it.value.name]));
    until not it.next;
    it.free;

    writeln('Test TryGetValue');
    for i := 0 to 100 do
      if children.TryGetValue(i, Value) then
        writeln(format('%d -> "%s"', [i, value.Name])); // key -> value

    // ... free elements

  finally
    children.Free;
  end;

  readln;

end.

so if tested value in range 0 . . 100 will be absent (gap) TryGetValue return False.
« Last Edit: May 04, 2013, 06:30:58 pm by v.denis »

v.denis

  • Guest
Re: fgl documentation
« Reply #10 on: May 04, 2013, 06:20:37 pm »
Here's the patch for TryGetValue.

I've submitted it to bugtracker (#24378)
« Last Edit: May 04, 2013, 06:26:32 pm by v.denis »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: fgl documentation
« Reply #11 on: May 04, 2013, 06:43:58 pm »
Quote
stl.lpr(8,3) Fatal: Can not find unit gutil used by stl.
A short discussion about this

Borneq

  • Full Member
  • ***
  • Posts: 248
Re: fgl documentation
« Reply #12 on: May 04, 2013, 08:15:04 pm »
Here's the patch for TryGetValue.

Code Not compiles
function TMap.TryGetValue(key: TKey; out Value: TValue): boolean;
var Pair:TPair;
    Node: TMSet.PNode;
-----------------------^ Error: Error in type definition
I have bad compiler options?

Also if I add:
 TSymbol = class
    entries:   TEntryMap;
    real_name: string;
    local_scope: integer;   // for quick find proc local vars
  end;

  TSymbolMapCmp = specialize TLess<string>;
  TSymbolMap = specialize TMap<string, TSymbol, TSymbolMapCmp>;

Is error in unit gset:

function TSet.Find(value:T):TIterator;inline;
var ret:TIterator; x:PNode;
begin
  x := NFind(value);
  if x = nil then exit(nil);
  ret := TIterator.create;
  ret.FNode := x;
------------------^
gset.pp(269,16) Error: Incompatible types: got "TMap$AnsiString$TSymbol$TLess$AnsiString.TSet$TPair$TMapCompare$TPair$TLess$AnsiString.PNode" expected "TMap$LongInt$TSymbolEntry$TLess$LongInt.TSet$TPair$TMapCompare$TPair$TLess$LongInt.TSetIterator$TPair$Node.PNode"
« Last Edit: May 04, 2013, 08:47:18 pm by Borneq »

v.denis

  • Guest
Re: fgl documentation
« Reply #13 on: May 04, 2013, 09:27:49 pm »
I tested patch with FPC 2.7.1 from trunk only.

Borneq

  • Full Member
  • ***
  • Posts: 248
Re: fgl documentation
« Reply #14 on: May 04, 2013, 10:22:08 pm »
I have fpc 2.6.2 with Lazarus 1.0.8, how I can install version 2.7.2?
In https://github.com/graemeg/freepascal are only old tags
where I find nightly builds?

 

TinyPortal © 2005-2018