Why deep_compare ignores fields which starts with "__"? - specman

If i have a struct with a field which starts with '__' deep_compare will ignore this field. Why?
Exmaple:
struct example {
a : uint;
__b : uint;
};
extend sys {
var item_1 : example = new;
var item_2 : example = new;
item_1.a = 5;
item_1.__b = 6;
item_2.a = 5;
item_2.__b = 7;
print deep_compare(item_1, item_2, 10); // Should return a list of 1
};

I believe that these fields are considered as Specman internal fields, I guess this is the reason it ignores it.

Related

Complex Dictionary Get Values as Strings With Conditions

I have built a survey using laanlabs/SwiftSurvey and I am able to get the results from the surveyComplete method as a dictionary.
The results return in a complex structure and I need to get the values for each response by the key tag of the questions array -> question object. This object contains an array of choices and within each choice object there is a key of selected. If the selected key's value is true (1) I need to get the text key's value that is in the same object. Some of these choices will have multiple selected keys with a value of true (1), if this is the case I'd like to concatenate the text key values with a comma in between the values.
The intention is then to insert the keys in to a SQLite database.
I am new to decoding dictionaries and traversing them in the correct way, I can access the dictionary print(dictionary) and also get into the the correct NSArray - print(dictionary["questions"] but from there I am stumped, could someone show me how please.
The results are below unfortunately its a large block apologies.
[
"version": 001,
"metadata":
{
"app_version" = "1.1";
build = 22;
debug = true;
},
"questions": <__NSArrayI 0x600000614d20>(
{
question = {
allowsMultipleSelection = 0;
choices = (
{
allowsCustomTextEntry = 0;
selected = 1;
text = Physician;
uuid = "224E1B76-D220-4068-AA22-6861E5F836CB";
},
{
allowsCustomTextEntry = 0;
selected = 0;
text = Dietitian;
uuid = "2DB2B6FB-E344-4BBF-A551-2FABE0DFF6AA";
},
{
allowsCustomTextEntry = 0;
selected = 0;
text = "Genetic Counsellor";
uuid = "A9BE7093-B95C-4BF4-B629-12FDA3154ABE";
},
{
allowsCustomTextEntry = 0;
selected = 0;
text = "Nurse/Nurse Practitioner/Physician Assistant";
uuid = "8E75A41B-0D8C-4ADA-A31C-2BC408F8269D";
},
{
allowsCustomTextEntry = 0;
selected = 0;
text = "Pharmacist / Pharmaceutical Industry";
uuid = "C943430D-EA48-4BCB-8ADF-011A223BDF36";
},
{
allowsCustomTextEntry = 0;
selected = 0;
text = "Academic/Researcher";
uuid = "E28377A4-37FC-4351-A857-88383A3D5A3B";
},
{
allowsCustomTextEntry = 0;
selected = 0;
text = "Patient/Patient Advocacy Group";
uuid = "E5836187-6C08-4272-A88E-40578F4FCF44";
},
{
allowsCustomTextEntry = 1;
selected = 0;
text = "Other (please specify)";
uuid = "EFF22342-48A9-4B8E-81A0-BB44D0E86EBC";
}
);
required = 1;
tag = "hcp-specialty";
title = "Please select the option that best describes your specialty:";
uuid = "7F77E248-8429-463E-9291-241B94BEE4F8";
};
type = 0;
},
{
question = {
autoAdvanceOnChoice = 1;
choices = (
{
allowsCustomTextEntry = 0;
selected = 1;
text = Yes;
uuid = "3C7A330D-F16B-4F3E-8ABC-6767A1A6332A";
},
{
allowsCustomTextEntry = 0;
selected = 0;
text = No;
uuid = "0E4F5360-FCCD-4860-9971-86E23BB8F6C1";
}
);
required = 1;
tag = "newborn-screening";
title = "Is newborn screening for classical homocystinuria available in your region/country?";
uuid = "F7C1A9D5-43AB-420D-80CF-F6644B95C73E";
};
type = 1;
},
{
question = {
feedback = "This is a free text response";
required = 1;
tag = "biggest-unmet-need";
title = "What do you believe is the biggest unmet need for patients living with classical HCU?";
uuid = "133E2EDC-8FF4-48D1-8BFA-3A20E5DA0052";
};
type = 4;
}
)
]
Based on their result example, you are getting JSON. Converting JSON to Dictionary is the easiest, of course, but also is the dirtiest. You are basically getting "whatever", so when it's time to use the data, you have to do a lot of - as you said - "decoding", validation, etc.
Instead of that, create a few Decodable structures that match your response. In this case you just need 3 structures:
struct Survey: Codable {
let questions: [Question]
}
struct Question: Codable {
let allowsMultipleSelection: Int?
let choices: [Choice]?
let required: Int
let tag: String
let title: String
let uuid: String
let feedback: String?
}
struct Choice: Codable {
let allowsCustomTextEntry: Int
let selected: Int
let text: String
let uuid: String
}
(I didn't verify every fields, you can adjust as needed. And you can omit any properties you don't need / don't care about.)
And then you decode it like this:
// Assume jsonData is your original JSON, which you currently decode as dictionary. So instead you do this:
let decoder = JSONDecoder()
let product = try decoder.decode(Survey.self, from: jsonData)
This approach allows you to
Most importantly, having a well-defined data makes working with database easier. You can even implement database encoder, based on your codable structures, which means you don't need to manually walk through columns of the database. Also when. you read from database, you get the same structures, no need to have 2 sets of rules / validations for database and dictionary you decoded.
This also allows you to be confident about data you decoded: it has proper names and types. You don't need to validate it (it was validated for you on decoding). You are in control which properties are required, which defaults to set, and so on. In more complicated cases, you may need to add manual decoding to your structures, but even then this manual decoding is inside the structure itself, easy to change / work with / test. All this instead of giant messy dictionary with "some stuff" in it.

Unable to Get Bitwise XOR to work in GameMaker 2

I'm trying to implement Zobrist hashing for a chess game and thought I was doing things correctly. However, when I try to apply XOR to a Hash value, it simply changes between 0 (the starting value for the hash) and -2147483648, in spite of conducting a bitwise XOR against a random number between 0 and 18446744073709551616.
Here's some code. I thought it was relatively simple with a struct for each entry in the Grid, a constructor function to make an entry, a function to create the Hash Table, and finally one for the Hash itself.
//Hash Table struct. This can be expanded for each new piece type
HashTableEntry = {
QueenHash:-1,
BishopHash:-1,
KnightHash:-1,
RookHash:-1,
PawnHash:-1,
KingHash:-1
};
//A function for creating a Hash Table Entry
function toHashTableEntryStruct() constructor {
var HashUpperBound = 18446744073709551616;
QueenHash = random(HashUpperBound);
BishopHash = random(HashUpperBound);
KnightHash = random(HashUpperBound);
RookHash = random(HashUpperBound);
PawnHash = random(HashUpperBound);
KingHash = random(HashUpperBound);
}
//A function to create a ds_grid Hash Table of the size passed to it, which it then returns
function CreateHashTable(_width, _height){
//Copy the local variables
var width = _width;
var height = _height;
//Create the grid for the Hash Table
var NewHashTable = ds_grid_create(width, height);
//Copy the original seed and set the game's seed to create the same table each time
var OriginalSeed = random_get_seed;
random_set_seed(280804);
//Loop through the Grid and fill each value with a HashTableEntry struct
for(var i = 0; i < width; i++;){
for(var j = 0; j < height; j++;){
var NewHash = new toHashTableEntryStruct();
ds_grid_set(NewHashTable, i, j, NewHash);
}
}
//Reset the seed
random_set_seed(OriginalSeed);
//Return the shiny new Hash Table
return NewHashTable;
}
//A function for creating the original Hash of a board. This should only be called on
initialising a Board as it sets the Original Hash
function CreateBoardHash(_board){
var Board = _board;
var width = ds_grid_width(Board);
var height = ds_grid_height(Board);
HashTable = CreateHashTable(width, height);
var FinalHash = 0;
for(var i = 0; i < width; i++;){
for(var j = 0; j < height; j++;){
var PieceToCheck = ds_grid_get(Board, i, j);
if(PieceToCheck == Pieces.BLANK){ continue; }
var HashEntry = ds_grid_get(HashTable, i, j);
var XorValue = MatchPieceToHashTableEntry(PieceToCheck, HashEntry);
FinalHash ^= XorValue;
}
}
if(FinalHash == 0){
//Error here
return;
}
//Set the instance variable for this original Hash
OriginalHash = FinalHash;
//Add it to the Current Hash stack
CurrentHash = FinalHash;
//Return said hash for the calling function
return FinalHash;
}
The problem comes in the FinalHash ^= XorValue; line, where XorValue each time is a random number, but FinalHash only ever comes out as 0 or -2147483648.
Can someone help with why this is the case and what I may be doing wrong with my XOR function?
Thanks to Reddit, I have fixed this. GameMaker must be doing something weird with the ^= operator, as the solution was as follows:
FinalHash = FinalHash ^ XorValue;

Closures over not yet defined variables in CoffeeScript

For the following code:
inc = -> value = (value ? 0) + 1
dec = -> value = (value ? 0) - 1
print = -> console.log value ? 0
How can you make this work properly, so inc and dec close upon value instead of creating separate function-local variables, in the way other than explicitly assigning something to value?
In plain Javascript, you would just declare var value at outer scope:
var value;
function inc() { value = (value || 0) + 1; };
function dec() { value = (value || 0) - 1; };
function print() { console.log(value || 0); };
What is CoffeeScript way for exactly the same thing?
In CoffeeScript, the way to introduce a local variable is to assign to the variable in the appropriate scope.
This is simply the way that CoffeeScript was defined and as such is similar to Python or Ruby, which do not require a "variable declaration", except CoffeeScript also allows forward access. A side-effect is that one cannot shadow a lexical variable.
Just as with the placement of var in JavaScript, where this assignment is done (as long as it is in the correct scope) does not affect the scope of the variable.
Given
x = undefined
f = -> x
// JS
var f, x;
x = void 0;
f = function() {
return x;
};
Given
f = -> x
x = undefined
// JS
var f, x;
f = function() {
return x;
};
x = void 0;

Weird value from NSDictionary

I got a NSDictionary that when I do a [currentOrder debugDescription] call on it the layout is below, when I then do a:
[currentOrder valueForKey: #"itemOrder"]
It return it funny with the brackets as:
(
"4 X ESPRESSO"
)
where it should only be (without brackets):
4 X EXPRESSO
Any idea why?
Content of currentOrder:
currentOrder: <__NSArrayM 0x68426c0>(
{
extra1Select = 0;
extra2Select = 0;
extra3Select = 0;
itemCost = 58;
itemOrder = "4 X ESPRESSO";
itemOrderDescription = "Cookie: YES, Sugar: YES";
itemQuantity = 4;
itemRestaurant = VidaECaffe;
plistItem = {
cost = "11.5";
description = "R11.50";
extra1 = {
desc = Cookie;
details = (
{
cost = "3.00";
option1 = YES;
},
{
cost = "0.00";
option2 = NO;
}
);
};
extra2 = {
desc = Sugar;
details = (
{
cost = "0.00";
option1 = YES;
},
{
cost = "0.00";
option2 = NO;
}
);
};
itemRestaurant = VidaECaffe;
level = 1;
title = ESPRESSO;
};
}
)
The problem is that currentOrder is not a dictionary but a NSArray containing a dictionary. The failure you are making next is that you use valueForKey: which is part of the key value coding family and not the designated access method for dictionaries (which is objectForKey:), and the array returns you a filtered array as a result...
When you print out the value of an array or dictionary in the debugger, the debugger includes the extra brackets, braces, and whatnot so you can see the layout of the structure you are asking about.
If the debugger shows the contents of an array something like
(
"Foobar"
)
it's telling you that the array has one element, a string with the value Foobar.
Put entirely another way: Read up on what the debugger does when it prints out values. Lots of resources here and on the web for this. For example: Debugging with GDB: Introduction to Commands, Print and Print-Object

After JSON data is returned, I can't access it using javascript code

I have a WCF RESTful service that returns a complex object with the following format:
{"GetMatterSearchRESTResult":{"BreakDownBypracticeGroups":[],"BreakDownByCountry":[],"TotalMatterCount":0}}.
When I have this value return, I try to access it using the following code below:
if (executor.get_responseAvailable) {
var serviceResults = executor.get_responseData();
var prasevalues = eval('(' + serviceResults + ')');
var mattersResults = prasevalues.GetMatterSearchRESTResult;
for (var a = 0; a < mattersResults.length; a++) {
alert(mattersResults[a].TotalMatterCount);
var pgBreakDown = mattersResults[a].BreakDownBypracticeGroups;
for (var b = 0; b < pgBreakDown.length; b++) {
alert(pgBreakDown[b].DepartmentName + " " + pgBreakDown[b].Count);
}
}
}
return false;
After the eval function called, i get an undefined value. Please help.
GetMatterSearchRESTResult isn't an array, it's an object, so matterResults won't have a length method. Make GetMatterSearchRESTResult an array of objects:
{"GetMatterSearchRESTResult":[{"BreakDownBypracticeGroups":[],"BreakDownByCountry":[],"TotalMatterCount":0}]}
Edit: Or, if that's not possible, you don't need to loop through the object