Caching is orthogonal to recursion... Explain? [closed] - c

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 13 years ago.
Improve this question
Although it's true that some
recursive-nameserver configurations
are (sloppily) referred to as
"caching", e.g., by
RHEL/Fedora/CentOS, that's a really
bad name for that function -- because
caching is orthogonal to recursion.
Theoretically, you could write a
nameserver that does recursive service
but doesn't cache its results. (That
would be a bit perverse, and I don't
know of any.) Conversely, nameserver
packages that cache but know nothing
about how to recurse and instead do
less-helpful alternative iterative
service are common: dnsmasq, pdnsd,
etc. ... ...
Above text source: http://linuxgazette.net/170/lan.html
Please explain what does the author means by "caching is orthogonal to recursion" ?

From Wikipedia's definition of orthogonal:
For example, a car has orthogonal
components and controls (e.g.
accelerating the vehicle does not
influence anything else but the
components involved exclusively with
the acceleration function).
The author is saying that whether a nameserver caches is nothing to do with whether it can recurse.

caching is orthogonal to recursion?
Caching doesn't require/imply recursion.
The term "orthogonal" is meant to be interpreted from a mathematical sense loosely has "the things have nothing in common i.e. separate concepts".

It mean it is one feature is independent with the other one. Or have bothe feature have no influence with the other one. So they can be implemented independantly
in a programming point of view, two orthogonal features
do_work(bool feature1, bool feature2)
{
// do common work
if(feature1)
{ //... do this }
// do common work
if(feature2)
{ // do work }
// do common work
}
or: if they are not orthogonal:
you need to do this: ( and it may have case where you cant combine the two feature.
do_work(bool feature1, bool feature2)
{
if(not feature1 and feature 2)
{ //... do this }
else if(feature1 and not feature2)
{ // do work }
// else impossible or different behavior
// etc..
}

Related

Should I put the game logic inside the draw loop? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I'm making a simple game using CSFML and am wondering if I can make my code more efficient.
Right now the code looks like this inside the main function's game loop:
// game logic
for (int i = 0; i <= perf.arramount; i++)
{
UpdateBody(&body[i]);
}
// clear
sfRenderWindow_clear(window.window, window.bg);
// draw
for (int i = 0; i <= perf.arramount; i++)
{
sfRenderWindow_drawRectangleShape(window.window, body[i].rect, NULL);
}
// display
sfRenderWindow_display(window.window);
I was told to do things in the order: logic, clear, draw, display. However, would there be any issues with me putting the logic inside the draw loop like this?
// clear
sfRenderWindow_clear(window.window, window.bg);
// draw
for (int i = 0; i <= perf.arramount; i++)
{
UpdateBody(&body[i]);
sfRenderWindow_drawRectangleShape(window.window, body[i].rect, NULL);
}
// display
sfRenderWindow_display(window.window);
Would it slow down the draw calls or make the code more efficient?
I'm not sure if this specific library may be different from others, so even a general answer is appreciated.
How to structure your game is your design decision.
You will be the first to notice problems with it.
But here is my experience. I learned that in the game logic loop you have calculations which are definitly needed, they also tend to need information on how much time has passed. In the drawing loop you just need to visualise the result of the calculations. As soon as you are going to split into threads, the timing of the need to calculate and to visualise will change. You are likely to want to calculate more often than you draw, while you want to draw always completly (except for more advanced FPS-based level of detail rendering....).
So there are multiple aspects which might make you want to keep those two things apart.
What I did was having a calculation step (which by my design gets real time information as a parameter of elapsed seconds) and a separate drawing step. That way you can have them in one parent loop, but you are free to move them at any point AND you are protecting yourself from confusing those two steps.

A "should never get here" line in a method [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I find myself writing functions with this line and I always feel a little bad about myself when doing it. Do other people do this? Is it bad practice?
void f()
{
while(true) {
// ...
if(condition)
return;
}
//should not get here
assert(0);
}
Depends on how critical it is that the code never get to that point.
If I have code that absolutely can't be allowed to fail, I frequently put in checks for non-allowable conditions that shouldn't be able to happen.
If it's a case/switch statement, I always put in a default with an error handler even though it should never be able to get there.
Do other people do this? Is it bad practice?
Yes, other coders do this. No, it not not bad practice, just weaker than alternatives.
The reason it is weak is reflected in this question. It draws unnecessary attention to a coding style that has other good alternatives. Why code in a manner that looks wrong? Even if OK here, it encourages reviewers to become nonchalant about seeing other loops without a clear exit - that in itself is not good.
void f() {
while(true) {
// ...
if(condition) {
break;
}
// ...
}
}
If there is a good reason to code in OP's exceptional style, it deserves comments to explain why to not waste time of reviewers, maintainers or anyone reading the code.
void f() {
while(true) {
// ...
if(condition) {
// Clear documentation why the above layout does not work
return;
}
// ...
}
//should not get here
assert(0);
}

Code structure for sending a lot of values in C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm making a robot that should have simple two way communication for a large variety of values to a desktop via UART (serial communication, modem port). It's also desirable for it to be easy to add new values.
The board on the robot is an Olimexino, a rather powerful Arduino clone with an arm processor. For the sake of simplicity, I believe it could be affordable to store all relevant global values as floats.
My idea is to store all values in a matrix, like below. On the robot I for instance would like multiple controllers, which here can be viewed just as a heap of values grouped. These should be easy to modify and send.
enum rows
{
controller_a,
controller_b,
controller_c,
rows
}
enum controllers
{
controller_value1,
controller_value2,
columns
}
float values[rows][columns]
This should make it quite easy to for instance add a controller. It would also be rather easy to send and insert values, as I could target any vale by just sending the coordinates as [row, column].
The question is though, are there other better ways?
Should I perhaps define the controllers in structs instead
struct controller
{
float controller_value1;
etc
}controllers[columns];
If I make sure that all controllervalues are floats, maybe I could use pointers to the structs for easy read/write? I dont't know how to do that pointer aritmethics though. I guess it would require a specific routine for handling each different struct, meaning that the code wouldnt be that easy to modify?
First, it is a very very bad idea to use floats if you are going to be sending the data between devices, particularly if they are going to be of different architectures, it would be far more sound to use 64 bit integers and implement a fixed point by hand where necessary. Floats are not of a predictable format in memory and thus trying to serialise them is highly difficult.
Second, I think you have some sound ideas as far as storing the data goes, using a 2D array of a fixed type to store the data will work well and hopefully be well optimised. The enums are also a nice way of avoiding confusion. However what you are missing is a way to send them easily. As you are using a micro a nice simple protocol would do, may I suggest something along the lines of the following. This assumes you are keeping the table synced between your computer and the arduino like:
uint64_t ValueTable[CONTROLLER_MAX][VALUE_MAX];
uint32_t UartFD; // This gets set somewhere during init
// It should be a non blocking fd so we dont block if there are
// no value changes
void SetValue(uint32_t ControllerNum, uint32_t ValueIndex, uint64_t Value)
{
ValueTable[ControllerNum][ValueIndex] = Value;
write(UartFD, &ControllerNum, sizeof(uint32_t));
write(UartFD, &ValueIndex, sizeof(uint32_t));
write(UartFD, &Value, sizeof(uint64_t));
}
void GetValue(void)]
{
uint32_t ContNum;
uint32_t ValIndx;
uint64_t Value;
if(!read(UartFD, &ContNum, sizeof(uint32_t)))
{
// No data waiting
return;
}
// Wait for the rest
while(!read(UartFD, &ValIndx, sizeof(uint32_t)));
while(!read(UartFD, &Value, sizeof(uint64_t)));
ValueTable[ContNum][ValIndx] = Value;
}
Obviously this is the version for the PC, the arduino needs read and write replaced with Serial.read and Serial.write and does not need the first argument, otherwise the two are the same.Obviously you need to call those wherever you write to a value in the table.
Effectively for what you are doing your idea is great, just make sure to keep the implementation fast and simple so it runs well on the robot's embedded system. Yuo need not worry too much about extensibility, as you are likely to update both at once, particularly if you can keep a fair amount of common code between the PC and robot.

Accessing multidimensional arrays without using loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hi friends I was working on a project where we need to use quite a few multidimensional arrays. I am using for loops to access different array elements, then I just thought what if I don’t have a liberty to use looping? How am I going to access array element?
I am new to C, so thought of discussing here, I am sure there might be thousans of people who could have thought the same way, and hopefully found the solution.
Below example of multidimensional array is give, please guide me.
Thanks
static int t[3][2][4] = { {2,4,3,6,
1,6,7,9,},
{8,2,1,1,
2,3,7,3,},
{1,6,2,4,
0,7,9,5,},
};
Please Help me...thanks!
If you need to go through all of the values inside the loop without manual handling (i.e. x = t[1][1][1] then x = t[1][1][2] etc) then you want to use loops, enhanced loops or iterators. However since you're using C the only of those three options available are standard loops, which you're trying not to use. So... there's mo straight forward way to do that really.
If you're willing to use some other C libraries however then there may be more options for you. Iterator libraries probably exist.
A non-straightforward way to do it (if you're looking for one) could be through recursion, however that's really quite wasteful. I advise you just use loops :P
What are you trying to prove with loops and without loops should be first thought.
If u want to access all the elements and not use loop is like writing a lot of code manually and waste of memory(in your case 3 * 2 * 4 no of lines instead of few ).
Instead of showing the array if you had put in your code how and where you accessing elements it would have been more clear to tell what you wanted .

Is there such a thing as a javascript deminifier (deobfuscator)? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
This question is exactly the opposite of Which Javascript minifier (cruncher) does the same things that the one Google uses for its JS APIs?
I want to learn how google does it's loading so I can build my own with non-popular JS toolkits.
Try this: JS Beautifier
Try http://www.jsnice.org/
I just stumbled on it and it is great. It expands the code. It has statistical variable renaming. for example, if you have this code:
var g = f.originalEvent.targetTouches[0];
Then it it turns your code into:
var touches = event.originalEvent.targetTouches[0];
Pretty good guess, methinks.
It turned this:
d.slide.hasClass("selected") ? (e.onSlideOpen.call(d.prev.children("div")[0]), q ? (e.rtl && d.slide.position().left > i.w / 2 || d.slide.position().left < i.w / 2) && h.animateSlide.call(d) : t && d.index ? (h.animateGroup(d, !0), h.fitToContent(d.prev)) : d.slide.position().top < i.h / 2 && h.animateSlide.call(d)) : (setTimeout(function() { e.onSlideOpen.call(d.slide.children("div")[0]) }, e.slideSpeed), h.animateGroup(d), t && h.fitToContent(d.slide)), e.autoPlay && (f.stop(), f.play(l.index(j.filter(".selected"))))
Into this:
if (e.slide.hasClass("selected")) {
settings.onSlideOpen.call(e.prev.children("div")[0]);
if (val) {
if (settings.rtl && e.slide.position().left > box.w / 2 || e.slide.position().left < box.w / 2) {
self.animateSlide.call(e);
}
} else {
if (isMac && e.index) {
self.animateGroup(e, true);
self.fitToContent(e.prev);
} else {
if (e.slide.position().top < box.h / 2) {
self.animateSlide.call(e);
}
}
}
} else {
setTimeout(function() {
settings.onSlideOpen.call(e.slide.children("div")[0]);
}, settings.slideSpeed);
self.animateGroup(e);
if (isMac) {
self.fitToContent(e.slide);
}
}
if (settings.autoPlay) {
node.stop();
node.play(tabs.index(options.filter(".selected")));
}
A library I'm working on has a couple of bugs, and after spending hours trying to decipher the code, finding this is going to save me a bunch of time.
Seriously, this tool wipes the floor with JS Beautifier.
Uhhh, it would be impossible to restore variable names unless there was a mapping of minified -> original variable names available. Otherwise, I think the authors of that tool could win the Randi prize for psychic feats.
Chrome Developer tools has this built in
You will not be able to reconstruct method name or variable names. The best you can hope for is a simple JS code formater (like those previously mentioned), and then to go through the file method by method, line by line, working out what each part does.
Perhaps using a good JS refactoring tool would make this easier as well (being able to rename/document methods)
You can use the \b (word boundary) feature in regular expressions to find single-letter variable names in a file.
for i in "abcdefghij..z"; do
sed -i "s/\b$i\b/$(random /usr/share/dict/words)/g" somefile.js
done
You can also use this in vim with something like :%s/\<a\>/truesaiyanpower/g.
To unminify js files, Unminify would be the best!
To unminify css, html and js files, you can use Unminify or Unminify JS online tool!
See our SD ECMAScript Formatter for a tool that will nicely format code.
EDIT: If you want to reverse the renaming process you need something can rename the obfuscated names back to the originals.
This tool can technically do that: SD Thicket ECMAScript Obfuscator.
It does so by applying a renaming map over which you have precise control.
Typically you implicitly construct such a map during the obfuscation process by choosing which names to obfuscate and which to preserve, and the obfuscator applies that map to produce the obfuscated code.
The Thicket obfuscator generates this map as side effect when you obfuscate
in the form essentially of a set of pairs (originalname,obfuscatedname)
for reference and debugging purposes.
Swapping elements gives the map (obfuscatedname,originalname). That inverted map can be applied by Thicket to recover the code with the original names, from the obfuscated code. And the Thicket obfuscator includes the Formatter to let you make it look nice again.
The catch to "reversing minification" (as you put it poorly, you are trying to reverse obfuscation), is that you need the map. Since people doing obfuscation don't give away the map, you, as a recipient of obfuscated code, have nothing to apply. A would-be pirate would have to reconstruct the map presumably by painful reverse engineering.
The "reversing" process also can't recover comments. They're gone forever.
This is all by design, so the real question is why are you looking to reverse obfuscation?
Javascript minifier and Javascript Obfuscator are two different things.
Minifier - removes the comments, unnecessary whitespace and newlines from a program.
Obfuscator - make modifications to the program, changing the names of variables, functions, and members, making the program much harder to understand. Some obfuscators are quite aggressive in their modifications to code.
This Javascript Obfuscator will obfuscate your code.
If you want to deobfuscate your code, try Javascript Beautifier. It will deobfuscate if obfuscation is found and then beautify the code.

Resources