STL queue function: empty and size - stl

Stl queue is used in our large program, with one line
if( q_block.empty() )
It returns true while it shouldn't.
And I change this code to
if( q_block.size() == 0 )
It returns false as it supposed to.
Reading the STL source code, I found that this two function quite the same. But I don't know why it behaves differently in this situation.
Can anyone give me some idea?

Related

attempt to index global 'INV' (a nil value) (line:23)

print("cl_inv wurde geladen!")
concommand.Add("inv_model", function(ply)
local wep = ply:GetActiveWeapon()
if not IsValid(wep) then return end
print(wep:GetWeaponWorldModel())
end)
net.Receive("inv_init", function()
LocalPlayer().inv = {}
end)
net.Receive("inv_give", function()
local classname = net.ReadString()
table.insert(LocalPlayer().inv, classname)
end)
function INV.Open()
local ply = LocalPlayer()
local plyinv = ply.inv
if not ply.inv then return end
local scrw, scrh = ScrW(),ScrH()
INV.Menu = vgui.Create("DFrame")
local inv = INV.Menu
inv:SetSize(scrw * .4, scrh * .6)
inv:Center()
inv:SetTitle("")
inv:MakePopup()
inv.Paint = function(me,w,h)
surface.SetDrawColor(0,0,0,200)
surface.DrawRect(0,0,w,h)
end
local scroll = inv:Add("DScrollPanel")
scroll:Dock(FILL)
scroll.panels = {}
for k,v in pairs(plyinv) do
local itemPanel = scroll:Add("DPanel")
itemPanel:Dock(TOP)
scroll.panels[itemPanel] = true
end
end
hook.Add( "OnPlayerChat", "InvOpen", function( ply, strText, bTeam, bDead )
if ( ply != LocalPlayer() ) then return end
if string.lower(strText) != "!inv" then return end
INV.Open()
end )
I already searched the whole internet for a solution, i did found something but it didnt really helped me so what im expecting is that someone may be so nice and can help me solve my problem. In line 23 is the error :attempt to index global 'INV' (a nil value) (line:23)
As I can see, you're using the Garry's Mod Lua API. Although this API adds some features to the basic Lua language (e.g. ! operator for negation, C-style comments //), you still need to use the Lua language properly.
What the error says, is that you're trying to access to a table that isn't defined in the current scope of your program.
I can bet your addon defined some global table to interoperate between files and other addons installed on your machine.
As I don't have that much information on the way you're loading your file, here are multiple guesses on possible solutions:
For a file located inside an autorun folder
If the snippet you gave is under an autorun folder, the issue may be that the INV table does not yet exist when you load your file. To correct that, you can use the GLua Timer's Library.
Here is a simple snippet:
Timer.Simple(0.2, function()
-- put your code here
end
Timer.Simple takes two parameters: first one is a delay, in seconds and the other one is a callback function, executed once the delay has ended !
If you're doing something that requires the server to have loaded some other addons before actually running your script, this might be helpfull.
Accessing Global variables of your environment
As you did not gave that many informations about your problem I have to continue to guess what you're trying to do. An other source of problem is maybe that the INV table simply doesn't exist.
How to get all defined global tables in Lua ?
In Lua, there is the _G table that contains all the data of your current environnement. For more in-depth information, please see the official Lua documentation.
To know if your table does in fact exist in your environment, you can run this snippet on your Lua server:
local function doesINVExist()
for name in pairs(_G) do
if (name == "INV") then return true end
end
return false
end
And then you can simply run it using:
print(doesINVExist())
What to do if the table doesn't exist
If INV isn't present in your environment, then it may be because your Garry's Mod server did not loaded any file that defines such a table. Then what you can do is checking the addon that should dclare that table to see if there are any errors that might make it a nil value.
I hope this can help. Of course my answer could've been much more detailed if I had more information. But as I can't write comments (my rating isn't high enough), here is all I can do for you !
Have a great day,
Pedro.

sympy autowrap (cython): limit of # of arguments, arguments in array form?

I have the following issue:
I want to use autowrap to generate a compiled version of a sympy matrix, with cells containing sympy expressions. Depending on the specification of my problem, the number of arguments can get very large.
I ran into the following 2 issues:
The number of arguments that autowrap accepts seems to be limited to 509.
i.e., this works:
import sympy
from sympy.utilities.autowrap import autowrap
x = sympy.symbols("x:509")
exp = sum(x)
cyt = autowrap(exp, backend="cython", args=x)
and this fails to compile:
x = sympy.symbols("x:510")
exp = sum(x)
cyt = autowrap(exp, backend="cython", args=x)
The message I get seems not very telling:
[...] (Full output upon request)
Generating code
c:\users\[classified]\appdata\local\temp\tmp2zer8vfe_sympy_compile\wrapper_module_17.c(6293) : fatal error C1001: An internal error has occurred in the compiler.
(compiler file 'f:\dd\vctools\compiler\utc\src\p2\hash.c', line 884)
To work around this problem, try simplifying or changing the program near the locations listed above.
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
LINK : fatal error LNK1257: code generation failed
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\link.exe' failed with exit status 1257
Is there any way around this? I would like to use versions of my program that need ~1000 input variables.
(I have no understanding of C/cython. Is this an autowrap limitation, a C limitation ...?)
Partly connected to the above:
Can one compile functions that accept the arguments as array.
Is there any way to generate code that accepts a numpy array as input? I specifically mean one array for all the arguments, instead of providing the arguments as list. (Similar to lambdify using a DeferredVector). ufuncify supports array input, but as I understand only for broadcasting/vectorizing the function.
I would hope that an array as argument could circumvent the first problem above, which is most pressing for me. Apart from that, I would prefer array input anyways, both because it seems faster (no need to unpack the numpy array I have as input into a list), and also more straightforward and natural.
Does anyone have any suggestions what I can do?
Also, could anyone tell me whether f2py has similar limitations? This would also be an option for me if feasible, but I don't have it set up to work currently, and would prefer to know whether it helps at all before investing the time.
Thanks!
Edit:
I played around a bit with the different candidates for telling autowrap that the input argument will be something in array form, rather than a list of numbers. I'll document my steps here for posterity, and also to increase chances to get some input:
sympy.DeferredVector
Is what I use with lambdify for the same purpose, so I thought to give it a try. However, warning:
A = sympy.DeferredVector("A")
expression = A[0]+A[1]
cyt = autowrap(expression, backend="cython", args=A)
just completely crashed my OS - last statement started executing, (no feedback), everything got really slow, then no more reactions. (Can only speculate, perhaps it has to do with the fact that A has no shape information, which does not seem to bother lambdify, but might be a problem here. Anyways, seems not the right way to go.)
All sorts of array-type objects filled with the symbols in the expression to be wrapped.
e.g.
x0 ,x1 = sympy.symbols("x:2")
expression = x0 + x1
cyt = autowrap(expression, backend="cython", args=np.array([x0,x1]))
Still wants unpacked arguments. Replacing the last row by
cyt = autowrap(expression, backend="cython", args=[np.array([x0,x1])])
Gives the message
CodeGenArgumentListError: ("Argument list didn't specify: x0, x1 ", [InputArgument(x0), InputArgument(x1)])
Which is a recurrent theme to this approach: also happens when using a sympy matrix, a tuple, and so on inside the arguments list.
sympy.IndexedBase
This is actually used in the autowrap examples; however, in a (to me) inintuitive way, using an equation as the expression to be wrapped. Also, the way it is used there seems not really feasible to me: The expression I want to cythonize is a matrix, but its cells are themselves longish expressions, which I cannot obtain via index operations.
The upside is that I got a minimal example to work:
X = sympy.IndexedBase("X",shape=(1,1))
expression = 2*X[0,0]
cyt = autowrap(expression, backend="cython", args=[X])
actually compiles, and the resulting function correctly evaluates - when passed a 2d-np.array.
So this seems the most promising avenue, even though further extensions to this approach I keep trying fail.
For example this
X = sympy.IndexedBase("X",shape=(1,))
expression = 2*X[0]
cyt = autowrap(expression, backend="cython", args=[X])
gets me
[...]\site-packages\sympy\printing\codeprinter.py", line 258, in _get_expression_indices " rhs indices in %s" % expr)
ValueError: lhs indices must match non-dummy rhs indices in 2*X[0]
even though I don't see how it should be different from the working one above.
Same error message when sticking to two dimensions, but increasing the size of X:
X = sympy.IndexedBase("X",shape=(2,2))
expression = 2*X[0,0]+X[0,1]+X[1,0]+X[1,1]
cyt = autowrap(expression, backend="cython", args=[X])
ValueError: lhs indices must match non-dummy rhs indices in 2*X[0, 0] + X[0, 1] + X[1, 0] + X[1, 1]
I tried snooping around the code for autowrap, but I feel a bit lost there...
So I'm still searching for a solution and happy for any input.
Passing the argument as an array seems to work OK
x = sympy.MatrixSymbol('x', 520, 1)
exp = 0
for i in range(x.shape[0]):
exp += x[i]
cyt = autowrap(exp, backend='cython')
arr = np.random.randn(520, 1)
cyt(arr)
Out[48]: -42.59735861021934
arr.sum()
Out[49]: -42.597358610219345

ref keyword with C++/CLI code interface

I'm writing code for the Windows Phone 8, and I'm trying to learn the ins and outs of crossing the C#/C++ code barrier.
Let's say I have two float[] arrays on the C# side, we'll call them data1 and data2. I want to perform an element-by-element addition on them. As this is in a realtime system, (audio processing) I don't want to risk doing allocations and deallocations inside my audio processing routines, so I'll want something along the lines of data1[i] += data2[i]. Since I'm explicitly learning how to call C++ from C# though, I won't just write the for loop in C#, I will write it in C++, like so:
void LibFilter::mixIn( Platform::Array<float>^ * data1, const Platform::Array<float>^ data2 ) {
auto outArray = (*data1);
// Don't do anything if we don't have matching lengths
if( data2->Length != outArray->Length )
return;
// Sum data2 into outArray (which is just a dereferenced data1)
for( unsigned int i=0; i<outArray->Length; ++i ) {
outArray[i] += data2[i];
}
}
This seems like it should work to me, but then again, I'm a C++ programmer, not a C# programmer, and apparently this should be used with the out keyword in C#, which means that data1 is a null pointer, no matter the value passed in. I believe I want to use the ref keyword instead, but I can't figure out how to do that using the Windows Phone 8 SDK.
Suggestions on how to use the ref keyword here, or how best to architect this would be much appreciated, as I am still learning best practices with C#. Thank you all!
EDIT: Error information
If I just try to use libfilter.mixIn( ref data1, data2 ); from my C# code, I get the following:
error CS1502: The best overloaded method match for 'libfilter.LibFilter.mixIn(out float[], float[])' has some invalid arguments
error CS1620: Argument 1 must be passed with the 'out' keyword
Based on your edit, it seems that the metadata that the C++/CX compiler generates for the method uses the out keyword, not the ref keyword.
Anyway, I don't see why you need to pass by reference at all. Platform::Array is mutable, so you can just modify it even if it's passed by value. The following MSDN article has an example similar to your code (see the section "Fill arrays"):
http://msdn.microsoft.com/en-us/library/windows/apps/hh700131.aspx

'Invalid Handle object' when using a timer inside a function in MatLab

I am using a script in MatLab that works perfectly fine by itself, but I need to make a function out of it.
The script read a .csv file, extract all values, start a timer, and at each tick displays the corresponding coordinates extracted from the .csv, resulting in a 3D animation of my graph.
What I would like is to give it the location of the .csv, so that it starts displaying the graphs for this csv.
Here is what I have come up with:
function handFig(fileLoc)
csv=csvread(fileLoc,1,0);
both = csv(:,2:19);
ax=axes;
set(ax,'NextPlot','replacechildren');
Dt=0.1; %sampling period in secs
k=1;
hp1=text(both(k,1),both(k,2),both(k,3),'thumb'); %get handle to dot object
hold on;
hp2=text(both(k,4),both(k,5),both(k,6),'index');
hp3=text(both(k,7),both(k,8),both(k,9),'middle');
hp4=text(both(k,10),both(k,11),both(k,12),'ring');
hp5=text(both(k,13),both(k,14),both(k,15),'pinky');
hp6=text(both(k,16),both(k,17),both(k,18),'HAND');
L1=plot3([both(k,1),both(k,16)],[both(k,2),both(k,17)],[both(k,3),both(k,18)]);
L2=plot3([both(k,4),both(k,16)],[both(k,5),both(k,17)],[both(k,6),both(k,18)]);
L3=plot3([both(k,7),both(k,16)],[both(k,8),both(k,17)],[both(k,9),both(k,18)]);
L4=plot3([both(k,10),both(k,16)],[both(k,11),both(k,17)],[both(k,12),both(k,18)]);
L5=plot3([both(k,13),both(k,16)],[both(k,14),both(k,17)],[both(k,15),both(k,18)]);
hold off;
t1=timer('TimerFcn','k=doPlot(hp1,hp2,hp3,hp4,hp5,hp6,L1,L2,L3,L4,L5,both,t1,k)','Period', Dt,'ExecutionMode','fixedRate');
start(t1);
end
And the doplot function used:
function k=doPlot(hp1,hp2,hp3,hp4,hp5,hp6,L1,L2,L3,L4,L5,pos,t1,k)
k=k+1;
if k<5000%length(pos)
set(hp1,'pos',[pos(k,1),pos(k,2),pos(k,3)]);
axis([0 255 0 255 0 255]);
set(hp2,'pos',[pos(k,4),pos(k,5),pos(k,6)]);
set(hp3,'pos',[pos(k,7),pos(k,8),pos(k,9)]);
set(hp4,'pos',[pos(k,10),pos(k,11),pos(k,12)]);
set(hp5,'pos',[pos(k,13),pos(k,14),pos(k,15)]);
set(hp6,'pos',[pos(k,16),pos(k,17),pos(k,18)]);
set(L1,'XData',[pos(k,1),pos(k,16)],'YData',[pos(k,2),pos(k,17)],'ZData',[pos(k,3),pos(k,18)]);
set(L2,'XData',[pos(k,4),pos(k,16)],'YData',[pos(k,5),pos(k,17)],'ZData',[pos(k,6),pos(k,18)]);
set(L3,'XData',[pos(k,7),pos(k,16)],'YData',[pos(k,8),pos(k,17)],'ZData',[pos(k,9),pos(k,18)]);
set(L4,'XData',[pos(k,10),pos(k,16)],'YData',[pos(k,11),pos(k,17)],'ZData',[pos(k,12),pos(k,18)]);
set(L5,'XData',[pos(k,13),pos(k,16)],'YData',[pos(k,14),pos(k,17)],'ZData',[pos(k,15),pos(k,18)]);
else
k=1;
set(hp1,'pos',[pos(k,1),pos(k,2),pos(k,3)]);
axis([0 255 0 255 0 255]);
set(hp2,'pos',[pos(k,4),pos(k,5),pos(k,6)]);
set(hp3,'pos',[pos(k,7),pos(k,8),pos(k,9)]);
set(hp4,'pos',[pos(k,10),pos(k,11),pos(k,12)]);
set(hp5,'pos',[pos(k,13),pos(k,14),pos(k,15)]);
set(hp6,'pos',[pos(k,16),pos(k,17),pos(k,18)]);
end
However, when I run handFig('fileName.csv'), I obtain the same error everytime:
??? Error while evaluating TimerFcn for timer 'timer-7'
Invalid handle object.
I figured that it might come from the function trying to create a new 'csv' and 'both' everytime, so I tried removing them, and feeding the function the data directly, without results.
What is exactly the problem? Is there a solution?
Thanks a lot!
I think it's because when you call doPlot in the timer for the first time, you pass in t1 as an argument, and it might not exist the first time.
Does doPlot need t1 at all? I'd suggest modifying it so it's not used, and then your call to:
t1=timer('TimerFcn','k=doPlot(hp1,hp2,hp3,hp4,hp5,hp6,L1,L2,L3,L4,L5,both,k)','Period', Dt,'ExecutionMode','fixedRate');
Note the missing t1 in the doPlot call.
Either that, or initialise your t1 before you create the timer so it has some value to pass in.
Update (as an aside, can you use pause(Dct) in a loop instead? seems easier)
Actually, now I think it's a problem of scope.
It took a bit of digging to get to this, but looking at the Matlab documentation for function callbacks, it says:
When MATLAB evaluates function handles, the same variables are in scope as when the function handle was created. (In contrast, callbacks specified as strings are evaluated in the base workspace.)
You currently give your TimerFcn argument as a string, so k=doPlot(...) is evaluated in the base workspace. If you were to go to the matlab prompt, run handFig, and then type h1, you'd get an error because h1 is not available in the global workspace -- it's hidden inside handFig.
That's the problem you're running into.
However, the workaround is to specify your function as a function handle rather than a string (it says function handles are evaluated in the scope in which they are created, ie within handFig).
Function handles to TimerFcn have to have two arguments obj and event (see Creating Callback Functions). Also, that help file says you have to put doPlot in its own m-file to have it not evaluate in the base Matlab workspace.
In addition to these two required input arguments, your callback
function can accept application-specific arguments. To receive these
input arguments, you must use a cell array when specifying the name of
the function as the value of a callback property. For more
information, see Specifying the Value of Callback Function Properties.
It goes through an example of what you have to do to get this working. Something like:
% create timer
t = timer('Period', Dt,'ExecutionMode','fixedRate');
% attach `k` to t so it can be accessed within doPlot
set(t,'UserData',k);
% specify TimerFcn and its extra arguments:
t.TimerFcn = { #doPlot, hp1, hp2, hp3, ...., both };
start(t)
Note -- the reason k is set in UserData is because it needs to be somehow saved and modified between calls to doPlot.
Then modify your doPlot to have two arguments at the beginning (which aren't used), and not accept the k argument. To extract k you do get(timer_obj,'UserData') from within doPlot:
function k=doPlot(timer_obj, event, hp1,hp2,hp3,.....)
k = get(timer_obj,'UserData');
.... % rest of code here.
% save back k so it's changed for next time!
set(timer_obj,'UserData',k);
I think that's on the right track - play around with it. I'd highly recommend the mathworks forums for this sort of thing too, those people are whizzes.
This thread from the mathworks forum was what got me started and might prove helpful to you.
Good luck!

Does the construct do .. while(false) contribute to better control flow?

I've recently come across this code:
do {
if ( ! checkSomething() )
break;
// some code
if ( ! checkSomeOtherThing() )
break;
// some other code
} while(false);
// some final code
The programmer that wrote it, wrote a comment along the lines of "cleaner control flow".
In my opinion, the original code could look better if its refactored into something else. But is there any truth in this statement ? Is this construct any good ?
I find this much easier to read, and it produces an identical result:
if ( checkSomething() )
{
// some code
if ( checkSomeOtherThing() )
{
// some other code
}
}
// some final code
I think do ... while is normally hard to follow, but using it for something other than a loop is misleading at best.
This is equivalent to a goto.
In such situations, it is better to use a goto than to use an ugly hack.
Changing it to use a goto makes it much more readable:
if (!checkSomething())
goto Done;
// some code
if (!checkSomeOtherThing())
goto Done;
// some other code
Done: //some final code
If you don't mind loops containing several break statements, then the only problem here is that C (for obvious reasons) doesn't let you break out of a bare block, hence the "non-loop" which some unsuspecting future maintainer could mistake for a real loop.
The considerations, I think, are:
if there are only two break points, what's so bad about two if statements?
if there are more than two break points then the indentation with if statements could get unpleasant, and this saves that, but then again is the function doing too much? And even if not, would it be better just to use goto and avoid the weirdness of a loop that doesn't loop?
Since you tag this language-agnostic, I used to use a macroised assembly language, with a block ... endblock that you could break out of. This lead to reasonably nice code for checking necessary conditions, such as:
block
breakif str1 == null
breakif str2 == null
get some combined property of str1 and str2
breakif some other condition that stops us getting on with it
get on with it
endblock
Actually, it wasn't breakif str1 == null, it was breakifeq.p str1, null, or something like that, but I forget exactly what.
I've seen the do-while form adopted as a standard to which coders conformed. The advantage is that it communicates, and implements, that the loop will always be executed at least once. This helps isolate, with consistency, the situations where something different occurs, i.e. where the code in the loop is not executed.
This standard was adopted because the Warnier-Orr technique was being applied.