I have a very big C programming project that uses thousands of struct variables with this naming convention:
specificstruct->x = specificstruct->y + specificstruct->z
I want to do some heavy refactoring, namely converting most of these struct members to arrays. The code above would look like this:
specificstruct->x[i] = specificstruct->y[i] + specificstruct->z[i]
... and I don't feel like wasting an entire day on doing all this manually. Does anyone have a suitable regex in store?
EDIT: It is always the same struct, but the equations vary.
Thanks in advance!
Best regards,
P. Nilsson
I'm not sure about your particular case, but maybe Coccinelle can help you. It is a system for patching source code, based on some rules like "if x is an expression without function invocations, change x+x to 2*x" etc.
For a generalized approach something like this ought to do - the assumption is that you've got consistent spacing between your expressions
(.*?->.) = (.*?->.) \+ (.*?->.)
You can then write your new array structure as:
\1[i] = \2[i] + \3[i]
s/\(specificstruct->x\) = \(specificstruct->y\ )\+ \(specificstruct->z\)/\1[i] = \2[i] + \3[i]/g
If you're just looking for the name followed by -> then a single character, you could try
(?<struct>\w+)\s?->\s?(?<var>\w{1}) //single char after ->
(?<struct>\w+)\s?->\s?(?<var>\w+) //multiple char after ->
That way you have groups so you can compare the names before you do any replacements. The \s? helps to match even if you added spacing between some but not others.
Related
Trying to implement something like this:
arr = (1..10)
arr[2,5] = [2,3,4,5]
arr(2,5] = [3,4,5]
arr[2,5) = [2,3,4]
arr(2,5) = [3,4]
Well, we need to override four bracket opreators: [], [), (], ()
Any ideas?
It's called "Including or excluding" in mathematics. https://en.wikipedia.org/wiki/Interval_(mathematics)#Including_or_excluding_endpoints
In short, this is not possible with the current Ruby parser.
The slightly longer answer: You'd have to start by modifying parse.y to support the syntax you propose and recompile Ruby. This is of course not a terrible practical approach, since you'd have to do that again for every new Ruby version. The saner approach would be to start a discussion on ruby-core to see if there is sufficient interest for this to be made part of the language (probably not tbh).
Your wanted syntax is not valid for the Ruby parser, but it could be implemented in Ruby with the help of self-modifying code.
The source files need to be pre-processed. A simple regular expression can substitute your interval expressions with ordinary method syntax, i.e.
arr[2,5] -> interval_closed(arr,2,5)
arr(2,5] -> interval_left_open(arr,2,5)
arr[2,5) -> interval_right_open(arr,2,5)
arr(2,5) -> interval_open(arr,2,5)
The string holding the modified source can be evaluated and becomes part of the application just like a source file on the hard disk. (See instance_eval)
The usage of self-modifying code should be well justified.
Is the added value worth the effort and the complications?
Does the code have to be readable for other programmers?
Is the preprocessing practical? E.g. will this syntax occur in one or a few isolated files, or be spread everywhere?
I'm currently writing a program that basically logs data from multiple peripherals. Since I'm logging the same data from each peripheral, I saw this as a good opportunity to encapsulate this data in a structure. I originally envisioned doing this with a "C" style structure, but when I dug into MATLAB's documentation, I realized the syntax is quite different.
To be more specific, I need - in MATLAB's conventions - a 1-by-6 structure. If I were using C, I would simply define a structure prototype, and create as many instances of it as I needed. Ideally, this allows me to cleanly organize my code.
However, MATLAB doesn't seem to provide this capability. For instance, this is one way I saw how to do what I want:
patient.name = 'John Doe';
patient.billing = 1;
%Create a second "instance"
patient.name(2) = 'Someone else';
patient.billing(2) = 2;
The method above does work, being that I can add as many instances as I want. Still, I'm wondering if I could simply define a generic structure with the fields I already need? If I can, this would allow me to better distinguish between the different peripherals while maintaining cleaner and easier to follow code.
Any constructive input is appreciated.
You can make structs in a similar way to c if you would like.
patient.name(2) = 'Someone else';
patient.billing(2) = 2;
This is simply making the elements of a struct into arrays. If you want to make an array of structs you would do this:
patient(2).name = 'Someone else';
patient(2).billing = 2;
If you want to make a struct with fields that you need you can do this:
function outstruct = createstruct(name, billing history, age)
outstruct = struct('name', name, 'billing', billing, 'hist ...
end
Although it would be just as easy to add that to the body of the code without the function.
I am trying to create an ADT.
It is a dynamic set of finite elements. It must be implemented using arrays and linked lists.
Some operations include add(set, x) and remove(set, x).
I understand that I first need to create an interface which will be common to both the array implementation and the linked list implementation.
I am however, not sure as regards the structure for this data type. What should I include?
struct test {
int x;
char y;
};
Something like that? Or let's say that I make the set exclusive for integers, what will the data structure involve?
Help would be greatly appreciated. Thanks!
Since this is for school, I'm not going to give you an implementation, but I'll point you in the right direction. Using arrays and lists screams 'hash table'. See this answer for some good information.
For simplicity, let's assume it's a set of integers.
Essentially, you want an array hash_table of N 'buckets', i.e. lists. To add an element x, you do hash_table[hash(x) % N] to get the 'bucket' (list) x should go into, and add it to that list if it's not already in the list.
To remove x, do hash_table[hash(x) % N] to get your bucket, and remove x if it's there.
If you can implement those, search(set, x) is trivial. You can also try implementing union(setA, setB), intersect(setA, setB), difference(setA, setB), isSubset(setA, setB), etc. You may also want to peruse the Wikipedia article on the Set ADT, and do check out the entry in The Algorithm Design Manual which includes links to implementations at the bottom.
Good luck, and happy coding. If you get stuck it's always OK to ask here on SO, just post code next time. :)
This is the principle of a structure, you can put all type of variable you want in it ..
I think you want to create an array of int.
Declaring like :
int tab["number of int you want"];
And access it like that :
tab["number of case of the int you want to access"];
I'd like my program to not treat this:
{0:1000}
{ones(1,1000)}
not as vectors when I input them for my structure array. Any idea on how to do this? Thanks in advance.
if you want a complete other action of the function depending on the input type, you'll have to write a wrapper, catching off this input type. This is possible with the isa function. It works as follows
if isa(var,'double')
% do something with the double
elseif isa(var,'struct')
% do something with the structure
else
% ...
end
OR
maybe it's possible to avoid this wrapping and treat all variables alike, but then you'll have to provide a bit more information about this function and what you want it to do...
I have legacy C code base at work and I find a lot of function implementations in the style below.
char *DoStuff(char *inPtr, char *outPtr, char *error, long *amount)
{
*error = 0;
*amount = 0;
// Read bytes from inPtr and decode them as a long storing in amount
// before returning as a formatted string in outPtr.
return (outPtr);
}
Using DoStuff:
myOutPtr = DoStuff(myInPtr, myOutPtr, myError, &myAmount);
I find that pretty obtuse and when I need to implement a similar function I end up doing:
long NewDoStuff(char *inPtr, char *error)
{
long amount = 0;
*error = 0;
// Read bytes from inPtr and decode them as a long storing in amount.
return amount;
}
Using NewDoStuff:
myAmount = NewDoStuff(myInPtr, myError);
myOutPtr += sprintf (myOutPtr, "%d", myAmount);
I can't help but wondering if there is something I'm missing with the top example, is there a good reason to use that type of approach?
One advantage is that if you have many, many calls to these functions in your code, it will quickly become tedious to have to repeat the sprintf calls over and over again.
Also, returning the out pointer makes it possible for you to do things like:
DoOtherStuff(DoStuff(myInPtr, myOutPtr, myError, &myAmount), &myOther);
With your new approach, the equivalent code is quite a lot more verbose:
myAmount = DoNewStuff(myInPtr, myError);
myOutPtr += sprintf("%d", myAmount);
myOther = DoOtherStuff(myInPtr, myError);
myOutPtr += sprintf("%d", myOther);
It is the C standard library style. The return value is there to aid chaining of function calls.
Also, DoStuff is cleaner IMO. And you really should be using snprintf. And a change in the internals of buffer management do not affect your code. However, this is no longer true with NewDoStuff.
The code you presented is a little unclear (for example, why are you adding myOutPtr with the results of the sprintf.
However, in general what it seems that you're essentially describing is the breakdown of one function that does two things into a function that does one thing and a code that does something else (the concatenation).
Separating responsibilities into two functions is a good idea. However, you would want to have a separate function for this concatenation and formatting, it's really not clear.
In addition, every time you break a function call into multiple calls, you are creating code replication. Code replication is never a good idea, so you would need a function to do that, and you will end up (this being C) with something that looks like your original DoStuff.
So I am not sure that there is much you can do about this. One of the limitations of non-OOP languages is that you have to send huge amounts of parameters (unless you used structs). You might not be able to avoid the giant interface.
If you wind up having to do the sprintf call after every call to NewDoStuff, then you are repeating yourself (and therefore violating the DRY principle). When you realize that you need to format it differently you will need to change it in every location instead of just the one.
As a rule of thumb, if the interface to one of my functions exceeds 110 columns, I look strongly at using a structure (and if I'm taking the best approach). What I don't (ever) want to do is take a function that does 5 things and break it into 5 functions, unless some functionality within the function is not only useful, but needed on its own.
I would favor the first function, but I'm also quite accustomed to the standard C style.