Skip to content

Commit

Permalink
keep subprogram type on the same line as the name
Browse files Browse the repository at this point in the history
  • Loading branch information
umarcor committed May 17, 2019
1 parent 9eb8971 commit 0db2959
Show file tree
Hide file tree
Showing 15 changed files with 431 additions and 848 deletions.
98 changes: 27 additions & 71 deletions vunit/vhdl/data_types/src/dict_pkg.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,40 @@ use work.integer_vector_ptr_pool_pkg.all;

package dict_pkg is
type dict_t is record
p_meta : integer_vector_ptr_t;
p_meta : integer_vector_ptr_t;
p_bucket_lengths : integer_vector_ptr_t;
p_bucket_keys : integer_vector_ptr_t;
p_bucket_values : integer_vector_ptr_t;
p_bucket_keys : integer_vector_ptr_t;
p_bucket_values : integer_vector_ptr_t;
end record;
constant null_dict : dict_t := (others => null_ptr);

impure function
new_dict
impure function new_dict
return dict_t;

procedure
deallocate(
procedure deallocate (
variable dict : inout dict_t
);

procedure
set(
procedure set (
dict : dict_t;
key, value : string
);

impure function
get(
impure function get (
dict : dict_t;
key : string
) return string;

impure function
has_key(
impure function has_key (
dict : dict_t;
key : string
) return boolean;

impure function
num_keys(
impure function num_keys (
dict : dict_t
) return natural;

procedure
remove(
procedure remove (
dict : dict_t;
key : string
);
Expand All @@ -60,14 +53,11 @@ end package;
package body dict_pkg is
constant int_pool : integer_vector_ptr_pool_t := new_integer_vector_ptr_pool;
constant str_pool : string_ptr_pool_t := new_string_ptr_pool;

constant meta_num_keys : natural := 0;
constant meta_length : natural := meta_num_keys+1;

constant new_bucket_size : natural := 1;

impure function
new_dict
impure function new_dict
return dict_t is
variable dict : dict_t;
variable tmp : integer_vector_ptr_t;
Expand All @@ -77,24 +67,19 @@ package body dict_pkg is
p_bucket_lengths => new_integer_vector_ptr(int_pool, num_buckets),
p_bucket_keys => new_integer_vector_ptr(int_pool, num_buckets),
p_bucket_values => new_integer_vector_ptr(int_pool, num_buckets));

set(dict.p_meta, meta_num_keys, 0);

for i in 0 to length(dict.p_bucket_lengths)-1 loop
-- Zero items in bucket
set(dict.p_bucket_lengths, i, 0);

tmp := new_integer_vector_ptr(int_pool, new_bucket_size);
set(dict.p_bucket_keys, i, to_integer(tmp));

tmp := new_integer_vector_ptr(int_pool, new_bucket_size);
set(dict.p_bucket_values, i, to_integer(tmp));
end loop;
return dict;
end;

procedure
deallocate(
procedure deallocate (
variable dict : inout dict_t
) is
constant num_buckets : natural := length(dict.p_bucket_lengths);
Expand All @@ -112,27 +97,23 @@ package body dict_pkg is
bucket_keys := to_integer_vector_ptr(get(dict.p_bucket_keys, bucket_idx));
bucket_values := to_integer_vector_ptr(get(dict.p_bucket_values, bucket_idx));
bucket_length := get(dict.p_bucket_lengths, bucket_idx);

for idx in 0 to bucket_length-1 loop
key := to_string_ptr(get(bucket_keys, idx));
value := to_string_ptr(get(bucket_values, idx));
recycle(str_pool, key);
recycle(str_pool, value);
end loop;

recycle(int_pool, bucket_values);
recycle(int_pool, bucket_keys);
end loop;

recycle(int_pool, dict.p_meta);
recycle(int_pool, dict.p_bucket_lengths);
recycle(int_pool, dict.p_bucket_values);
recycle(int_pool, dict.p_bucket_keys);
end;

-- DJB2 hash
impure function
hash(
impure function hash (
str : string
) return natural is
variable value : natural := 5381;
Expand All @@ -143,17 +124,14 @@ package body dict_pkg is
return value;
end;

impure function
get_value_ptr(
impure function get_value_ptr (
dict : dict_t;
key_hash : natural;
key : string
) return string_ptr_t is
constant num_buckets : natural := length(dict.p_bucket_lengths);
constant bucket_idx : natural := key_hash mod num_buckets;

constant bucket_length : natural := get(dict.p_bucket_lengths, bucket_idx);

constant bucket_values : integer_vector_ptr_t := to_integer_vector_ptr(get(dict.p_bucket_values, bucket_idx));
constant bucket_keys : integer_vector_ptr_t := to_integer_vector_ptr(get(dict.p_bucket_keys, bucket_idx));
begin
Expand All @@ -165,8 +143,7 @@ package body dict_pkg is
return null_string_ptr;
end;

procedure
remove(
procedure remove (
dict : dict_t;
bucket_idx : natural;
i : natural;
Expand All @@ -175,33 +152,27 @@ package body dict_pkg is
constant bucket_length : natural := get(dict.p_bucket_lengths, bucket_idx);
constant bucket_values : integer_vector_ptr_t := to_integer_vector_ptr(get(dict.p_bucket_values, bucket_idx));
constant bucket_keys : integer_vector_ptr_t := to_integer_vector_ptr(get(dict.p_bucket_keys, bucket_idx));

variable key, value : string_ptr_t;
begin

if deallocate_item then
key := to_string_ptr(get(bucket_keys, i));
value := to_string_ptr(get(bucket_values, i));
recycle(str_pool, key);
recycle(str_pool, value);
end if;

set(bucket_keys, i, get(bucket_keys, bucket_length-1));
set(bucket_values, i, get(bucket_values, bucket_length-1));

set(dict.p_bucket_lengths, bucket_idx, bucket_length-1);
set(dict.p_meta, meta_num_keys, num_keys(dict)-1);
end;

procedure
remove(
procedure remove (
dict : dict_t;
key_hash : natural;
key : string
) is
constant num_buckets : natural := length(dict.p_bucket_lengths);
constant bucket_idx : natural := key_hash mod num_buckets;

constant bucket_length : natural := get(dict.p_bucket_lengths, bucket_idx);
constant bucket_keys : integer_vector_ptr_t := to_integer_vector_ptr(get(dict.p_bucket_keys, bucket_idx));
begin
Expand All @@ -213,22 +184,19 @@ package body dict_pkg is
end loop;
end;

procedure
insert_new(
procedure insert_new (
dict : dict_t;
key_hash : natural;
key, value : string_ptr_t
);

procedure
relocate_items(
procedure relocate_items (
dict : dict_t;
old_num_buckets : natural
) is
constant num_buckets : natural := length(dict.p_bucket_lengths);
variable bucket_values : integer_vector_ptr_t;
variable bucket_keys : integer_vector_ptr_t;

variable idx : natural;
variable key_hash : natural;
variable key : string_ptr_t;
Expand Down Expand Up @@ -261,9 +229,8 @@ package body dict_pkg is
end loop;
end;

procedure
resize(
dict : dict_t;
procedure resize (
dict : dict_t;
num_buckets : natural
) is
constant old_num_buckets : natural := length(dict.p_bucket_lengths);
Expand All @@ -282,9 +249,8 @@ package body dict_pkg is
relocate_items(dict, old_num_buckets);
end;

procedure
set(
dict : dict_t;
procedure set (
dict : dict_t;
key, value : string
) is
constant key_hash : natural := hash(key);
Expand All @@ -298,21 +264,17 @@ package body dict_pkg is
end if;
end;

procedure
insert_new(
procedure insert_new (
dict : dict_t;
key_hash : natural;
key, value : string_ptr_t
) is
constant num_buckets : natural := length(dict.p_bucket_lengths);
constant bucket_idx : natural := key_hash mod num_buckets;

constant bucket_length : natural := get(dict.p_bucket_lengths, bucket_idx);

constant bucket_values : integer_vector_ptr_t := to_integer_vector_ptr(get(dict.p_bucket_values, bucket_idx));
constant bucket_keys : integer_vector_ptr_t := to_integer_vector_ptr(get(dict.p_bucket_keys, bucket_idx));
constant bucket_max_length : natural := length(bucket_values);

constant num_keys : natural := get(dict.p_meta, meta_num_keys);
begin
if num_keys > num_buckets then
Expand All @@ -321,22 +283,19 @@ package body dict_pkg is
resize(dict, 2*num_buckets);
insert_new(dict, key_hash, key, value);
return;

elsif bucket_length = bucket_max_length then
-- Bucket size to small, resize
resize(bucket_keys, bucket_max_length+1);
resize(bucket_values, bucket_max_length+1);
end if;

set(dict.p_meta, meta_num_keys, num_keys+1);
set(dict.p_bucket_lengths, bucket_idx, bucket_length+1);
-- Create new value storage
set(bucket_keys, bucket_length, to_integer(key));
set(bucket_values, bucket_length, to_integer(value));
end;

impure function
get(
impure function get (
dict : dict_t;
key : string
) return string is
Expand All @@ -347,8 +306,7 @@ package body dict_pkg is
return to_string(value_ptr);
end;

impure function
has_key(
impure function has_key (
dict : dict_t;
key : string
) return boolean is
Expand All @@ -357,8 +315,7 @@ package body dict_pkg is
return get_value_ptr(dict, key_hash, key) /= null_string_ptr;
end;

procedure
remove(
procedure remove (
dict : dict_t;
key : string
) is
Expand All @@ -367,8 +324,7 @@ package body dict_pkg is
remove(dict, key_hash, key);
end;

impure function
num_keys(
impure function num_keys (
dict : dict_t
) return natural is begin
return get(dict.p_meta, meta_num_keys);
Expand Down
Loading

0 comments on commit 0db2959

Please sign in to comment.