Skip to content

Commit 99cc71d

Browse files
authored
Merge pull request VUnit#482 from dbhi/style-arrays
style: data_types
2 parents 9ae0ae0 + 0db2959 commit 99cc71d

22 files changed

+1889
-968
lines changed

vunit/builtins.py

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def _add_data_types(self):
7272
"""
7373
Add data types packages
7474
"""
75+
self._add_files(join(VHDL_PATH, "data_types", "src", "types", "*.vhd"))
7576
self._add_files(join(VHDL_PATH, "data_types", "src", "*.vhd"))
7677

7778
def _add_array_util(self):

vunit/vhdl/com/src/com_string.vhd

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ package body com_string_pkg is
217217
constant data : integer_vector_ptr_t)
218218
return string is
219219
begin
220-
return create_group(1, to_string(data.index));
220+
return create_group(1, to_string(data.ref));
221221
end;
222222

223223
function to_string (

vunit/vhdl/data_types/src/dict_pkg.vhd

+96-51
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,54 @@ use work.integer_vector_ptr_pool_pkg.all;
1111

1212
package dict_pkg is
1313
type dict_t is record
14-
p_meta : integer_vector_ptr_t;
14+
p_meta : integer_vector_ptr_t;
1515
p_bucket_lengths : integer_vector_ptr_t;
16-
p_bucket_keys : integer_vector_ptr_t;
17-
p_bucket_values : integer_vector_ptr_t;
16+
p_bucket_keys : integer_vector_ptr_t;
17+
p_bucket_values : integer_vector_ptr_t;
1818
end record;
1919
constant null_dict : dict_t := (others => null_ptr);
2020

21-
impure function new_dict return dict_t;
22-
procedure deallocate(variable dict : inout dict_t);
21+
impure function new_dict
22+
return dict_t;
2323

24-
procedure set(dict : dict_t; key, value : string);
25-
impure function get(dict : dict_t; key : string) return string;
26-
impure function has_key(dict : dict_t; key : string) return boolean;
27-
impure function num_keys(dict : dict_t) return natural;
28-
procedure remove(dict : dict_t; key : string);
24+
procedure deallocate (
25+
variable dict : inout dict_t
26+
);
27+
28+
procedure set (
29+
dict : dict_t;
30+
key, value : string
31+
);
32+
33+
impure function get (
34+
dict : dict_t;
35+
key : string
36+
) return string;
37+
38+
impure function has_key (
39+
dict : dict_t;
40+
key : string
41+
) return boolean;
42+
43+
impure function num_keys (
44+
dict : dict_t
45+
) return natural;
46+
47+
procedure remove (
48+
dict : dict_t;
49+
key : string
50+
);
2951
end package;
3052

3153
package body dict_pkg is
3254
constant int_pool : integer_vector_ptr_pool_t := new_integer_vector_ptr_pool;
3355
constant str_pool : string_ptr_pool_t := new_string_ptr_pool;
34-
3556
constant meta_num_keys : natural := 0;
3657
constant meta_length : natural := meta_num_keys+1;
37-
3858
constant new_bucket_size : natural := 1;
3959

40-
impure function new_dict return dict_t is
60+
impure function new_dict
61+
return dict_t is
4162
variable dict : dict_t;
4263
variable tmp : integer_vector_ptr_t;
4364
constant num_buckets : natural := 1;
@@ -46,23 +67,21 @@ package body dict_pkg is
4667
p_bucket_lengths => new_integer_vector_ptr(int_pool, num_buckets),
4768
p_bucket_keys => new_integer_vector_ptr(int_pool, num_buckets),
4869
p_bucket_values => new_integer_vector_ptr(int_pool, num_buckets));
49-
5070
set(dict.p_meta, meta_num_keys, 0);
51-
5271
for i in 0 to length(dict.p_bucket_lengths)-1 loop
5372
-- Zero items in bucket
5473
set(dict.p_bucket_lengths, i, 0);
55-
5674
tmp := new_integer_vector_ptr(int_pool, new_bucket_size);
5775
set(dict.p_bucket_keys, i, to_integer(tmp));
58-
5976
tmp := new_integer_vector_ptr(int_pool, new_bucket_size);
6077
set(dict.p_bucket_values, i, to_integer(tmp));
6178
end loop;
6279
return dict;
63-
end function;
80+
end;
6481

65-
procedure deallocate(variable dict : inout dict_t) is
82+
procedure deallocate (
83+
variable dict : inout dict_t
84+
) is
6685
constant num_buckets : natural := length(dict.p_bucket_lengths);
6786

6887
variable bucket_values : integer_vector_ptr_t;
@@ -78,40 +97,41 @@ package body dict_pkg is
7897
bucket_keys := to_integer_vector_ptr(get(dict.p_bucket_keys, bucket_idx));
7998
bucket_values := to_integer_vector_ptr(get(dict.p_bucket_values, bucket_idx));
8099
bucket_length := get(dict.p_bucket_lengths, bucket_idx);
81-
82100
for idx in 0 to bucket_length-1 loop
83101
key := to_string_ptr(get(bucket_keys, idx));
84102
value := to_string_ptr(get(bucket_values, idx));
85103
recycle(str_pool, key);
86104
recycle(str_pool, value);
87105
end loop;
88-
89106
recycle(int_pool, bucket_values);
90107
recycle(int_pool, bucket_keys);
91108
end loop;
92-
93109
recycle(int_pool, dict.p_meta);
94110
recycle(int_pool, dict.p_bucket_lengths);
95111
recycle(int_pool, dict.p_bucket_values);
96112
recycle(int_pool, dict.p_bucket_keys);
97113
end;
98114

99115
-- DJB2 hash
100-
impure function hash(str : string) return natural is
116+
impure function hash (
117+
str : string
118+
) return natural is
101119
variable value : natural := 5381;
102120
begin
103121
for i in str'range loop
104122
value := (33*value + character'pos(str(i))) mod 2**(31-6);
105123
end loop;
106124
return value;
107-
end function;
125+
end;
108126

109-
impure function get_value_ptr(dict : dict_t; key_hash : natural; key : string) return string_ptr_t is
127+
impure function get_value_ptr (
128+
dict : dict_t;
129+
key_hash : natural;
130+
key : string
131+
) return string_ptr_t is
110132
constant num_buckets : natural := length(dict.p_bucket_lengths);
111133
constant bucket_idx : natural := key_hash mod num_buckets;
112-
113134
constant bucket_length : natural := get(dict.p_bucket_lengths, bucket_idx);
114-
115135
constant bucket_values : integer_vector_ptr_t := to_integer_vector_ptr(get(dict.p_bucket_values, bucket_idx));
116136
constant bucket_keys : integer_vector_ptr_t := to_integer_vector_ptr(get(dict.p_bucket_keys, bucket_idx));
117137
begin
@@ -123,32 +143,36 @@ package body dict_pkg is
123143
return null_string_ptr;
124144
end;
125145

126-
procedure remove(dict : dict_t; bucket_idx : natural; i : natural; deallocate_item : boolean := true) is
146+
procedure remove (
147+
dict : dict_t;
148+
bucket_idx : natural;
149+
i : natural;
150+
deallocate_item : boolean := true
151+
) is
127152
constant bucket_length : natural := get(dict.p_bucket_lengths, bucket_idx);
128153
constant bucket_values : integer_vector_ptr_t := to_integer_vector_ptr(get(dict.p_bucket_values, bucket_idx));
129154
constant bucket_keys : integer_vector_ptr_t := to_integer_vector_ptr(get(dict.p_bucket_keys, bucket_idx));
130-
131155
variable key, value : string_ptr_t;
132156
begin
133-
134157
if deallocate_item then
135158
key := to_string_ptr(get(bucket_keys, i));
136159
value := to_string_ptr(get(bucket_values, i));
137160
recycle(str_pool, key);
138161
recycle(str_pool, value);
139162
end if;
140-
141163
set(bucket_keys, i, get(bucket_keys, bucket_length-1));
142164
set(bucket_values, i, get(bucket_values, bucket_length-1));
143-
144165
set(dict.p_bucket_lengths, bucket_idx, bucket_length-1);
145166
set(dict.p_meta, meta_num_keys, num_keys(dict)-1);
146167
end;
147168

148-
procedure remove(dict : dict_t; key_hash : natural; key : string) is
169+
procedure remove (
170+
dict : dict_t;
171+
key_hash : natural;
172+
key : string
173+
) is
149174
constant num_buckets : natural := length(dict.p_bucket_lengths);
150175
constant bucket_idx : natural := key_hash mod num_buckets;
151-
152176
constant bucket_length : natural := get(dict.p_bucket_lengths, bucket_idx);
153177
constant bucket_keys : integer_vector_ptr_t := to_integer_vector_ptr(get(dict.p_bucket_keys, bucket_idx));
154178
begin
@@ -160,13 +184,19 @@ package body dict_pkg is
160184
end loop;
161185
end;
162186

163-
procedure insert_new(dict : dict_t; key_hash : natural; key, value : string_ptr_t);
187+
procedure insert_new (
188+
dict : dict_t;
189+
key_hash : natural;
190+
key, value : string_ptr_t
191+
);
164192

165-
procedure relocate_items(dict : dict_t; old_num_buckets : natural) is
193+
procedure relocate_items (
194+
dict : dict_t;
195+
old_num_buckets : natural
196+
) is
166197
constant num_buckets : natural := length(dict.p_bucket_lengths);
167198
variable bucket_values : integer_vector_ptr_t;
168199
variable bucket_keys : integer_vector_ptr_t;
169-
170200
variable idx : natural;
171201
variable key_hash : natural;
172202
variable key : string_ptr_t;
@@ -199,7 +229,10 @@ package body dict_pkg is
199229
end loop;
200230
end;
201231

202-
procedure resize(dict : dict_t; num_buckets : natural) is
232+
procedure resize (
233+
dict : dict_t;
234+
num_buckets : natural
235+
) is
203236
constant old_num_buckets : natural := length(dict.p_bucket_lengths);
204237
begin
205238
resize(dict.p_bucket_lengths, num_buckets);
@@ -216,7 +249,10 @@ package body dict_pkg is
216249
relocate_items(dict, old_num_buckets);
217250
end;
218251

219-
procedure set(dict : dict_t; key, value : string) is
252+
procedure set (
253+
dict : dict_t;
254+
key, value : string
255+
) is
220256
constant key_hash : natural := hash(key);
221257
constant old_value_ptr : string_ptr_t := get_value_ptr(dict, key_hash, key);
222258
begin
@@ -228,16 +264,17 @@ package body dict_pkg is
228264
end if;
229265
end;
230266

231-
procedure insert_new(dict : dict_t; key_hash : natural; key, value : string_ptr_t) is
267+
procedure insert_new (
268+
dict : dict_t;
269+
key_hash : natural;
270+
key, value : string_ptr_t
271+
) is
232272
constant num_buckets : natural := length(dict.p_bucket_lengths);
233273
constant bucket_idx : natural := key_hash mod num_buckets;
234-
235274
constant bucket_length : natural := get(dict.p_bucket_lengths, bucket_idx);
236-
237275
constant bucket_values : integer_vector_ptr_t := to_integer_vector_ptr(get(dict.p_bucket_values, bucket_idx));
238276
constant bucket_keys : integer_vector_ptr_t := to_integer_vector_ptr(get(dict.p_bucket_keys, bucket_idx));
239277
constant bucket_max_length : natural := length(bucket_values);
240-
241278
constant num_keys : natural := get(dict.p_meta, meta_num_keys);
242279
begin
243280
if num_keys > num_buckets then
@@ -246,42 +283,50 @@ package body dict_pkg is
246283
resize(dict, 2*num_buckets);
247284
insert_new(dict, key_hash, key, value);
248285
return;
249-
250286
elsif bucket_length = bucket_max_length then
251287
-- Bucket size to small, resize
252288
resize(bucket_keys, bucket_max_length+1);
253289
resize(bucket_values, bucket_max_length+1);
254290
end if;
255-
256291
set(dict.p_meta, meta_num_keys, num_keys+1);
257292
set(dict.p_bucket_lengths, bucket_idx, bucket_length+1);
258293
-- Create new value storage
259294
set(bucket_keys, bucket_length, to_integer(key));
260295
set(bucket_values, bucket_length, to_integer(value));
261-
end procedure;
296+
end;
262297

263-
impure function get(dict : dict_t; key : string) return string is
298+
impure function get (
299+
dict : dict_t;
300+
key : string
301+
) return string is
264302
constant key_hash : natural := hash(key);
265303
constant value_ptr : string_ptr_t := get_value_ptr(dict, key_hash, key);
266304
begin
267305
assert value_ptr /= null_string_ptr report "missing key '" & key & "'";
268306
return to_string(value_ptr);
269307
end;
270308

271-
impure function has_key(dict : dict_t; key : string) return boolean is
309+
impure function has_key (
310+
dict : dict_t;
311+
key : string
312+
) return boolean is
272313
constant key_hash : natural := hash(key);
273314
begin
274315
return get_value_ptr(dict, key_hash, key) /= null_string_ptr;
275316
end;
276317

277-
procedure remove(dict : dict_t; key : string) is
318+
procedure remove (
319+
dict : dict_t;
320+
key : string
321+
) is
278322
constant key_hash : natural := hash(key);
279323
begin
280324
remove(dict, key_hash, key);
281325
end;
282326

283-
impure function num_keys(dict : dict_t) return natural is
284-
begin
327+
impure function num_keys (
328+
dict : dict_t
329+
) return natural is begin
285330
return get(dict.p_meta, meta_num_keys);
286331
end;
287332

0 commit comments

Comments
 (0)