I want to simplify the following code in C. Is there any hash table in C to make it simple? For example "dict" in Python.
int a, b, c, d ......
a = get_value_from_sth( A_NAME )
b = get_value_from_sth( B_NAME )
c = get_value_from_sth( C_NAME )
d = get_value_from_sth( D_NAME )
......
No, C does not have a built-in hash table type like Python's dicts. You may be able to get by with an array, depending on your needs.
Check out glib hash tables. Not "official" or "built in," but widely used and as close as you can get to a standard hash table implementation for C.
You will need to create a function to map a ptr to a value in an array.
This is how python does it.
http://docs.python.org/c-api/dict.html
Personally I do not bother. It's C. The best solution will still be ugly.
Related
I am trying to write a function in CPN ML which changes 3 variables, but I don't know how, I just can write one statement. My function should be something like this:
fun T1() =
x=x+1;
y=y+2;
k=k-1;
when I write this lines of code, I get an error.
Caveat: I don't know anything about CPN ML, but based on this I guess it has syntax similar to Standard ML?
In that case, you would need to group the statements in parentheses:
fun T1 () =
(x=x+1;
y=y+2;
k=k-1)
In SML, expressions can also be separated by semicolons in the body of a let expression, like this:
fun T1() =
let in
x=x+1;
y=y+2;
k=k-1
end
Some people prefer this to parentheses because it looks more block-structured. It also gives you a place to insert declarations (in the let .. in part), which is a common way for a function to evolve.
Of course, since this is a functional language, you either need to be using reference cells (x := !x + 1) or declaring new variables (val x = x + 1) to do what you have in the body of your function. There aren't really "statements" like in C and all variables are immutable.
I've embedded Lua into my C application, and am trying to figure out why a table created in my C code via:
lua_createtable(L, 0, numObjects);
and returned to Lua, will produce a result of zero when I call the following:
print("Num entries", table.getn(data))
(Where "data" is the table created by lua_createtable above)
There's clearly data in the table, as I can walk over each entry (string : userdata) pair via:
for key, val in pairs(data) do
...
end
But why does table.getn(data) return zero? Do I need to insert something into the meta of the table when I create it with lua_createtable? I've been looking at examples of lua_createtable use, and I haven't seen this done anywhere....
table.getn (which you shouldn't be using in Lua 5.1+. Use the length operator #) returns the number of elements in the array part of the table.
The array part is every key that starts with the number 1 and increases up until the first value that is nil (not present). If all of your keys are strings, then the size of the array part of your table is 0.
Although it's a costly (O(n) vs O(1) for simple lists), you can also add a method to count the elements of your map :
>> function table.map_length(t)
local c = 0
for k,v in pairs(t) do
c = c+1
end
return c
end
>> a = {spam="data1",egg='data2'}
>> table.map_length(a)
2
If you have such requirements, and if your environment allows you to do so think about using penlight that provides that kind of features and much more.
the # operator (and table.getn) effectivly return the size of the array section (though when you have a holey table the semantics are more complex)
It does not count anything in the hash part of the table (eg, string keys)
for k,v in pairs(tbl) do count = count + 1 end
I am looking for something like list comprehensions in matlab however I couldnt find anything like this in the documentary.
In python it would be something like
A=[i/50 for i in range(50)]
Matlab is very fond of 'vectorizing'. You would write your example as:
A = (0:49) ./ 50
Matlab hates loops and therefore list comprehension. That said, take a look at the arrayfun function.
You can do:
(1:50)/50
Or for something more general, you can do:
f=#(x) (x/50);
arrayfun(f,1:50)
No, Matlab does not have list comprehensions. You really don't need it, as the focus should be on array-level computations:
A = (1:50) / 50
Matlab can work with arrays directly, making list comprehension less useful
If what you're trying to do is as trivial as the sample, you could simply do a scalar divide:
A = (0:50) ./ 50
There are several ways to generate a list in Matlab that goes from 0 to 49/50 in increments of 1/50
A = (0:49)/50
B = 0:1/50:49/50
C = linspace(0,49/50,50)
EDIT As Sam Roberts pointed out in the comments, even though all of these lists should be equivalent, the numerical results are different due to floating-point errors. For example:
max(abs(A-B))
ans =
1.1102e-16
This doesn't work help with your numerical example but for the special case of strings there is the compose function that does the same thing as a list comprehension of the form:
s = [f"Label_{i}" for i in range(1, 6)]
Example:
str = compose("Label_%d", 1:5)
Result:
str =
1×5 string array
"Label_1" "Label_2" "Label_3" "Label_4" "Label_5"
I'm inexperienced with MATLAB, so sorry for the newbie question:
I've got a large vector (905350 elements) storing a whole bunch of data in it.
I have the standard deviation and mean, and now I want to cut out all the data points that are above/below one standard deviation from the mean.
I just have no clue how. From what I gather I have to make a double loop of some sort?
It's like: mean-std < data i want < mean + std
If the data is in variable A, with the mean stored in meanA and the standard deviation stored in stdA, then the following will extract the data you want while maintaining the original order of the data values:
B = A((A > meanA-stdA) & (A < meanA+stdA));
Here are some helpful documentation links that touch on the concepts used above: logical operators, matrix indexing.
You can simply use the Element-wise logical AND:
m = mean(A);
sd = std(A);
B = A( A>m-sd & A<m+sd );
Also, knowing that: |x|<c iff -c<x<c, you can combine both into one as:
B = A( abs(A-m)<sd );
Taking A as your original vector, and B as the final one:
B = sort(A)
B = B(find(B > mean-std,1,'first'):find(B < mean+std,1,'last'))
y = x(x > mean-std);
y = y(y < mean+std);
should work. See FIND for more details. The FIND command is being used implicitly in the above code.
These days I'm solving Project Euler problems in Erlang.
Since I'm a C++ programmer from the beginning, sometimes I really want to code using two dimensional arrays.
One of my idea is to use tuples and lists like this:
List=[{X,0}||X<-lists:seq(1,3)]
{1,0}
{2,0}
{3,0}
Is there nice way to implement multidimensional arrays in Erlang?
See array module but for multidimensional access you have to write your own wrapper. If any of your dimension is short and access is mostly read you can use tuples and use erlang:element and erlang:setelement. Own wrapper is recommended anyway.
Try array(actually dict) with {X, Y, Z} as a key. It's look like 3d array ;)
I wrote a small wrapper over array module for 2d arrays
-module(array_2d).
-export([new/2, get/3, set/4]).
new(Rows, Cols)->
A = array:new(Rows),
array:map(fun(_X, _T) -> array:new(Cols) end, A).
get(RowI, ColI, A) ->
Row = array:get(RowI, A),
array:get(ColI, Row).
set(RowI, ColI, Ele, A) ->
Row = array:get(RowI, A),
Row2 = array:set(ColI, Ele, Row),
array:set(RowI, Row2, A).