Code Monkey home page Code Monkey logo

lgenerics's Introduction

LGenerics

Collection of generic algorithms and data structures entirely written in/for FPC and Lazarus. Started as a self-education project, it now seems quite comfortable and fast. In order to use it (FPC 3.2.2 and higher and Lazarus 2.2.0 and higher):

  • open and compile package lgenerics/LGenerics.lpk.

  • add LGenerics package to project dependencies.

Implemented primitives:

  • stack(unit lgStack)
  • queue(unit lgQueue)
  • deque(unit lgDeque)
  • vector(unit lgVector)
  • vector of bits(unit lgVector)
  • priority queue based on binary heap(unit lgPriorityQueue)
  • priority queue with key update and melding based on pairing heap(unit lgPriorityQueue)
  • sorted list(unit lgList)
  • hashed list - array based list with the ability to fast search by key(unit lgList)
  • hashset(unit lgHashSet)
  • fine-grained concurrent hashset(unit lgHashSet)
  • sorted set(unit lgTreeSet)
  • set of arbitrary size(unit lgUtil, TGSet)
  • hash multiset(unit lgHashMultiSet)
  • fine-grained concurrent hashmultiset(unit lgHashMultiSet)
  • sorted multiset(unit lgTreeMultiSet)
  • hashmap(unit lgHashMap)
  • fine-grained concurrent hashmap(unit lgHashMap)
  • sorted map(unit lgTreeMap)
  • hash multimap(unit lgMultiMap)
  • tree multimap(unit lgMultiMap)
  • list miltimap(unit lgMultiMap)
  • bijective map(unit lgBiMap)
  • sparse 2D table(unit lgTable2D)
  • disjoint set(unit lgHashSet)
  • AVL tree(unit lgAvlTree)
  • red-black tree(unit lgRbTree)
  • some treap variants(unit lgTreap)
  • general rooted tree(unit lgRootTree)
  • sparse labeled undirected graph(unit lgSimpleGraph)
  • sparse labeled directed graph(unit lgSimpleDigraph)

features:

  • extended IEnumearble interface - filtering, mapping, etc.
  • lite containers based on advanced records

Implemented graph features:

  • core functions:
    • vertices/edges addition/removal/query/enumeration, edge contraction, degree
    • load/save to own binary format, primitive export to DOT format
  • connectivity:
    • connected/strongly connected components, bipartite detection, degeneracy, k-core
    • articulation points, bridges, biconnected components
    • edge-connectivity
  • traversals:
    • BFS/DFS traversals with visitors,
    • cycle/negative cycle detection,
    • topological sort
  • operations:
    • induced subgraphs, complement, reverse, union, intersect, symmetric difference,
  • chordality testing
  • planarity testing: FMR Left-Right Planarity algorithm
  • distance within graph:
    • eccentricity, radius, diameter, center, periphery
  • matching:
    • maximum cardinality matching on bipartite/arbitrary graphs
    • minimum/maximum weight matching on bipartite graphs
  • dominators in flowgraps: simple iterative and Semi-NCA algorithms
  • some suggestions for NP-hard problems:
    • maximum independent set, maximal independent sets enumeration
    • maximum clique, cliques enumeration
    • minimum vertex cover, minimal vertex covers enumeration
    • vertex coloring, approximations and exact
    • minimum dominating set
    • Hamiltonian cycles and paths
    • local search TSP approximations, BnB TSP solver
  • minimum spanning trees: Prims's and Kruskal's algorithms
  • single source shortest paths:
    • Dijkstra with pairing heap, Bellman-Ford-Moor with Tarjan's subtree disassembly(BFMT)
  • single pair shortest paths:
    • Dijkstra with binary heap, BFMT, bidirection Dijkstra, A*, NBA*
  • all pairs shortest paths:
    • Floyd–Warshall, Johnson, BFMT
  • networks:
    • maximum flow: push/relabel, capacity scaling Dinitz
    • minimum-cost flow: Busacker-Gowen, cost scaling push/relabel algorithm
    • global minimum cut: Stoer–Wagner, Nagamochi-Ibaraki

Algorithms on arrays and vectors(mostly unit lgArrayHelpers):

  • reverse, right/left cyclic shifts
  • permutations
  • binary search
  • N-th order statistics
  • inversion counting
  • distinct values selection
  • quicksort
  • introsort
  • dual pivot quicksort
  • mergesort
  • timsort(unit lgMiscUtils)
  • counting sort
  • radix sort
  • translation of Orson Peters' PDQSort algorithm
  • static segment tree
  • longest increasing subsequence
  • ...

Algorithms on strings and sequences(units lgStrHelpers, lgSeqUtils)

  • Boyer-Moore string matching algorithm(in Fast Search variant)
  • Boyer-Moore-Horspool-Raita algorithm
  • Aho-Corasick automation for multiple string matching
  • longest common subsequence of two sequences:
    • reducing the LCS problem to LIS
    • Kumar-Rangan algorithm for LCS
    • Myers algorithm for LCS
  • Levenshtein distance:
    • simple DP algorithm
    • modified Berghel-Roach algorithm
    • Myers bit-vector algorithm with cut-off heuristic
  • LCS distance:
    • Myers algorithm for LCS distance
  • restricted Damerau-Levenshtein distance
    • modified Berghel-Roach algorithm
  • fuzzy string matching with k differences
    • Ukkonen EDP algorithm
  • fuzzy string matching with k mismatches
    • using bitap algorithm
  • fuzzy string matching with preprocessing(something similar to fuzzywuzzy)

Other:

  • non-cryptogarphic hashes(unit lgHash):
    • Yann Collet's xxHash32, xxHash64
    • Austin Appleby's MurmurHash2, MurmurHash2A, MurmurHash3_x86_32, MurmurHash64A
  • brief and dirty implementation of futures concept(unit lgAsync)
  • brief channel implementation(unit lgAsync)
  • brief implementation of thread pool(unit lgAsync)
  • 128-bit integers(unit lgInt128)
  • JSON validator/parser/generator(unit lgJson)
  • JSON Patch/Diff(unit lgJson)
  • JSON serialization/deserialization of native Pascal data structures using RTTI(unit lgPdo)
  • JSON Type Definition schemas(unit lgJsonTypeDef)
    • validating a JSON document against a JTD schema(unit lgJsonTypeDef)
    • generating Pascal code from a JTD schema(unit lgJtdCodegen)
    • inferring JTD schemas from example data(unit lgJtdInfer)
  • JSONPath implementation(unit LgJsonPath)
  • CSV document processing(unit lgCsvUtils)
  • Eisel-Lemire fast string-to-double conversion algorithm(unit lgJson)
  • Ryū double-to-string conversion algorithm(unit lgJson)

lgenerics's People

Contributors

avk959 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lgenerics's Issues

too generic names

resourcestring
SEInvalidJsonFile = '"%s" is not a valid JSON configuration file';
SECouldNotOpenKey = 'Could not open key "%s"';

They may conflict with user-defined names

Can't compile lgenerics with fpc 3.2 and lazarus 2.0.1

Hi,

i want to use your package lgenerics at first time. But i have a problem to compile the package.
Can you tell me what can i do, whats wrong?

Thanks for your efforts.

On compile lgenerics.lpk, the following error message appears:

Hint: (11030) Start of reading config file C:\lazarus\fpc\3.2.0\bin\i386-win32\fpc.cfg
Hint: (11031) End of reading config file C:\lazarus\fpc\3.2.0\bin\i386-win32\fpc.cfg
Free Pascal Compiler version 3.2.0 [2020/07/07] for i386
Copyright (c) 1993-2020 by Florian Klaempfl and others
(1002) Target OS: Win32 for i386
(3104) Compiling lgenerics.pas
(3104) Compiling lgabstractcontainer.pas
(3104) Compiling lgutils.pas
(3104) Compiling lgstrconst.pas
(1010) Writing Resource String Table file: lgstrconst.rsj
(3104) Compiling lghelpers.pas
(3104) Compiling lghash.pas
..\LGenerics-master\lgenerics\lghelpers.pas(670,36) Hint: (4055) Conversion between ordinals and pointers is not portable
(3104) Compiling lgarrayhelpers.pas
..\LGenerics-master\lgenerics\lgarrayhelpers.pas(14980,23) Hint: (5058) Variable "aOfs" does not seem to be initialized
..\LGenerics-master\lgenerics\lgarrayhelpers.pas(33,8) Hint: (5023) Unit "lgHelpers" not used in lgArrayHelpers
(3104) Compiling lgasync.pas
(3104) Compiling lgqueue.pas
(3104) Compiling lgvector.pas
..\LGenerics-master\lgenerics\lgvector.pas(2541,41) Note: (6058) Call to subroutine "function BsfQWord(const AValue:QWord):DWord;" marked as inline is not inlined
(3104) Compiling lgpriorityqueue.pas
(3104) Compiling lgfunction.pas
(3104) Compiling lghashtable.pas
..\LGenerics-master\lgenerics\lghashtable.pas(34,8) Hint: (5023) Unit "lgHelpers" not used in lgHashTable
..\LGenerics-master\lgenerics\lgfunction.pas(181,3) Error: (1026) Compilation raised exception internally
Fatal: (1018) Compilation aborted
An unhandled exception occurred at $00470C1E:
EAccessViolation: Access violation
$00470C1E
$004AFED0
$004AED50
$004C61F0
$004C6B88
$004366BC
$004B7076
$00569C44
$0056AACE
$004366BC
$004B7076
$00569C44
$0056AACE
$004366BC
$00414D05

Error: C:\lazarus\fpc\3.2.0\bin\i386-win32\ppc386.exe returned an error exitcode

separate package for JSON

what do you think, about JSON code is needing to be separate package? it is so general for using. ie I want to use JSON code only and not the rest of the stuff.

LgHashMap Initialization issue: cannot be determined whether it is due to the compiler or LGenrics.

Hello avk959!
Thank you very much for excellent LGenerics library!

I encountered an issue when applying HashMap. but I cannot determine whether it is due to the compiler or LGenrics.

type
  TTestMapSpec=specialize TGLiteHashMapLP<string, string, string>;
  TTestMap=TTestMapSpec TMap;

  TFoo=class
  private
    FMap: TTestMap;
    procedure DoInitialize; // Here, some Key-Val will be inserted into FMap
    procedure DoFinalize;
  public
    constructor Create;  // Calling DoInitialize
    destroyer Destroy; override;
   end;

----TFoo is working properly. But if a TBar is derived from TFoo, such as:

  TBar=class (TFoo)
  public
    constructor Create; // Call TFoo.Create
    destroyer Destroy; override;
  end;

----Execute TBar.Create will result in a memory overflow.
Tracking the code reveals that it is constantly calling TGLiteHashTableLP.Resize(aNewCapacity: SizeInt);
The difference is:
The "class operator TGLiteHashTableLP.Initialize(var ht: TGLiteHashTableLP)" is called before the body of the TFoo.Create
while call TFoo.Create DIRECTLY, and it will be NOT while “TBar.Create ->TFoo.Create".

************so, it seems to be related to initialization, so
if defined
TTestMap=specialize TGHashMapLP<string, string>;
Modify

  TFoo.DoInitialize;
   begin
     FMap := TTestMap.Create;  // add this line, do Initialization
     ...
   end;

--- both TFoo.Create and TBar.Create are working properly.

Test Enviroment: Win7-x64, Lazarus 3.2/FPC 3.2.2; LGenerics 0.56 or master version

the test code attached here, note that the conditional compilation
fb.zip

 {$DEFINE LITE_VERSION}      // undefine this, both TFoo and TBar are working properly.
                                              // define LITE_VERSION, TFoo working properly, TBar go wrong.  

2024-04-25 22 13 27
2024-04-25 22 12 31
2024-04-25 22 13 07

No size specified and unable to determine the size of the operands

Hello,

I recently upgraded FPC to most recent trunk snapshot and am now facing the following errors in compiling lgint128:

lgint128.pas(1110,16) Error: No size specified and unable to determine the size of the operands
lgint128.pas(1673,15) Error: No size specified and unable to determine the size of the operands
lgint128.pas(1682,15) Error: No size specified and unable to determine the size of the operands
lgint128.pas(1684,15) Error: No size specified and unable to determine the size of the operands
lgint128.pas(1734,15) Error: No size specified and unable to determine the size of the operands

I don't have much experience in ASM, so I appreciate any help to fix this error.

I am trying to cross-compile from win64 to win32.

Thank you :)

Linux - lgAvlTree Enumerator Error

on MX-Linux, Mint Linux, code like here will raise invalid pointer error, but on Windows 7, everything go right
and if I switch back to LGenerics_054, non error

// my test code

type
  
   TWordSetSpec = specialize TGLiteTreeSet<string, string>;

var 
  ws: TWordSetSpec;
begin
  ws.Add('S1');
  ws.Add('M2');
  ws.Add('K3');
  for s in ws do
      Memo1.Append(s);     // first 'K3' , Okay;   but Next, Raise error at lgAvlTree line 2666
end;

// the lgAvlTree .pas

function TGLiteAvlTree.TEnumerator.MoveNext: Boolean;
var
  NextNode: SizeInt = 0;
begin
  if FCurrNode <> 0 then
    NextNode := FTree^.Successor(FCurrNode)    //  this line(2666),  FTree INVALID pointer error
else
  ...

Compiler: FPC 3.2.2 Lazarus: 2.2.6

JSON code - compat with Lazarus TJsonConfig

I cannot yet try your json code in CudaText because I have too many code for TJsonConfig, hard to edit it all. But if you give some 'compat layer' for it, I will try it. I can change type "TJsonConfig" in all places to other, but I don't want to edit code.

Compiling package on macOS fails

Error is
lgasync.pas(2103,13) Error: Incompatible types: got "ShortInt" expected "TThreadID"
fpc 3.2.2 Lazarus 2.2.6
MacOS Ventura 13.6.1

Fixed locally by changing
lgAsync line 2103 from Result :=0; to Result := TThreadId(0);
and
lgAsync line 2110 from Result := 0; to Result := TThreadId(0);

Unable to use latest LGenerics with latest FPC trunk

Hi @avk959,

Thanks for the fine library for FPC. I am runnning into issues when compiling with FPC trunk and using lghashmap.
I get the following error.

Compile Project, Mode: Release, Target: C:\Users\iwank\<secret>\1brc-ObjectPascal\bin\ikelaiah.exe: Exit code 1, Errors: 40, Hints: 2
Hint: Start of reading config file C:\fpcupdeluxe\fpc\bin\x86_64-win64\fpc.cfg
Hint: End of reading config file C:\fpcupdeluxe\fpc\bin\x86_64-win64\fpc.cfg
Verbose: Free Pascal Compiler version 3.3.1-15663-ga888a141a2 [2024/05/04] for x86_64
Verbose: Copyright (c) 1993-2024 by Florian Klaempfl and others
Verbose: Target OS: Win64 for x64
Verbose: Compiling OneBRC.lpr
Verbose: Compiling weatherstation.pas
lgabstractcontainer.pas(140,6) Error: No matching implementation for interface method "FindFirst(out OpenString):System.Boolean;" found
lgabstractcontainer.pas(140,6) Error: No matching implementation for interface method "FindLast(out OpenString):System.Boolean;" found
lgabstractcontainer.pas(140,6) Error: No matching implementation for interface method "FindMin(out OpenString;TGLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgabstractcontainer.pas(140,6) Error: No matching implementation for interface method "FindMin(out OpenString;TGOnLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgabstractcontainer.pas(140,6) Error: No matching implementation for interface method "FindMin(out OpenString;TGNestLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgabstractcontainer.pas(140,6) Error: No matching implementation for interface method "FindMax(out OpenString;TGLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgabstractcontainer.pas(140,6) Error: No matching implementation for interface method "FindMax(out OpenString;TGOnLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgabstractcontainer.pas(140,6) Error: No matching implementation for interface method "FindMax(out OpenString;TGNestLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
enumsh.inc(25,19) Error: No matching implementation for interface method "FindFirst(out OpenString):System.Boolean;" found
enumsh.inc(25,19) Error: No matching implementation for interface method "FindLast(out OpenString):System.Boolean;" found
enumsh.inc(25,19) Error: No matching implementation for interface method "FindMin(out OpenString;TGLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
enumsh.inc(25,19) Error: No matching implementation for interface method "FindMin(out OpenString;TGOnLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
enumsh.inc(25,19) Error: No matching implementation for interface method "FindMin(out OpenString;TGNestLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
enumsh.inc(25,19) Error: No matching implementation for interface method "FindMax(out OpenString;TGLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
enumsh.inc(25,19) Error: No matching implementation for interface method "FindMax(out OpenString;TGOnLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
enumsh.inc(25,19) Error: No matching implementation for interface method "FindMax(out OpenString;TGNestLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgabstractcontainer.pas(201,6) Error: No matching implementation for interface method "FindFirst(out OpenString):System.Boolean;" found
lgabstractcontainer.pas(201,6) Error: No matching implementation for interface method "FindLast(out OpenString):System.Boolean;" found
lgabstractcontainer.pas(201,6) Error: No matching implementation for interface method "FindMin(out OpenString;TGLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgabstractcontainer.pas(201,6) Error: No matching implementation for interface method "FindMin(out OpenString;TGOnLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgabstractcontainer.pas(201,6) Error: No matching implementation for interface method "FindMin(out OpenString;TGNestLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgabstractcontainer.pas(201,6) Error: No matching implementation for interface method "FindMax(out OpenString;TGLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgabstractcontainer.pas(201,6) Error: No matching implementation for interface method "FindMax(out OpenString;TGOnLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgabstractcontainer.pas(201,6) Error: No matching implementation for interface method "FindMax(out OpenString;TGNestLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgutils.pas(259,6) Error: No matching implementation for interface method "FindFirst(out OpenString):System.Boolean;" found
lgutils.pas(259,6) Error: No matching implementation for interface method "FindLast(out OpenString):System.Boolean;" found
lgutils.pas(259,6) Error: No matching implementation for interface method "FindMin(out OpenString;TGLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgutils.pas(259,6) Error: No matching implementation for interface method "FindMin(out OpenString;TGOnLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgutils.pas(259,6) Error: No matching implementation for interface method "FindMin(out OpenString;TGNestLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgutils.pas(259,6) Error: No matching implementation for interface method "FindMax(out OpenString;TGLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgutils.pas(259,6) Error: No matching implementation for interface method "FindMax(out OpenString;TGOnLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgutils.pas(259,6) Error: No matching implementation for interface method "FindMax(out OpenString;TGNestLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgutils.pas(259,6) Error: No matching implementation for interface method "FindFirst(out OpenString):System.Boolean;" found
lgutils.pas(259,6) Error: No matching implementation for interface method "FindLast(out OpenString):System.Boolean;" found
lgutils.pas(259,6) Error: No matching implementation for interface method "FindMin(out OpenString;TGLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgutils.pas(259,6) Error: No matching implementation for interface method "FindMin(out OpenString;TGOnLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgutils.pas(259,6) Error: No matching implementation for interface method "FindMin(out OpenString;TGNestLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgutils.pas(259,6) Error: No matching implementation for interface method "FindMax(out OpenString;TGLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgutils.pas(259,6) Error: No matching implementation for interface method "FindMax(out OpenString;TGOnLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
lgutils.pas(259,6) Error: No matching implementation for interface method "FindMax(out OpenString;TGNestLessCompare$1$crc640B6513_crc69E394E1):System.Boolean;" found
weatherstation.pas(66,1) Verbose: There were 40 errors compiling module, stopping
Verbose: Compilation aborted
Verbose: C:\fpcupdeluxe\fpc\bin\x86_64-win64\ppcx64.exe returned an error exitcode

Can you help?
Many thanks.

-Iwan

FindMaxClique fail

TintChart.FindMaxClique failes with message Acces Violation in file lgsimplegraph.pas line 1642

function TGSimpleGraph.FindChordalMaxClique(out aClique: TIntSet): Boolean;
procedure NodeDone(aIndex: SizeInt; const aLefts: TIntSet);
begin
if aLefts.Count > Pred(aClique.Count) then
begin
aClique.Assign(aLefts);
aClique.Push(aIndex);
end;
end;
var
Dummy: TIntArray;
begin
aClique.Clear;
Result := FindPerfectElimOrd(@NodeDone, Dummy);
if not Result then
aClique.Clear;
end;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.