GithubHelp home page GithubHelp logo

generictree's Introduction

  • ๐Ÿ‘‹ Hi, Iโ€™m @davidberneda
  • ๐Ÿ‘€ Iโ€™m interested in ...
  • ๐ŸŒฑ Iโ€™m currently learning ...
  • ๐Ÿ’ž๏ธ Iโ€™m looking to collaborate on ...
  • ๐Ÿ“ซ How to reach me ...

generictree's People

Contributors

davidberneda avatar sherlyn20 avatar skamradt 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

Watchers

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

generictree's Issues

Support for "capacity"

When adding many child nodes to a node, better speed could be obtained by using a Capacity private property, to resize the internal children array few times instead of once per each added node.

Pros:
Speed when adding many many nodes

Cons:
The extra "Capacity" field adds 8 (or 16 in x64) bytes per node (more memory used) because an extra "Count" field should also be added, to remember the actual number of child nodes.

Add OwnsObject, Root properties

Enhancements:
OwnsObject should work as in other generic collection types. Destroy descendants of TObject.
Root returns the topmost parent of a node.

Maybe also add an event OnNodeDestroy.

TNodeDictionary (generic Tree dictionary)

It is currently possible to create a Tree of dictionary nodes, for example:

uses
   System.Generic.Collections;

type
  TMyTree = TNode<TDictionary<String, TButton>>;

In this way, each node "Data" property is a TDictionary that can be used as normal.

However, this is different than having a Tree structure where the children nodes are unique, and accessible using the desired "Key", for example.

type
  TMyTree = TNodeDictionary<String, Integer>;

var 
  Root : TMyTree;
...
  Root['America'].Data := 123;
  Root['America']['USA'].Data := 456;

In the above code, "USA" is a unique children node of "America", and each node has a "Data" property of type Integer.

Sort not constantly working

I was having problems getting the sort to always sort properly. I have another quicksort array algorithm laying about that appears to work more reliably, but requires loosing the const on the l parameter.

procedure TNode<T>.PrivateSort(const ACompare: TCompareProc; l,r:TInteger);
var
  I, J: TInteger;
  P, T: Pointer;
begin
  if r < 0 then
    exit;
  repeat
    I := L;
    J := R;
    P := fItems[(L + R) shr 1];
    repeat
      while ACompare(fItems[I], P) < 0 do
        Inc(I);
      while ACompare(fItems[J], P) > 0 do
        Dec(J);
      if I <= J then
      begin
        if I <> J then
        begin
          Exchange(I,J);
        end;
        Inc(I);
        Dec(J);
      end;
    until I > J;
    if L < J then
      privatesort(ACompare,L,J);
    L := I;
  until I >= R;
end;

SetParent will free node if parent is already set

The SetParent method will call orphan, which if a parent is already set will indirectly free the node when delete is called. Orphan should not call delete, but a different method which will remove it from the internal list but NOT free the node. This means that your destructor must also change to call delete directly as orphan would leave a reference

suggestion: Add swap or exchange node method

The generic tree structure mostly works well, but if one wants to perform sorting, there is no way to get at and adjust the underlying node order easily. I would suggest adding either a swap or exchange method that would simply swap positions in the internal FItems array between two child elements.

ForEach problem with orphan

If one performs an orphan as part of the forEach loop, this can cause problems as the array is resized in the middle of the ForEach call. One example that I ran into this was by moving the entire contents of one tree to another by setting a new parent in a forEach call.

One possible solution would be to load the nodes to process into a new list separate from the fItems array so any changes in the array do not impact what needs to be run.

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.