Specman: Error in on-the-fly generating of list of lists with all different values - specman

I try to generate on-the-fly list of list of uint (my_list_of_list) with all different values (I have a variable num_of_ms_in_each_g : list of uint, that keeps lengths of every list inside my_list_of_list):
var my_list_of_list : list of list of uint;
gen my_list_of_list keeping {
for each (g) using index (g_idx) in it {
g.size() == num_of_ms_in_each_g[g_idx];
for each (m) using index (m_idx) in g {
// Error in the next line:
m not in it[0..g_idx-1][0..num_of_ms_in_each_g[g_idx]-1];
m not in it[g_idx][0..max(0, m_idx-1)];
};
};
Explanation for the code algorithm: generate m (the value) that was not yet in any list of uint (g) before, and does not appear in current list for previous indexes.
I get compilation error:
*** Error: 'm' is of type 'uint', while expecting type 'list of uint' or
'list of list of uint'.
Do you have any idea how to solve the compilation error? (it[0..g_idx-1][0..num_of_ms_in_each_g[g_idx]-1] is uint..) Or maybe another way to generate on-the-fly list of list of uint with all different values?
Thank you for your help.

I would reduce the complexity of this constraint by using a sort of a unified list that contains all the items, and then
Break this list into the desired list of list ( as It is easier to generate a single unique list).
Also, in general, it is best to keep all non-generative operations outside of the constraints since it could be done procedurally
Afterwards which will improve the overall performance of generating such a field set.
I would do this using the following code:
var unified_list:list of uint;
var my_list_of_list : list of list of uint;
gen unified_list keeping {
it.size()==num_of_ms_in_each_g.sum(it);
it.all_different(it);
};
for each in num_of_ms_in_each_g {
var temp_list:list of uint;
for i from 0 to it-1 {
temp_list.add(unified_list.pop0());
};
my_list_of_list.add(temp_list);
};
thanks

Actually, the expression it[0..g_idx-1][0..num_of_ms_in_each_g[g_idx]-1] is of type list of list of uint. The operator list[from..to] produces a sub list. In your code you apply it twice to it which first produces a sublist and then produces a sublist of the sublist.
The second such constraint in your code works, because it[g_idx] does not produce a sublist, but rather accesses a list item, which is of type list of uint and then produces a sublist.
To produce an all different list of list I would do something like:
var my_list_of_list : list of list of uint;
for each (sz) in num_of_ms_in_each_g {
var l : list of uint;
gen l keeping {
it.size() == sz;
it.all_different(it);
// not it.has(it in my_list_of_list.flatten());
// for better performance
for each (itm) in it {
itm not in my_list_of_list.flatten();
};
};
my_list_of_list.add(l);
};

Related

RetainWhere Function Dart Uncertanties

I have a following code.
void onItemSelected(String status) {
print("default list count: " + transactionList.length.toString());
List<trx.Transaction> filteredTrx = transactionList;
print("default2 list count: " + transactionList.length.toString());
filteredTrx.retainWhere((element) => element.status == status);
print("default3 list count: " + transactionList.length.toString());
filteredTransactionList = filteredTrx;
}
I am doing a dropdownbutton to allow user to filter based on their status selection. In this example, the status of the transaction can be (processing, approved, rejected).
I know that I can use foreach loop to compare and assign into a new list. But i would like to use a function which is more efficient. And i think that retainWhere could be a good solution for it.
So, I had a list of Transactions recorded into transactionList variable. And to prevent this list from changing, i declare a new list to store into it and apply it with retainWhere function. However, i noticed that once it runs the retainWhere function, the default transactionList will be empty as well. Does anyone know why?
The debug result is as below:
I/flutter (12068): default list count: 2
I/flutter (12068): default2 list count: 2
I/flutter (12068): default3 list count: 0
You are not copying the list, you are copying the reference to same list (see the second sentence here: https://dart.dev/guides/language/language-tour#variables). This is the reason why both lists are affected by retainWhere.
You probably want to (shallow) copy the list like this:
List<trx.Transaction> filteredTrx = List.from(transactionList)
https://api.dart.dev/stable/2.18.0/dart-core/List/List.from.html

How to loop through two lists at the same time - Flutter

I'm trying to loop through two lists at the same time
For example
List A = ['Chapter 1','Chapter 2','Chapter 3'];
List B = ['Paragraph 1','Paragraph 2','Paragraph 3'];
for(var chapter in A) // This would itreate through only first list (A), How can I iterate through both list
children: [
Text(chapter);
Text(paragraph);
]
I need to iterate through both lists at the same time in "for" loop.
Here is the code:
List A = ['Chapter 1','Chapter 2','Chapter 3'];
List B = ['Paragraph 1','Paragraph 2','Paragraph 3'];
for (int i = 0; i < A.length; i++) {
print ("${A[i]}");
print ("${B[i]}");
}
Let me know if this does not help.
final c = zip([a, b]).map((item) => Foo(item[0], item[1])).toList();
I went along with Randal Schwartz's note about IterableZip. Note that I was not able to find any zip mentioned by Gbenga, and also note that package:collection/iterable_zip.dart is deprecated, you'd need to import simply the collection.dart, like so:
import 'package:collection/collection.dart';
final A = ['Chapter 1','Chapter 2','Chapter 3'];
final B = ['Paragraph 1','Paragraph 2','Paragraph 3'];
for (final pairs in IterableZip([A, B])) {
print(pairs[0]);
print(pairs[1]);
}
Some more notes:
final A and final B variables will be typed List<String> lists, I disadvise dynamic List A.
I have not specified template type for IterableZip, but in some cases (in a complex case where I used it) you may need to explicitly type it like IterableZip<String>, so in the loop you can more conveniently access the members. In this simple case the compiler/interpreter can infer the type easily.
Some other answers in other StackOverflow issues mention quiver, but unless you need it for some other features you don't have to add another extra package dependency to your project. The standard collection package has this IterableZip

Have assertion error return respective list position instead of the object?

test("Comparison") {
val list: List[String] = List("Thing", "Entity", "Variable")
val expected: List[String] = List("Thingg", "Entityy", "Variablee")
var expectedPosition = 0
for (item <- list) {
assert(list(expectedPosition) == expected(expectedPosition))
expectedPosition += 1
}
}
In Scala, in order to make my low-level code tests more readable, I thought it would be a good idea to just use one assertion and have it iterate through a loop and increase an accumulator at the end.
This is to test more than one input at a time when the multiple inputs would more or less have similar attributes. When the assertion fails, it comes back as "Thing[]" did not equal "Thing[g]". Instead of it reporting the item in the list that failed, is there a way to get it to directly state the list position without concatenating the list position before the assertion or using a conditional statement that returns the list position? Like I would rather keep it all contained within the assert() error report.
val expected: LazyList[String] = ...
expected.zip(list)
.zipWithIndex
.foreach{case ((exp,itm),idx) =>
assert(exp == itm, s"off at index $idx")
}
//java.lang.AssertionError: assertion failed: off at index 0
expected is lazy so that it is traversed only once even though it gets zipped twice.

Specman: How to find if a list of bytes exists in another list taking order of the list into account

I have a payload which is a lists of type bytes:
var payload : list of byte;
payload= {1;2;3;4;5;6};
var item1 :list of byte;
item = {3;4;5};
var item2 :list of byte;
item = {1;4};
I would like to implement a code that checks if a list is a sub-list of another. Using "if ..in.." doesn't quite work as it does not take into account the order of the items or if they appear successively or not. I want something that does the following:
if (item1 in payload) ...... should return TRUE. Items exist in payload in the same order.
if (item2 in payload) ...... should return FALSE because although each element in the list exists in the payload, but the item2 elements do not appear successively in the payload list.
Is there an easy way to achieve this? There must be a build -in function in specman for this.
Thanks
The following code should work:
if (item.size()==0) {return TRUE};
for i from 0 to payload.size()-item.size() {
if (item == payload[i..i+item.size()-1]) {
return TRUE;
};
};
return FALSE;
Note that this code is quite expensive memory-wise (the list[a..b] syntax creates a new list every time) so if you have memory considerations it should be modified.

Specman list pseudo-method to pop/push from/to specific index

I am looking for a way to implement a new List pseud-method that would
push/pop from a certain location in the list (not necessarily from index 0).
is there a way to add list pseudo-methods?
Implementing list pseudo-methods can be dont using macros.
Here is an example fpr how to implement the desired pop from index pseudo method:
define <my_n_pop'exp> "<list'exp>[ ].[ ]pop_index[ ]\(<num'exp>\)" as {
evaluate typeof_item(<list'exp>) {
if(<list'exp>.size()> <num'exp>) {
value = <list'exp>[<num'exp>];
<list'exp>.delete(<num'exp>);
}else {
error("error : This list is has the size of ",<list'exp>.size(),"and you requested item",<num'exp>);
};
};
};
The usage from within the code will look something like this:
i=l.pop_index(2); // pop the item with index 2. All greater indices will decrease by 1.