SystemVerilog provides powerful built-in methods for arrays:
Reduction Methods
int arr[] = '{1, 2, 3, 4, 5};
int sum = arr.sum(); // 15
int product = arr.product(); // 120
int bitwise_and = arr.and(); // Bitwise AND of all
int bitwise_or = arr.or(); // Bitwise OR of all
int bitwise_xor = arr.xor(); // Bitwise XOR of all
Locator Methods
int arr[] = '{5, 10, 15, 20, 25};
// Find elements matching condition
int gt15[] = arr.find with (item > 15); // {20, 25}
int indices[] = arr.find_index with (item > 15); // {3, 4}
// Find first/last
int first = arr.find_first with (item > 10); // 15
int last = arr.find_last with (item < 20); // 15
// Min and Max
int minimum = arr.min(); // 5
int maximum = arr.max(); // 25
Sorting and Ordering
int arr[] = '{30, 10, 50, 20, 40};
arr.sort(); // {10, 20, 30, 40, 50}
arr.rsort(); // {50, 40, 30, 20, 10}
arr.shuffle(); // Random order
arr.reverse(); // Reverse current order