Here’s my full code.
I had have commented the queue reduction at lines 14 and 16.
Also the print issue I mentioned regarding the sum operation is at line
1. module tb_array_methods;
2. function void disp_msg(input string tag, input string msg);
3. $display($sformatf("[%0t][%0s]: %0s", $time, tag, msg));
4. endfunction
5. int arr[$] = {21,33,42,20,20,42,11,24,33};
6. longint res;
7. int arr_loc[$];
8. int res_2;
9. bit b_array[] = '{1,0,0,1,0,1};
10. ///// Array Reduction Methods
11. initial begin
12. res = arr.sum();
13. disp_msg("Block 1", $sformatf("res_sum = %0d", res));
14. // res = arr.product();
15. // disp_msg("Block 1", $sformatf("res_product = %0d", res));
16. // res = arr.and();
17. // disp_msg("Block 1", $sformatf("res_and = %0d", res));
18. end
19. // ====================================== //
20. // Array Locator Methods
21. // All array locator methods return the results as a queue of type "int" and not "integer"
22. initial begin
23. arr_loc = arr.min(); // Returns {11}
24. disp_msg("Block 2", $sformatf("arr_loc = %0p", arr_loc));
25. arr_loc = arr.max(); // Returns {42}
26. disp_msg("Block 2", $sformatf("arr_loc = %0p", arr_loc));
27. // Min and Max function also work for associative arrays
28. arr_loc = arr.unique();
29. disp_msg("Block 2", $sformatf("arr_loc = %0p", arr_loc));
30. // We require an queue variable to store these results. Why?
31. // Because in case of empty queues, the functions will return empty queue.
32. //////////////////////////
33. // "with" operator
34. arr_loc = arr.find() with (item > 34); // Returns a queue of type int
35. disp_msg("Block 2", $sformatf("arr_loc = %0p", arr_loc));
36. arr_loc = arr.find(x) with (x <42);
37. disp_msg("Block 2", $sformatf("arr_loc = %0p", arr_loc));
38. arr_loc = arr.find_index() with (item == 33);
39. disp_msg("Block 2", $sformatf("arr_loc = %0p", arr_loc));
40. arr_loc = arr.find_first_index() with (item == 32); // Returns an empty string
41. disp_msg("Block 2", $sformatf("arr_loc = %0p", arr_loc));
42. arr_loc = arr.find_last_index with (item == 20);
43. disp_msg("Block 2", $sformatf("arr_loc = %0p", arr_loc));
44. // How about we combine some boolean functions with locators?
45. //int arr[$] = {21,33,42,20,20,42,11,24,33};
46. res_2 = arr.sum() with (item>34);
47. // Returns sum({0,0,1,0,0,1,0,0,0}) = 2;
48. // This can be used to find the count of certain values
49. disp_msg("Block 3", $sformatf("res_2 = %0d", res_2));
50. res_2 = arr.sum() with (item * (item>34));
51. // Returns sum({0,0,42,0,0,42,0,0,0}) = 84;
52. res_2 = arr.sum() with (item > 34? item:0);
53. // Returns 84
54. disp_msg("Block 3", $sformatf("res_2 = %0d", res_2));
55.
56. // One has to be careful of bit widths!
57. // sum() function uses the width of the array's data
58. // So if we add values of a 1-bit array, using sum(), we will will get a single bit result of either 0 or 1
59. // So we need to type cast values before.
60. res_2 = b_array.sum(); // Total = (1 + 1 + 1) = 1 (in 1-bit addition)
61. disp_msg("Block 3", $sformatf("res_2 = %0d", res_2));
62. res_2 = b_array.sum() with (int'(item)); // Total = 1 + 1 + 1 = 3
63. disp_msg("Block 3", $sformatf("res_2 = %0d", res_2));
64. end
65. //===================================//
66. // Array Sorting and Ordering
67. // Unlike array locators and reduction operators, these change the original array!!
68. int d [] = '{2,1,2,42,3,5};
69. struct packed {bit [7:0] r,g,b;} pix[];
70.
71. initial begin
72. #40;
73. pix = '{'{r:7, g:34, b: 65}, '{r:70, g:49, b: 6},'{r:46, g:32, b: 63}};
74. //Structures also use '{} instead of {} for full structure assignment
75. d.reverse();
76. disp_msg("Block 4", $sformatf("Reverse = %0p", d));
77. d.sort();
78. disp_msg("Block 4", $sformatf("Sorted = %0p", d));
79. d.shuffle();
80. disp_msg("Block 4", $sformatf("Shuffled = %0p", d));
81. d.rsort();
82. disp_msg("Block 4", $sformatf("Reverse Sorted = %0p", d));
83. // reverse() and shuffle() don't have a "with" clause. But others have...
84. pix.sort() with (item.r); // Sort using r only
85. disp_msg("Block 5", $sformatf("Structure with sorting based on Red= %0p", pix));
86. // pix = '{'{r:7, g:34, b: 65}, '{r:46, g:32, b: 63}, '{r:70, g:49, b: 6}};
87. pix.sort() with ({item.g, item.b}); // Sort first based on g then b
88. disp_msg("Block 5", $sformatf("Structure with sorting based on Red= %0p", pix));
89. // pix = '{'{r:70, g:49, b: 6}, '{r:46, g:32, b: 63},'{r:7, g:34, b: 65}};
90. // Only fixed & dynamic arrays and queues can be sorted, reversed or shuffled
91. // Associative arrays cannot be re-ordered because it's a key based storage.
92. // We can't just say move this key before that key.
93.
94. end
95. endmodule