virtual class dynamic_array #( type T = bit, int SIZE = 1 )
A parameterized class that manages a dynamic array.
| T | optional The type of a dynamic array. The default type is bit. |
| SIZE | optional The size of an unpacked array. This parameter is used only if a dynamic array is converted from/to an unpacked array. The default is 1. |
| dynamic_array | A parameterized class that manages a dynamic array. |
| Common Arguments | |
| Types | |
| ua_type | The shorthand of the unpacked array type of type T. |
| da_type | The shorthand of the dynamic array type of type T. |
| q_type | The shorthand of the queue type of type T. |
| Functions | |
| from_unpacked_array | static Converts an unpacked array of type T to a dynamic array of the same type. |
| to_unpacked_array | static Converts a dynamic array of type T to an unpacked array of the same type. |
| from_queue | static Converts a queue of type T to a dynamic array of the same type. |
| to_queue | static Converts a dynamic array of type T to a queue of the same type. |
| ua_to_da | static Converts an unpacked array of type T to a dynamic array of the same type. |
| da_to_ua | static Converts a dynamic array of type T to an unpacked array of the same type. |
| q_to_da | static Converts a queue of type T to a dynamic array of the same type. |
| da_to_q | static Converts a dynamic array of type T to a queue of the same type. |
| init | static Initializes the each element of the given dynamic array to the specified value. |
| reverse | static Reverses the order of the elements of the given dynamic array. |
| split | static Splits the given dynamic array into two dynamic arrays. |
| merge | static Merges two dynamic arrays into one by alternating the elements from the two arrays. |
| concat | static Concatenates two dynamic arrays into one. |
| extract | static Returns a new dynamic array by extracting a part of the given dynamic array. |
| append | static Appends the specified element to the given dynamic array. |
| compare | static Compares two dynamic arrays. |
| clone | static Returns a copy of the given dynamic array. |
| to_string | static Converts a dynamic array to the form of a string. |
| from_index | The index of the first element of a dynamic array to be processed. If negative, the index counts from the last. For example, if from_index is -9, a function starts at the ninth element (inclusive) from the last. The default is 0 (starts at the first element). |
| to_index | The index of the last element of a dynamic array to be processed. If negative, the index counts from the last. For example, if to_index is -3, a function ends at the third element (inclusive) from the last. The default is -1 (ends at the last element). |
static function da_type from_unpacked_array( const ref ua_type ua, input bit reverse = 0 )
static Converts an unpacked array of type T to a dynamic array of the same type.
| ua | An unpacked array to be converted. |
| reverse | optional If 0, the element at the index 0 of ua is positioned to the index 0 of the dynamic array. If 1, the elements are positioned in the reverse order. The default is 0. |
A dynamic array converted from ua.
bit ua[8] = '{ 0, 0, 0, 1, 1, 0, 1, 1 }; // same as ua[0:7]
bit da0[] = new[8]( '{ 0, 0, 0, 1, 1, 0, 1, 1 } );
bit da1[] = new[8]( '{ 1, 1, 0, 1, 1, 0, 0, 0 } );
assert( dynamic_array#(bit,8)::from_unpacked_array( ua ) == da0 );
assert( dynamic_array#(bit,8)::from_unpacked_array( ua, .reverse( 1 ) ) == da1 );
static function ua_type to_unpacked_array( const ref da_type da, input bit reverse = 0 )
static Converts a dynamic array of type T to an unpacked array of the same type.
| da | A dynamic array to be converted. |
| reverse | optional If 0, the element at the index 0 of da is positioned to the index 0 of the unpacked array. If 1, the elements are positioned in the reverse order. The default is 0. |
An unpacked array converted from da.
bit da[] = new[8]( '{ 0, 0, 0, 1, 1, 0, 1, 1 } );
bit ua0[8] = '{ 0, 0, 0, 1, 1, 0, 1, 1 };
bit ua1[8] = '{ 1, 1, 0, 1, 1, 0, 0, 0 };
assert( dynamic_array#(bit,8)::to_unpacked_array( da ) == ua0 );
assert( dynamic_array#(bit,8)::to_unpacked_array( da, .reverse( 1 ) ) == ua1 );
static function da_type from_queue( const ref q_type q, input bit reverse = 0 )
static Converts a queue of type T to a dynamic array of the same type.
| q | A queue to be converted. |
| reverse | optional If 0, the element at the index 0 of q is positioned to the index 0 of the dynamic array. If 1, the elements are positioned in the reverse order. The default is 0. |
A dynamic array converted from q.
bit q[$] = { 0, 0, 0, 1, 1, 0, 1, 1 }; // q[0] to q[7]
bit da0[] = new[8]( '{ 0, 0, 0, 1, 1, 0, 1, 1 } );
bit da1[] = new[8]( '{ 1, 1, 0, 1, 1, 0, 0, 0 } );
assert( dynamic_array#(bit)::from_queue( q ) == da0 );
assert( dynamic_array#(bit)::from_queue( q, .reverse( 1 ) ) == da1 );
static function q_type to_queue( const ref da_type da, input bit reverse = 0 )
static Converts a dynamic array of type T to a queue of the same type.
| da | A dynamic array to be converted. |
| reverse | optional If 0, the element at the index 0 of da is positioned to the index 0 of the queue. If 1, the elements are positioned in the reverse order. The default is 0. |
A queue converted from da.
bit da[] = new[8]( '{ 0, 0, 0, 1, 1, 0, 1, 1 } );
bit q0[$] = { 0, 0, 0, 1, 1, 0, 1, 1 };
bit q1[$] = { 1, 1, 0, 1, 1, 0, 0, 0 };
assert( dynamic_array#(bit)::to_queue( da ) == q0 );
assert( dynamic_array#(bit)::to_queue( da, .reverse( 1 ) ) == q1 );
static function void ua_to_da( const ref ua_type ua, ref da_type da, input bit reverse = 0 )
static Converts an unpacked array of type T to a dynamic array of the same type. Unlike from_unpacked_array, this function populates the dynamic array passed by reference, instead of returning a new dynamic array.
| ua | An unpacked array to be converted. |
| da | A dynamic array to be populated. This function does not resize da. Make sure to set the size of the dynamic array to accommodate the elements of ua before calling this function. |
| reverse | optional If 0, the element at the index 0 of ua is positioned to the index 0 of da. If 1, the elements are positioned in the reverse order. The default is 0. |
None.
bit ua[8] = '{ 0, 0, 0, 1, 1, 0, 1, 1 }; // assigned to ua[0:7]
bit da0[] = new[8]( '{ 0, 0, 0, 1, 1, 0, 1, 1 } );
bit da1[] = new[8]( '{ 1, 1, 0, 1, 1, 0, 0, 0 } );
bit da [] = new[8]; // set the size of da[]
dynamic_array#(bit,8)::ua_to_da( ua, da );
assert( da == da0 );
dynamic_array#(bit,8)::ua_to_da( ua, da, .reverse( 1 ) );
assert( da == da1 );
static function void da_to_ua( const ref da_type da, ref ua_type ua, input bit reverse = 0 )
static Converts a dynamic array of type T to an unpacked array of the same type. Unlike to_unpacked_array, this function populates the unpacked array passed by reference, instead of returning a new unpacked array. If the size of the dynamic array is larger than SIZE, the excess elements are ignored. If the size of the dynamic array is smaller than SIZE, the default value of type T is used for the missing elements.
| da | A dynamic array to be converted. |
| ua | An unpacked array to be populated. |
| reverse | optional If 0, the element at the index 0 of da is positioned to the index 0 of ua. If 1, the elements are positioned in the reverse order. The default is 0. |
None.
bit da[] = new[8]( '{ 0, 0, 0, 1, 1, 0, 1, 1 } ); // da[0] to da[7]
bit ua0[8] = '{ 0, 0, 0, 1, 1, 0, 1, 1 };
bit ua1[8] = '{ 1, 1, 0, 1, 1, 0, 0, 0 };
bit ua [8];
dynamic_array#(bit,8)::da_to_ua( da, ua );
assert( ua == ua0 );
dynamic_array#(bit,8)::da_to_ua( da, ua, .reverse( 1 ) );
assert( ua == ua1 );
static function void q_to_da( const ref q_type q, ref da_type da, input bit reverse = 0 )
static Converts a queue of type T to a dynamic array of the same type. Unlike from_queue, this function populates the dynamic array passed by reference, instead of returning a new dynamic array.
| q | A queue to be converted. |
| da | A dynamic array to be populated. |
| reverse | optional If 0, the element at the index 0 of q is positioned to the index 0 of da. If 1, the elements are positioned in the reverse order. The default is 0. |
None.
bit q[$] = { 0, 0, 0, 1, 1, 0, 1, 1 }; // q[0] to q[7]
bit da0[] = new[8]( '{ 0, 0, 0, 1, 1, 0, 1, 1 } );
bit da1[] = new[8]( '{ 1, 1, 0, 1, 1, 0, 0, 0 } );
bit da [] = new[8]; // set the size of da[]
dynamic_array#(bit)::q_to_da( q, da );
assert( da == da0 );
dynamic_array#(bit)::q_to_da( q, da, .reverse( 1 ) );
assert( da == da1 );
static function void da_to_q( const ref da_type da, ref q_type q, input bit reverse = 0 )
static Converts a dynamic array of type T to a queue of the same type. Unlike to_queue, this function populates the queue passed by reference instead of returning a new queue.
| da | A dynamic array to be converted. |
| q | A queue to be populated. This function does not change the size of q. Make sure that q has enough items to accommodate the elements of da before calling this function. |
| reverse | optional If 0, the element at the index 0 of da is positioned to the index 0 of q. If 1, the elements are positioned in the reverse order. The default is 0. |
None.
bit da[] = new[8]( '{ 0, 0, 0, 1, 1, 0, 1, 1 } ); // da[0] to da[7]
bit q0[$] = { 0, 0, 0, 1, 1, 0, 1, 1 };
bit q1[$] = { 1, 1, 0, 1, 1, 0, 0, 0 };
bit q [$];
dynamic_array#(bit)::da_to_q( da, q );
assert( q == q0 );
q.delete();
dynamic_array#(bit)::da_to_q( da, q, .reverse( 1 ) );
assert( q == q1 );
static function void init( ref da_type da, input T val )
static Initializes the each element of the given dynamic array to the specified value.
| da | A dynamic array to be initialized. |
| val | A value to initialize the elements of da. |
None.
bit da[] = new[8];
bit expected[] = new[8]( '{ 1, 1, 1, 1, 1, 1, 1, 1 } );
dynamic_array#(bit)::init( da, 1'b1 );
assert( da == expected );
static function void reverse( ref da_type da )
static Reverses the order of the elements of the given dynamic array.
| da | A dynamic array to be reversed. |
None.
bit da[] = new[8]( '{ 0, 0, 0, 0, 1, 1, 1, 1 } ); // da[0] to da[7]
bit expected[] = new[8]( '{ 1, 1, 1, 1, 0, 0, 0, 0 } );
dynamic_array#(bit)::reverse( da );
assert( da == expected );
static function void split( da_type da, ref da_type da0, ref da_type da1, input bit pad = 0 )
static Splits the given dynamic array into two dynamic arrays.
| da | A dynamic array to be split. |
| da0 | A new dynamic array that contains the elements at the even index of da. |
| da1 | A new dynamic array that contains the elements at the odd index of da. |
| pad | optional If the size of da is odd and pad is 1, the size of da1 is expanded to be the same size as da0. The padded element is initialized with the default value of type T. If 0, no padding element is added. The default is 0. |
bit da[] = new[7]( '{ 0, 0, 0, 1, 1, 0, 1 } ); // da[0] to da[6]
bit da0[], da1[], expected_da0[], expected_da1[];
expected_da0 = new[4]( '{ 0, 0, 1, 1 } ); // da[0], da[2], da[4], da[6]
expected_da1 = new[3]( '{ 0, 1, 0 } ); // da[1], da[3], da[5]
dynamic_array#(bit)::split( da, da0, da1 );
assert( da0 == expected_da0 );
assert( da1 == expected_da1 );
expected_da0 = new[4]( '{ 0, 0, 1, 1 } ); // da[0], da[2], da[4], da[6]
expected_da1 = new[4]( '{ 0, 1, 0, 0 } ); // the last element is padded with the default value of bit type
dynamic_array#(bit)::split( da, da0, da1, .pad( 1 ) );
assert( da0 == expected_da0 );
assert( da1 == expected_da1 );
static function da_type merge( da_type da0, da_type da1, bit truncate = 0 )
static Merges two dynamic arrays into one by alternating the elements from the two arrays.
| da0 | A dynamic array to be merged. The first element of this dynamic array becomes the first element of the merged array. |
| da1 | Another dynamic array to be merged. The first element of this dynamic array becomes the second element of the merged array. |
| truncate | optional If the sizes of da0 and da1 are different and truncate is 1, the merging stops when all the elements of the smaller array are merged. The remaining elements of the larger array are ignored. If truncate is 0, the remaining elements are appended to the merged array. The default is 0. |
A new merged dynamic array.
int da0[] = new[4]( '{ 0, 0, 0, 0 } );
int da1[] = new[6]( '{ 1, 2, 3, 4, 5, 6 } );
int expected[];
expected = new[10]( '{ 0, 1, 0, 2, 0, 3, 0, 4, 5, 6 } );
assert( dynamic_array#(int)::merge( da0, da1 ) == expected );
expected = new[8]( '{ 0, 1, 0, 2, 0, 3, 0, 4 } );
assert( dynamic_array#(int)::merge( da0, da1, .truncate( 1 ) ) == expected );
static function da_type concat( da_type da0, da_type da1 )
static Concatenates two dynamic arrays into one.
| da0 | A dynamic array. This array becomes the first part of the concatenated array. |
| da1 | Another dynamic array. The elements of this array are appended to da0. |
A new dynamic array created by concatenating da0 and da1.
int da0[] = new[4] ( '{ 0, 0, 0, 0 } );
int da1[] = new[6] ( '{ 1, 2, 3, 4, 5, 6 } );
int expected[] = new[10]( '{ 0, 0, 0, 0, 1, 2, 3, 4, 5, 6 } );
assert( dynamic_array#(int)::concat( da0, da1 ) == expected );
static function da_type extract( da_type da, int from_index = 0, int to_index = -1 )
static Returns a new dynamic array by extracting a part of the given dynamic array.
| da | A dynamic array to be extracted. |
| from_index | optional The index of the first element of da to be extracted. See Common Arguments. The default is 0. |
| to_index | optional The index of the last element of da to be extracted. See Common Arguments. The default is -1. |
A new dynamic array extracted from da.
int da[] = new[10]( '{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } );
int expected[] = new[5] ( '{ 3, 4, 5, 6, 7 } );
assert( dynamic_array#(int)::extract( da, 3, 7 ) == expected );
assert( dynamic_array#(int)::extract( da, 3, -3 ) == expected );
static function da_type append( da_type da, T e )
static Appends the specified element to the given dynamic array.
| da | A dynamic array to be appended. |
| e | An element to append. |
A copy of da appended with e. The input da is not modified.
int da[] = new[10]( '{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } );
int original[] = new[10]( '{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } );
int expected[] = new[11]( '{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } );
assert( dynamic_array#(int)::append( da, 10 ) == expected );
assert( da == original ); // da is not modified
static function bit compare( const ref da_type da1, const ref da_type da2, input int from_index1 = 0, int to_index1 = -1, int from_index2 = 0, int to_index2 = -1, comparator#(T) cmp = null )
static Compares two dynamic arrays.
| da1 | A dynamic array. |
| da2 | Another dynamic array to compare with da1. |
| from_index1 | optional The first index of the da1 to compare. See Common Arguments. The default is 0. |
| to_index1 | optional The last index of the da1 to compare. See Common Arguments. The default is -1. |
| from_index2 | optional The first index of the da2 to compare. See Common Arguments. The default is 0. |
| to_index2 | optional The last index of the da2 to compare. See Common Arguments. The default is -1. |
| cmp | optional A strategy object used to compare two dynamic arrays. If not specified or null, comparator#(T) is used. The default is null. |
If the numbers of elements to compare (to_index1-from_index1+1 and to_index2-from_index2+1) are different, 0 is returned. If the two dynamic arrays contain the same data in the specified range, 1 is returned. Otherwise, 0 is returned.
bit da1[] = new[8]( '{ 0, 0, 0, 1, 1, 0, 1, 1 } );
bit da2[] = new[8]( '{ 1, 1, 0, 1, 1, 0, 0, 0 } );
// |<------>|
// 2 5
assert( dynamic_array#(bit)::compare( da1, da2 ) == 0 );
assert( dynamic_array#(bit)::compare( da1, da2,
.from_index1( 2 ), .to_index1( 5 ),
.from_index2( 2 ), .to_index2( 5 ) ) == 1 );
static function da_type clone( da_type da )
static Returns a copy of the given dynamic array.
| da | A dynamic array to be cloned. |
A copy of da.
bit da[] = new[8]( '{ 0, 0, 0, 1, 1, 0, 1, 1 } );
bit expected[] = new[8]( '{ 0, 0, 0, 1, 1, 0, 1, 1 } );
assert( dynamic_array#(bit)::clone( da ) == expected );
static function string to_string( const ref da_type da, input string separator = " ", int from_index = 0, int to_index = -1, formatter#(T) fmtr = null )
static Converts a dynamic array to the form of a string.
| da | An dynamic array to be converted. |
| separator | optional A string to separate each element of da. The default is a space (“ “). |
| from_index | optional The index of the first element of da to convert. See Common Arguments. The default is 0. |
| to_index | optional The index of the last element of da to convert. See Common Arguments. The default is -1. |
| fmtr | optional A strategy object used to format da. If not specified or null, hex_formatter #(T) is used. The default is null. |
A string to represent da.
bit da[] = new[8]( '{ 0, 0, 0, 1, 1, 0, 1, 1 } );
assert( dynamic_array#(bit,8)::to_string( da ) == "0 0 0 1 1 0 1 1" );
assert( dynamic_array#(bit,8)::to_string( da, .separator( "-" ) ) == "0-0-0-1-1-0-1-1" );
assert( dynamic_array#(bit,8)::to_string( da, .from_index( 4 ) ) == "1 0 1 1" );A parameterized class that manages a dynamic array.
virtual class dynamic_array #( type T = bit, int SIZE = 1 )
The shorthand of the unpacked array type of type T.
typedef T ua_type[SIZE]
The shorthand of the dynamic array type of type T.
typedef T da_type[]
The shorthand of the queue type of type T.
typedef T q_type[$]
static Converts an unpacked array of type T to a dynamic array of the same type.
static function da_type from_unpacked_array( const ref ua_type ua, input bit reverse = 0 )
static Converts a dynamic array of type T to an unpacked array of the same type.
static function ua_type to_unpacked_array( const ref da_type da, input bit reverse = 0 )
static Converts a queue of type T to a dynamic array of the same type.
static function da_type from_queue( const ref q_type q, input bit reverse = 0 )
static Converts a dynamic array of type T to a queue of the same type.
static function q_type to_queue( const ref da_type da, input bit reverse = 0 )
static Converts an unpacked array of type T to a dynamic array of the same type.
static function void ua_to_da( const ref ua_type ua, ref da_type da, input bit reverse = 0 )
static Converts a dynamic array of type T to an unpacked array of the same type.
static function void da_to_ua( const ref da_type da, ref ua_type ua, input bit reverse = 0 )
static Converts a queue of type T to a dynamic array of the same type.
static function void q_to_da( const ref q_type q, ref da_type da, input bit reverse = 0 )
static Converts a dynamic array of type T to a queue of the same type.
static function void da_to_q( const ref da_type da, ref q_type q, input bit reverse = 0 )
static Initializes the each element of the given dynamic array to the specified value.
static function void init( ref da_type da, input T val )
static Reverses the order of the elements of the given dynamic array.
static function void reverse( ref da_type da )
static Splits the given dynamic array into two dynamic arrays.
static function void split( da_type da, ref da_type da0, ref da_type da1, input bit pad = 0 )
static Merges two dynamic arrays into one by alternating the elements from the two arrays.
static function da_type merge( da_type da0, da_type da1, bit truncate = 0 )
static Concatenates two dynamic arrays into one.
static function da_type concat( da_type da0, da_type da1 )
static Returns a new dynamic array by extracting a part of the given dynamic array.
static function da_type extract( da_type da, int from_index = 0, int to_index = -1 )
static Appends the specified element to the given dynamic array.
static function da_type append( da_type da, T e )
static Compares two dynamic arrays.
static function bit compare( const ref da_type da1, const ref da_type da2, input int from_index1 = 0, int to_index1 = -1, int from_index2 = 0, int to_index2 = -1, comparator#(T) cmp = null )
static Returns a copy of the given dynamic array.
static function da_type clone( da_type da )
static Converts a dynamic array to the form of a string.
static function string to_string( const ref da_type da, input string separator = " ", int from_index = 0, int to_index = -1, formatter#(T) fmtr = null )
singleton Provides a strategy to convert an object of type T to a string using a hexadecimal format.
class hex_formatter #( type T = int ) extends formatter#( T )