User Tools

Site Tools


Sidebar

GitHub

CoreLib

Sponsors

This website is sponsored by

Notes

This wiki uses icons from icons8.com licensed under CC BY-ND 3.0.

proposals:data:genericlist:start
Author Patrick Lehmann
Last Update 23.08.2016
Related Proposals TBV19 - Lists

Generic List

This generic packages implements a generic list. The list uses dynamic memory allocations and deallocations to grow in size if more items are stored in it. The list is implemented as a protected type and thus requires VHDL-200x.

Implemented methods are:

  • Append
  • Clear
  • Contains
  • Count
  • Get
  • Init
  • Insert
  • IndexOf
  • Prepend
  • RemoveAt
  • Remove
  • Resize
  • Set
  • Size
  • ToArray

Here is a VHDL interface definition:

package corelib_List is
  generic (
    type ELEMENT_TYPE;
    InitialMasterListSize : POSITIVE  := 4;
    InitialChunkListSize  : POSITIVE  := 8;
    MasterListResize      : POSITIVE  := 8
  );
  
  type ELEMENT_ARRAY is array(NATURAL range <>) of ELEMENT_TYPE;
  
  type PT_LIST is protected
    procedure       Append(Value : ELEMENT_TYPE);
    impure function Append(Value : ELEMENT_TYPE) return NATURAL;
    procedure       Append(Values : ELEMENT_ARRAY);
    impure function Append(Values : ELEMENT_ARRAY) return NATURAL;
    procedure       Clear;
    impure function Contains(Value : ELEMENT_TYPE) return BOOLEAN;
    impure function Count return NATURAL;
    procedure       Init;
    procedure       Insert(Index : NATURAL; Value : ELEMENT_TYPE);
    procedure       Insert(Index : NATURAL; Values : ELEMENT_ARRAY);
    impure function IndexOf(Value : ELEMENT_TYPE) return INTEGER;
    impure function Get(Index : NATURAL) return ELEMENT_TYPE;
    procedure       Prepend(Value : ELEMENT_TYPE);
    impure function Prepend(Value : ELEMENT_TYPE) return NATURAL;
    procedure       Prepend(Values : ELEMENT_ARRAY);
    impure function Prepend(Values : ELEMENT_ARRAY) return NATURAL;
    procedure       RemoveAt(Index : NATURAL);
    impure function RemoveAt(Index : NATURAL) return ELEMENT_TYPE;
    procedure       Remove(Value : ELEMENT_TYPE);
    impure function Remove(Value : ELEMENT_TYPE) return NATURAL;
    procedure       Remove(Values : ELEMENT_ARRAY);
    procedure       Resize(Size : positive);
    procedure       Set(Index : NATURAL; Value : ELEMENT_TYPE);
    impure function Set(Index : NATURAL; Value : ELEMENT_TYPE) return ELEMENT_TYPE;
    impure function Size return positive;
    impure function ToArray(Start : INTEGER := 0; Stop : INTEGER := -1; COUNT : NATURAL := 0) return ELEMENT_ARRAY;
  end protected;
end package;

The package corelib should be extended by these lines:

package corelib is
  -- Lists
  package Integer_List_Pkg  is new work.corelib_List generic map (ELEMENT_TYPE => INTEGER);
  package Natural_List_Pkg  is new work.corelib_List generic map (ELEMENT_TYPE => NATURAL);
  package Positive_List_Pkg is new work.corelib_List generic map (ELEMENT_TYPE => POSITIVE);
  
  alias Integer_List        is Integer_List_Pkg.PT_List;
  alias Natural_List        is Natural_List_Pkg.PT_List;
  alias Positive_List       is Positive_List_Pkg.PT_List;
end package;

General Comments

  • Using a protected type gives you the benefit of encapsulation but you can't pass a protected type to a function. An alternative is to use a record but then you lose encapsulation. Lieven Lemiengre 2016/08/29 05:11
  • I don't see this as a big disadvantage, as you can pass them to procedures. And the restriction passing them to a function will be removed with VHDL-2017 hopefully. T. Meissner 2016/09/05 17:35
  • Another point: Maybe there should some kind of feedback in case of an error. For example when trying to get content of an entry that doesn't exist. T. Meissner 2016/09/05 17:35

Supporters

proposals/data/genericlist/start.txt · Last modified: 10.10.2016 13:33 by tmeissner