I've made a controls room for custom keymapping. I had an idea - if key is assigned to more than 1 control, then it shows up red. But it works only partially.
Spawn code:
with(instance_create(64,64,obj_button_key)) {
mytext="UP: ";
myKEY=global.keyUP;
mytype=1;
}
with(...
scr_keymap_conflict(argument0):
var ii;
ii = 0;
if (argument0 == global.keyUP) ii+=1;
if (argument0 == global.keyDOWN) ii+=1;
if (argument0 == global.keyLEFT) ii+=1;
if (argument0 == global.keyRIGHT) ii+=1;
if (argument0 == global.keySPRINT) ii+=1;
if (argument0 == global.keyCROUCH) ii+=1;
if (argument0 == global.keyGRENADE) ii+=1;
if (argument0 == global.keyACTION) ii+=1;
if (argument0 == global.keyCHAT) ii+=1;
if (argument0 == global.keyMELEE) ii+=1;
if (argument0 == global.keyDROP) ii+=1;
if (ii > 1) {
return true;
}
Draw:
if (active) {draw_set_color(c_yellow)}
else if (scr_keymap_conflict(myKEY)) {draw_set_color(c_red)}
else draw_set_color(c_gray);
...
It seems like there's a problem with scr_keymap_conflict(argument0) giving invalid info, so some buttons turn red, but some don't, an example, if there are two vk_space controls, then the first one will become red, but the second won't (I have a feeling that draw_set_color is overwriting separate objects at random moments). Anyway, global.key... hold ASCII real values (keyboard shortcuts). active and mytype are not important in this case. Anyone got any idea how to fix this?
Your script is only returning a value in case of ii being equal or more than 1. When GM ends a script without a return value (or simply calling "return") the value returned is just random, it might be "true" or "false", or "hello world" string.
(typically it is the previous function's result).
This is a first bug, but when you fix it you'll notice keys will never turn red.. This is because semantically you're checking if a key is binded to two "functions". NOT if two "functions" are binded to 2 keys.
You would need a different system for that, easiest thing I can think of is to use a controller (code below isn't tested, just to show the idea):
create event controlling object
keyUP = ds_list_create();
keyDown = ds_list_create();
keyLEFT = ds_list_create();
keyRIGHT = ds_list_create();
keySPRINT = ds_list_create();
keyCROUCH = ds_list_create();
keyGRENADE = ds_list_create();
keyACTION = ds_list_create();
keyCHAT = ds_list_create();
keyMELEE = ds_list_create();
keyDROP = ds_list_create();
step event controlling object
ds_list_clear(keyUP);
ds_list_clear(keyDOWN);
//... etc for all keys
with (objKeys) {
ds_list_add(myKEY, id);
}
spawn code of objKeys
with(instance_create(64,64,obj_button_key)) {
mytext="UP: ";
myKEY=objKeyController.keyUP;
//notice that it refers to the controller's list
mytype=1;
}
draw event
if (active) {draw_set_color(c_yellow)}
else if (ds_list_size(myKEY) > 1) {draw_set_color(c_red)}
else draw_set_color(c_gray);
...
The controller's step event can probably be removed if you add/remove the keys to/from the lists on assignment (user input). That way the lists don't have to rebuild each step.
Also an obvious improvement is to stash all those "keys" inside a map instead of simple variables.
This method has O(n) time instead of O(n^2) you would get when using a function instance_variable(obj,var)
Related
In logic app i have one myarray variable with values ['1','2','3']
In the Foreach connector it will come one value a a time and want to check which value it is and based on that set a value to another variable.
in c# it will be like -
foreach (item in myarray)
{
if (item == 1)
{
set variable_data = 1
}
else if (item == 2)
{
set variable_data = 2
}
else if (item == 3)
{
set variable_data = 3
}
in logic app i can able to do it like this -
is there any better way to do it in logic app?
You can use switch action to implement this requirement.
This is a question not about how LongAdder works, it's about an intriguing implementation detail that I can't figure out.
Here is the code from Striped64 (I've cut out some parts and left the relevant parts for the question):
final void longAccumulate(long x, LongBinaryOperator fn,
boolean wasUncontended) {
int h;
if ((h = getProbe()) == 0) {
ThreadLocalRandom.current(); // force initialization
h = getProbe();
wasUncontended = true;
}
boolean collide = false; // True if last slot nonempty
for (;;) {
Cell[] as; Cell a; int n; long v;
if ((as = cells) != null && (n = as.length) > 0) {
if ((a = as[(n - 1) & h]) == null) {
//logic to insert the Cell in the array
}
// CAS already known to fail
else if (!wasUncontended) {
wasUncontended = true; // Continue after rehash
}
else if (a.cas(v = a.value, ((fn == null) ? v + x : fn.applyAsLong(v, x)))){
break;
}
A lot of things from code are clear to me, except for the :
// CAS already known to fail
else if (!wasUncontended) {
wasUncontended = true; // Continue after rehash
}
Where does this certainty that the following CAS will fail?
This is really confusing for me at least, because this check only makes sense for a single case : when some Thread enters the longAccumulate method for the n-th time (n > 1) and the busy spin is at it's first cycle.
It's like this code is saying : if you (some Thread) have been here before and you have some contention on a particular Cell slot, don't try to CAS your value to the already existing one, but instead rehash the probe.
I honestly hope I will make some sense for someone.
It's not that it will fail, it's more that it has failed. The call to this method is done by the LongAdder add method.
public void add(long x) {
Cell[] as; long b, v; int m; Cell a;
if ((as = cells) != null || !casBase(b = base, b + x)) {
boolean uncontended = true;
if (as == null || (m = as.length - 1) < 0 ||
(a = as[getProbe() & m]) == null ||
!(uncontended = a.cas(v = a.value, v + x)))
longAccumulate(x, null, uncontended);
}
}
The first set of conditionals is related to existence of the long Cells. If the necessary cell doesn't exist, then it will try to accumulate uncontended (as there was no attempt to add) by atomically adding the necessary cell and then adding.
If the cell does exist, try to add (v + x). If the add failed then there was some form of contention, in that case try to do the accumulating optimistically/atomically (spin until successful)
So why does it have
wasUncontended = true; // Continue after rehash
My best guess is that with heavy contention, it will try to give the running thread time to catch up and will force a retry of the existing cells.
I am having a problem in the question SERVICES on SPOJ. I tried to solve it and came up with the following DP states [posofA][posofB][posofC][NextToMove]. But looking at the constraints, I think It will Give MLE. After trying for a day, I googled it and found blogs regarding a symmetry in the question. Despite my best efforts, I am unable to understand it. Can someone Please help and spare his time to help me. Thanks.
Observe that you can drop posOfC and always denote posOfC by the last requested position. When you are processing a request , you can easily get the previous position. Now you have all the positions of the 3 partners. Send one of them to the new requested position checking that all of them will be in different location.
int f(int pos,int a,int b)
{
if(pos == req.sz)
return 0;
// last position
int c = req[pos-1];
// current position we are sending one of them
int to = req[pos];
if( dp[pos][a][b] != -1)
return dp[pos][a][b];
int ans = inf;
// a goes to current request position
if(b != c && b != to && c != to)
ans = min(ans,f(pos+1,b,c) + cost[a][to]);
// b goes to current request position
if(a != c && a != to && c != to)
ans = min(ans,f(pos+1,a,c) + cost[b][to]);
// c goes to current request position
if(a != b && a != to && b != to)
ans = min(ans , f(pos+1,a,b) + cost[c][to]);
return dp[pos][a][b] = ans;
}
First 3 elements of req will be 1,2,3 . Get the answer by calling f(3,1,2).
Reverse characters of each word in a sentence. For eg:
My name is alex
changes to
yM eman si xela
I thought of the normal O(n) time algorithm of using two pointer to point to either end of the word and reverse it.
But in the below site
http://www.businessinsider.com/8-mind-bending-interview-questions-that-google-asks-its-engineers-2012-7?op=1
(Refer to ans of ques 2)
it is give that converting it to linked list and repetitively applying reversal of linked list for individual word is better. I found the following solution for the same program on Hackerearth:
http://learn.hackerearth.com/question/317/reverse-characters-of-each-word-in-a-sentence/
This solution takes O(n) time and O(n) space. The solution I suggested takes O(n) time O(1) space. How is the second one better?
Following is the code from Hackerearth:
public node stringReverseChars(node ll){
if(ll == null || ll.next == null)
return ll;
node tmp = ll;
node head = null, prev = null;
while(tmp != null){
while(tmp != null && tmp.data == ' '){
if(head == null)
head = tmp;
prev = tmp;
tmp = tmp.next;
}
if(tmp == null)
break;
node curr = tmp;
while(tmp.next != null && tmp.next.data != ' '){
tmp = tmp.next;
}
node np = tmp.next;
tmp.next = null;
node rev = reverseLL(curr);
if(prev != null)
prev.next = rev;
prev = curr;
curr.next = np;
if(head == null)
head = rev;
tmp = np;
}
return head;
}
I'm pretty skeptical that those other approaches are better. They have worse memory usage (Θ(n) versus O(1)) and worse locality of reference (they use linked lists rather than arrays). I don't see anything wrong with your solution; in fact, I think it's the standard way to do this.
Hope this helps!
I don't understand exactly how to use a function that returns a boolean. I know what it is, but I can't figure out how to make it work in my program. I'm trying to say that if my variable "selection" is any letter beween 'A' and 'I' then it is valid and can continue on to the next function which is called calcExchangeAmt(amtExchanged, selection). If it is false I want it to ask the user if they want to repeat the program and if they agree to repeat. I want it to clear the screen and restart to the main function. How do I make my program work as intended?
This is my bool function:
bool isSelectionValid(char selection, char yesNo, double amtExchanged)
{
bool validData;
validData = true;
if ((selection >= 'a' && selection <= 'i') ||
(selection >= 'A' && selection <= 'I'))
{
validData = calcExchangeAmt (amtExchanged, selection);
}
else(validData == false);
{
cout << "Do you wish to continue? (Y for Yes / N for No)";
cin >> yesNo;
}
do
{
main();
}
while ((yesNo =='y')||(yesNo == 'Y'));
{
system("cls");
}
return 0;
}
I get this warning:
warning C4800: 'double' : forcing value to bool 'true' or 'false' (performance warning)
A bool function should return true or false. I'm guessing your warning is caused by the fact that you're declaring validData as bool, but then assign it a different value (returned by calcExchangeAmt function). That value is getting converted from its value type (double) to boolean (true or false).
So, your IsSelectionValid method should just return true if selection is valid, or false if it's not. Then whatever code needs to know that information can proceed accordingly.
I don't know much C++, so forgive me for syntax problems my code is bound to have, but your code should look something like this:
bool isSelectionValid(char selection)
{
return (selection >= 'a' && selection <= 'i') || (selection >= 'A' && selection <= 'I');
}
void myCallingFunction(double amtExchanged, char selection)
{
bool isSelectionValid = isSelectionValid(selection);
if(isSelectionValid)
{
double exchangeAmt = calcExchangeAmt (amtExchanged, selection);
}
else
{
cout<<"Do you wish to continue? (Y for Yes / N for No)";
cin>>yesNo;
if((yesNo =='y')||(yesNo == 'Y'))
{
main(); // or whatever code starts another attempt
}
}
This code is seriously confusing and very non-C++ like. We normally expect main() to be the function that drives things and calls other functions, not to have it called from some other place. We generally avoid do unless there is a compelling reason (and I don't see one here). I think it's highly unlikely that a function called calcExchangeAmt returns true or false; I suspect it actually returns a number that you should be doing something else with (showing to the user?).
With all this going on, trying to explain your actual compiler error messages is of limited value. Your code is all inside out and backwards. Anna Lear's answer seems like a better starting point if it makes sense to you.
The type of 0 is not bool; true or false is bool. It is telling you that 0 is a double, but it is forcing it to a boolean type.