Specman macro to do set subtraction with int_range_list objects - specman

I work with a bunch of sets in order to generate constrained random traffic, but I want to be able to call a Specman macro that computes the complement of a set with syntax like:
COMPLEMENT begin
domain=[0..10,24..30],
complementing_set=[2..3,27..30]
end
and have it generate:
[0..1,4..10,24..26]
Every time I need the complement of a set I'm using fully populated lists (e.g. {0;1;2;3....} ) and then removing elements, instead of using Specman's built-in int_range_list object. And I'm also doing a lot of these set calculations at run-time instead of compile-time.

You can try this:
var domain: list of int = {0..10, 24..30};
var complementing_set: list of int = {2..3, 27..30};
var complement: list of int = domain.all(it in complementing set);
The all pseudo-method generates a sublist of the parent list of all the elements in the parent list for which the condition in the parentheses holds.

In the recent versions of Specman, you can use the pre-defined set type, that serves exactly this purpose. For example, you can do things like this:
var s1: set = [1..5, 10..15];
var s2: set = [4..13];
var s3: set = s1.intersect(s2);
and even like this:
x: int;
y: int;
........
var s1: set = [x..y];
var s2: set = [1..10];
var s3: set = s1.union(s2);
etc.

one more way may be to use uints, say you have a 500 possible values:
domain : uint(bits:500);
complement : uint(bits:500);
set : uint(bits:500) = domain & ~complement;
you can later extract the indices with
set_l : list of uint = set[.]].all_indices(it==1);
depending on your domain to possible values ratio this method may be quicker to calculate

Related

Change multiple subsequent variables in swift

I was making a shop but to show which background is selected I stumbled into a weird hick up. Is there a way to change multiple variables in a loop so that for example:
var achtergrondSelected1:Int = 1
var achtergrondSelected2:Int = 0
var achtergrondSelected3:Int = 0
var achtergrondSelected4:Int = 0
var achtergrondSelected5:Int = 0
var achtergrondSelected6:Int = 0
(Instead of a boolean my old code used 1 and 0, same effect)
Let's say the person selects background (= achtergrond) 4, at the moment I'm changing all values manually, but is there a way to create a loop that uses the number in the name to change the values to 0 who need to be 0 and 1 who need to be one?
You can avoid to declare N variables and loops, you can just use an enum.
enum achtergrondSelected : Int
{
case achterground1
case achterground2
case achterground3
case achterground4
case achterground5
}
var selectedBackground : achtergrondSelected
selectedBackground = .achterground1
print(selectedBackground.rawValue)
For cases name I choose "achterground" like it was in your example, but you can obviously choose other names.
I hope that's what you was looking for.

What are tuples and lists?

I was watching Stanford - Developing iOS 9 Apps with Swift - 3. More Swift and Foundation Framework on youtube was talking about tuples and lists and i'm not sure what they are even if i searched online i still can't understand what they are.
Turple is basically a group of variables, while a list is an index of a few values of the same type.
Tuple
Tuples are really cool (I think). It lets you basically put a bunch of variables together and put a name to them or nothing.
For example, let's say you have the quadratic formula. It would make sense to group these values:
var quadraticFunction: (a: Int, b: Int, c: Int, plus: Double, minus: Double)
With this you can even return the tuple. To take this further, you might want to make a function that performs the quadratic formula by taking 1 number and adding 1 for b and 2 for c.
func performQuadraticFormula(startValue: Int) -> (a: Int, b: Int, c: Int, plus: Double, minus: Double) {
var returnQuadratic: (a: Int, b: Int, c: Int, plus: Double, minus: Double)
returnQuadratic.a = startValue
returnQuadratic.b = startValue + 1
returnQuadratic.c = startValue + 2
returnQuadratic.plus = /* lots of math for this part */
returnQuadratic.minus = /* lots of math for this part */
return returnQuadratic
}
So now you can take this output and actually store it for use later:
let function = performQuadraticFormula(startValue: 10)
print(function.a)
// you can do whatever you want with this output now
So you can assign names to these values, store them for later, assign them, return them in a function. Also, you don't have to have a name for each value, in which case it would be like this:
function.0
Lists
I guess I can see how you can easily get confused with a list and a tuple. Both can be referenced with the index of it (tuple.0, list[0]). But lists are pretty different. First off, you cannot assign names for each value. The real big difference though, is a tuple is predefined with its values while a list can expand and remove items easily.
For example, you can store a few tests grades in a list
var tests = [100, 100, 90, 78, 100, 10]
Then, next week go and add a new one and it will automatically expand:
tests.append(99.8)
A tuple migght only have 3 values and that's it; you would have to go back and add a new variable in order to add a new value. For a list you have a bunch of the same type and can add whatever you want. To get one you can do this:
let firstTest = tests[0]
Also, a list (Array) has many functions that come with it like .map(), filter(), etc. Tuple does not have that.
Think of a list of a group of statistics or data and a tuple as just a way to put a bunch of variables on one line
A tuple is a group of zero or more values represented as one value.
For example ("John", "Smith") holds the first and last name of a person. You can access the inner values using the dot(.) notation followed by the index of the value:
var person = ("John", "Smith")
var firstName = person.0 // John
var lastName = person.1 // Smith
An array literal is written as a list of values, separated by commas, surrounded by a pair of square brackets:
The example below creates an array called shoppingList to store String values:
var shoppingList: [String] = ["Eggs", "Milk"]
// shoppingList has been initialized with two initial items
var firstItem = shoppingList[0]
// firstItem is equal to "Eggs"

How to make a simple division of Double by Int in Swift?

Looking at various posts on this topic but still no luck. Is there a simple way to make division/conversion when dividing Double (or Float) with Int? Here is a simple example in playground returning and error "Double is not convertible to UInt8".
var score:Double = 3.00
var length:Int = 2 // it is taken from some an array lenght and does not return decimal or float
var result:Double = (score / length )
Cast the int to double with var result:Double=(score/Double(length))
What this will do is before computing the division it will create a new Double variable with int inside parentheses hence constructor like syntax.
You cannot combine or use different variable types together.
You need to convert them all to the same type, to be able to divide them together.
The easiest way I see to make that happen, would be to make the Int a Double.
You can do that quite simply do that by adding a ".0" on the end of the Integer you want to convert.
Also, FYI:
Floats are pretty rarely used, so unless you're using them for something specific, its also just more fluid to use more common variables.

Specman coverage: Is there a way to define ranges using variable?

I have comp_value that gets values between 1 .. 100.
In addition I have an input variable period (of the same range). I need to cover 2 ranges of comp_values: [1..period] and [period+1 .. 100]. Something like this:
cover some_event_e is {
item period using no_collect;
item comp_val using no_collect,
ranges = {
range([1..period], "Smaller_than_period");
range([period+1..100], "Bigger_than_period");
};
};
(The code causes compilation error since no variable can be written inside range).
Is there a way to collect the coverage?
Thank you for your help.
Ranges must be constant.
But if I understood your intent correctly, you can define new items like
cover some_event_e is {
item smaller_or_equal_than_period: bool = (comp_val in [1..period]) using
ignore = (not smaller_or_equal_than_period);
item greater_than_period: bool = (comp_val in [(min(100,period+1)..100]) using
ignore = (not greater_than_period);
};
Assuming period is always in [1..100].

How exactly does the "let" keyword work in Swift?

I've read this simple explanation in the guide:
The value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once.
But I want a little more detail than this. If the constant references an object, can I still modify its properties? If it references a collection, can I add or remove elements from it? I come from a C# background; is it similar to how readonly works (apart from being able to use it in method bodies), and if it's not, how is it different?
let is a little bit like a const pointer in C. If you reference an object with a let, you can change the object's properties or call methods on it, but you cannot assign a different object to that identifier.
let also has implications for collections and non-object types. If you reference a struct with a let, you cannot change its properties or call any of its mutating func methods.
Using let/var with collections works much like mutable/immutable Foundation collections: If you assign an array to a let, you can't change its contents. If you reference a dictionary with let, you can't add/remove key/value pairs or assign a new value for a key — it's truly immutable. If you want to assign to subscripts in, append to, or otherwise mutate an array or dictionary, you must declare it with var.
(Prior to Xcode 6 beta 3, Swift arrays had a weird mix of value and reference semantics, and were partially mutable when assigned to a let -- that's gone now.)
It's best to think of let in terms of Static Single Assignment (SSA) -- every SSA variable is assigned to exactly once. In functional languages like lisp you don't (normally) use an assignment operator -- names are bound to a value exactly once. For example, the names y and z below are bound to a value exactly once (per invocation):
func pow(x: Float, n : Int) -> Float {
if n == 0 {return 1}
if n == 1 {return x}
let y = pow(x, n/2)
let z = y*y
if n & 1 == 0 {
return z
}
return z*x
}
This lends itself to more correct code since it enforces invariance and is side-effect free.
Here is how an imperative-style programmer might compute the first 6 powers of 5:
var powersOfFive = Int[]()
for n in [1, 2, 3, 4, 5, 6] {
var n2 = n*n
powersOfFive += n2*n2*n
}
Obviously n2 is is a loop invariant so we could use let instead:
var powersOfFive = Int[]()
for n in [1, 2, 3, 4, 5, 6] {
let n2 = n*n
powersOfFive += n2*n2*n
}
But a truly functional programmer would avoid all the side-effects and mutations:
let powersOfFive = [1, 2, 3, 4, 5, 6].map(
{(num: Int) -> Int in
let num2 = num*num
return num2*num2*num})
Let
Swift uses two basic techniques to store values for a programmer to access by using a name: let and var. Use let if you're never going to change the value associated with that name. Use var if you expect for that name to refer to a changing set of values.
let a = 5 // This is now a constant. "a" can never be changed.
var b = 2 // This is now a variable. Change "b" when you like.
The value that a constant refers to can never be changed, however the thing that a constant refers to can change if it is an instance of a class.
let a = 5
let b = someClass()
a = 6 // Nope.
b = someOtherClass() // Nope.
b.setCookies( newNumberOfCookies: 5 ) // Ok, sure.
Let and Collections
When you assign an array to a constant, elements can no longer be added or removed from that array. However, the value of any of that array's elements may still be changed.
let a = [1, 2, 3]
a.append(4) // This is NOT OK. You may not add a new value.
a[0] = 0 // This is OK. You can change an existing value.
A dictionary assigned to a constant can not be changed in any way.
let a = [1: "Awesome", 2: "Not Awesome"]
a[3] = "Bogus" // This is NOT OK. You may not add new key:value pairs.
a[1] = "Totally Awesome" // This is NOT OK. You may not change a value.
That is my understanding of this topic. Please correct me where needed. Excuse me if the question is already answered, I am doing this in part to help myself learn.
First of all, "The let keyword defines a constant" is confusing for beginners who are coming from C# background (like me). After reading many Stack Overflow answers, I came to the conclusion that
Actually, in swift there is no concept of constant
A constant is an expression that is resolved at compilation time. For both C# and Java, constants must be assigned during declaration:
public const double pi = 3.1416; // C#
public static final double pi = 3.1416 // Java
Apple doc ( defining constant using "let" ):
The value of a constant doesn’t need to be known at compile time, but you must assign the value exactly once.
In C# terms, you can think of "let" as "readonly" variable
Swift "let" == C# "readonly"
F# users will feel right at home with Swift's let keyword. :)
In C# terms, you can think of "let" as "readonly var", if that construct was allowed, i.e.: an identifier that can only be bound at the point of declaration.
Swift properties:
Swift Properties official documentation
In its simplest form, a stored property is a constant or variable that is stored as part of an instance of a particular class or structure. Stored properties can be either variable stored properties (introduced by the varkeyword) or constant stored properties (introduced by the let keyword).
Example:
The example below defines a structure called FixedLengthRange, which describes a range of integers whose range length cannot be changed once it is created:
struct FixedLengthRange {
var firstValue: Int
let length: Int
}
Instances of FixedLengthRange have a variable stored property called firstValue and a constant stored property called length. In the example above, length is initialized when the new range is created and cannot be changed thereafter, because it is a constant property.