• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

theecakee

Member
I fucking love Python, I'm diving into the depths of the language and I fall in love with its features.

Python is my new husbando/waifu.

Can you suggest me some cool books about advanced Python?

Here is a cool list of projects to make.

This book has some cool stuff you can do with Python.

Siraj Raval does a lot of cool AI Python projects.

Codefights was mentioned before, but another site like that.

Studying pretty hard for my exam; I actually made myself sick, lol. This semester was particularly stressful. One more exam, though. A lot of things I am finding easier once I stopped relying on textbook explanations of things and went to YouTube for visualizations. I hope I can do well.

Yeah same, what classes you taking?

Only CS class I'm doing is Computer Networks this semester (well also doing a research project with a professor but...not really a class only 2 credits). I'm doing pretty terrible this semester lol.
 

Mr.Mike

Member
If I want to open a connection directly between two machines across the internet (sitting behind different routers/NAT), without having to use a third party, what is the easiest way to do that? Ideally this should require no mucking around with settings on either users router, or any configuration at all.

I have an idea for a software project to make it easy to send files directly to people. To send a file to someone, as far as I know, right now you either use some middle-man like Dropbox or Gmail, or have one of the parties set up an FTP or some such server.

The first solution is fine in most cases, and is what most people do, although it sometimes requires creating accounts and has size limits, and introduces a reliance on a third party. The second one isn't really too hard if you're familiar with this stuff, but for the average user it's too much, the main hassle being configuration.

To make the second solution as easy as the first it would have to deal with the hassle of opening a port to listen to on it's own, I'm currently thinking it could do so using PCP. From there the software on the server (person sending the file) could just open up a port to listen on and generate a link encoding IP address, port, some unique id identifying the file of interest on the server. This link could be copy and pasted over whatever chat app and the client (person receiving the file) would paste it into the software on their side. This would be just as easy as the first solution that's popular now for the receiver, and maybe even easier for the sender, and would have no size limits or reliance on third party services.

Maybe put in some authentication features too, but the basic functionality seems like it should be pretty easy once you've got the NAT figure out. Probably best to just use an existing FTP library from there.
 

Somnid

Member
If I want to open a connection directly between two machines across the internet (sitting behind different routers/NAT), without having to use a third party, what is the easiest way to do that? Ideally this should require no mucking around with settings on either users router, or any configuration at all.

I have an idea for a software project to make it easy to send files directly to people. To send a file to someone, as far as I know, right now you either use some middle-man like Dropbox or Gmail, or have one of the parties set up an FTP or some such server.

The first solution is fine in most cases, and is what most people do, although it sometimes requires creating accounts and has size limits, and introduces a reliance on a third party. The second one isn't really too hard if you're familiar with this stuff, but for the average user it's too much, the main hassle being configuration.

To make the second solution as easy as the first it would have to deal with the hassle of opening a port to listen to on it's own, I'm currently thinking it could do so using PCP. From there the software on the server (person sending the file) could just open up a port to listen on and generate a link encoding IP address, port, some unique id identifying the file of interest on the server. This link could be copy and pasted over whatever chat app and the client (person receiving the file) would paste it into the software on their side. This would be just as easy as the first solution that's popular now for the receiver, and maybe even easier for the sender, and would have no size limits or reliance on third party services.

Maybe put in some authentication features too, but the basic functionality seems like it should be pretty easy once you've got the NAT figure out. Probably best to just use an existing FTP library from there.

You need a STUN server so each machine can get it's public IP address. There are public ones available. Unfortunately, you still aren't guaranteed that the network isn't going to be configured in some way that prevents your traffic from reaching it's destination. There's another type of server called a TURN server that can forward traffic, as you might suspect these a not public because they carry lots of traffic. You should look into WebRTC. Provided you have some ICE servers (STUN, TURN, TURNS) and some signaling channel to pass the SDP (this contains among other things the public IP address and some meta data about what kind of connection you want to allow) back and forth you can build this completely in browser.

Also, if you want the easy way out, just use webtorrent (https://webtorrent.io/) which is a bitorrent-like protocol built on top of WebRTC (the desktop webtorrent will actually interchange with vanilla bittorrent so web users have some access to the primary network).
 
So I am doing a C++ module (been programming in Java uptil this point) on my course and need some help. I don't understand how this piece of code prints 12 instead of 20.
Code:
#define func(x) (x*x)-x
....
int i=2;
cout << "func(i+3) is " << func(i+3) << endl;
As for the code below, I understand that s is a pointer to ss so whatever changes made to s also apply to ss but I have no idea how the print statements work. What does incrementing an entire array do?
Code:
char ss[5] = {'i','m','a','g','e'};
char *s = ss;

s[1]='c';
cout << "s++ is " << s++ << endl;
cout << "*s++ is " << *s++ << endl;
cout << "++*s++ is " << ++*s++ << endl;
cout << "s is " << s << endl;
 

Eridani

Member
So I am doing a C++ module (been programming in Java uptil this point) on my course and need some help. I don't understand how this piece of code prints 12 instead of 20.
Code:
#define func(x) (x*x)-x
....
int i=2;
cout << "func(i+3) is " << func(i+3) << endl;
As for the code below, I understand that s is a pointer to ss so whatever changes made to s also apply to ss but I have no idea how the print statements work. What does incrementing an entire array do?
Code:
char ss[5] = {'i','m','a','g','e'};
char *s = ss;

s[1]='c';
cout << "s++ is " << s++ << endl;
cout << "*s++ is " << *s++ << endl;
cout << "++*s++ is " << ++*s++ << endl;
cout << "s is " << s << endl;

Been a while since I've done C++, but I'm pretty sure your func(i+3) expands to (2+3*2+3)-2+3, which is 12 (if I'm right, #define func(x) ((x)*(x))-(x) should give you 20). You basically need to be really careful when you use macros to define functions, otherwise weird things happen.
 
So I am doing a C++ module (been programming in Java uptil this point) on my course and need some help. I don't understand how this piece of code prints 12 instead of 20.
Code:
#define func(x) (x*x)-x
....
int i=2;
cout << "func(i+3) is " << func(i+3) << endl;
Hint first, then spoiler.

Code:
int func(int x) { return x*x - x; }

this code returns 20. What's the difference?

spoiler:

macros are literally text substitution. What happens when you substitute the text "i+3" in the original statement? You get:

x*x - x
i+3*i+3 - i+3

Now order of operations kicks in and you get 2 + 3*2 + 3 - 2 + 3 = 2 + 6 + 3 - 2 + 3 = 12

Note that if you still want to use a macro for some bizarre reason, you can write it like this:

#define func(x) (x)*(x) - (x)

and then it will return the correct value.

As for the code below, I understand that s is a pointer to ss so whatever changes made to s also apply to ss but I have no idea how the print statements work. What does incrementing an entire array do?
Code:
char ss[5] = {'i','m','a','g','e'};
char *s = ss;

s[1]='c';
cout << "s++ is " << s++ << endl;
cout << "*s++ is " << *s++ << endl;
cout << "++*s++ is " << ++*s++ << endl;
cout << "s is " << s << endl;

s is not an array, it is a pointer to the first element of an array. pointer math works like this: Suppose you have

Code:
T *foo = /* whatever */;
// foo is actually a variable which contains an unsigned integral-valued memory address.
foo = foo + 1;
// the value of foo contains a new unsigned integral-valued address which is exactly sizeof(T) greater than before.

So when you say s++, all you are doing is incrementing the address stored in the pointer variable. In other words, it points to the next item in the array. Since sizeof(char) == 1, the next item in the array is exactly 1 byte past the previous item. In fact, indexing an array as such: ss[3] is exactly the same as writing *(ss + 3).

So the code works like this:

Code:
// s points to the following bytes: {'i', 'm', 'a', 'g', 'e'};

s[1]='c';
// Now s points to the following bytes: {'i', 'c', 'a', 'g', 'e'};

cout << "s++ is " << s++ << endl;
// this will print the pointer value (i.e. the memory address of the first item of the array)
// Now s points to the following bytes: {'c', 'a', 'g', 'e'}

cout << "*s++ is " << *s++ << endl;
// this will dereference the pointer and print the dereferenced value, then bump the address.
// Thus this will print the letter 'c'
// s now points to the bytes: {'a', 'g', 'e'};

cout << "++*s++ is " << ++*s++ << endl;
// postfix ++ has the highest operator precedence, and then prefix and pointer dereference are the same.  
// So this is the same as: ++(*(s++)).  Perhaps it helps to write this as multiple statements:
// char *s2 = s;
// ++s;
//     s now points to the bytes: {'g', 'e'}
// char &c2 = *s2;
//     c now is a reference to the character 'a'
// ++c2;
//     changes 'a' to 'b'
// So this prints 'b', and now s points to the bytes: {'g', 'e'}

cout << "s is " << s << endl;
// prints the value printed by the first line, plus 3.
 
Thanks for the help guys, really appreciate it. Still kinda confused about the third print statement though. The order of precedence is ++(*(s++)) but you do ++s first?
 

Koren

Member
Thanks for the help guys, really appreciate it. Still kinda confused about the third print statement though. The order of precedence is ++(*(s++)) but you do ++s first?
No, he does s++ first, it's just he has written incrementation ++s.

Maybe you should read things like this:
Code:
char* s2 = s;
s = s + 1;
 
Thanks for the help guys, really appreciate it. Still kinda confused about the third print statement though. The order of precedence is ++(*(s++)) but you do ++s first?

S++ means "increment S but return the old value". This is the same as saying "save the old value off somewhere and then increment s, and do all subsequent operations on the saved value". So that's whi wrote ++s, because at that point I already saved the value and won't be using S anymore
 

ZBR

Member
So for my first program I can't seem to figure out how to implement this part of the problem:

"Finally, add code to your program that will determine how many units of 100 miles the package is being shipped(remember that parts of 100 count as a full 100, so 300 is 3 units while 301 is 4 units of 100). Output how many ”parts of 100" you have so you know if your program is calculating how many units of 100 there are correctly. This output is a
debugging step and should be removed before submitting the program. Multiply this number by the rate you determined in step 7 and output the result."

So that I can multiply it by the rate for how much the package weighs.

Second problem I have is for another program.

I was supposed to create random birthdays for everyone in a stadium (18,000 people). I got that done. The problem I'm having is with trying to find the max people with the same birthday and the minimum people with the same birthday. So essentially I have to search the array, then print the max and min of the array followed by the corresponding index (day number) that those two values are in.

Sorry if it all seems kind of hectic, I feel like I haven't slept in a couple days.

Thanks,
ZBR

Edit: This is for Java btw.
 

compo

Banned
So for my first program I can't seem to figure out how to implement this part of the problem:

"Finally, add code to your program that will determine how many units of 100 miles the package is being shipped(remember that parts of 100 count as a full 100, so 300 is 3 units while 301 is 4 units of 100). Output how many ”parts of 100" you have so you know if your program is calculating how many units of 100 there are correctly. This output is a
debugging step and should be removed before submitting the program. Multiply this number by the rate you determined in step 7 and output the result."

So that I can multiply it by the rate for how much the package weighs.

Second problem I have is for another program.

I was supposed to create random birthdays for everyone in a stadium (18,000 people). I got that done. The problem I'm having is with trying to find the max people with the same birthday and the minimum people with the same birthday. So essentially I have to search the array, then print the max and min of the array followed by the corresponding index (day number) that those two values are in.

Sorry if it all seems kind of hectic, I feel like I haven't slept in a couple days.

Thanks,
ZBR

Edit: This is for Java btw.

The first question is testing your knowledge of the modulus operator. The modulus operator (%) performs division, but returns the remainder, instead of the normal result. For example, 545 % 100 will return 45, since 545 / 100 is 5 with a remainder of 45. So for this question, you have to divide the total number of miles by 100 get to your initial answer for how many units of miles there are. Then, you have to figure out how to use the modulus operator to determine whether you should add 1 to your initial answer.

For the second question, you have to use an array to store your birthday counts. So for example, birthdayCounts[25] will return the number of people who have a birthday on the 26th day of the year. Then, there will be two steps to this problem: filling the birthday counts array with random birthdays, and looping through the birthday counts to find the max and min. One tip I can give you is to think about birthdays as integers from 0 to 364. When you generate a random birthday for each person, you don't have to create a Date object. Just create a random integer from 0 to 364. Also, when you loop through the array to find the max and min birthday, you will need to use 4 integer variables to keep track of the max and min birthdays: maxCount, maxIndex, minCount, minIndex.

Hopefully that helps. I'm not going to explicitly give you the answers, but those are some pretty heavy hints.
 

Antagon

Member
The first question is testing your knowledge of the modulus operator. The modulus operator (%) performs division, but returns the remainder, instead of the normal result. For example, 545 % 100 will return 45, since 545 / 100 is 5 with a remainder of 45. So for this question, you have to divide the total number of miles by 100 get to your initial answer for how many units of miles there are. Then, you have to figure out how to use the modulus operator to determine whether you should add 1 to your initial answer.

For the second question, you have to use an array to store your birthday counts. One tip I can give you is to think about birthdays as integers from 0 to 364. When you generate a random birthday for each person, you don't have to create a Date object. Just create a random integer from 0 to 364. Then, you will have to increment the count of that birthday in your birthday counts array (hint: the random birthday integer can be used as the index for that array). Finally, when you loop through the array to find the max and min birthday, you will have to loop through the entire array, and you will need to use 4 integer variables to keep track of the max and min birthdays: maxCount, maxIndex, minCount, minIndex.

Hopefully that helps. I'm not going to explicitly give you the answers, but those are some pretty heavy hints.

Instead of the moduleus, you can also use the Math class, ie :
(int) Math.ceil(((double) 545) / 100)

Strangely enough, there's a Math.floorDiv method that takes two ints and returns an int, but no Math.ceilDiv.
 

compo

Banned
Instead of the moduleus, you can also use the Math class, ie :
(int) Math.ceil(((double) 545) / 100)

Strangely enough, there's a Math.floorDiv method that takes two ints and returns an int, but no Math.ceilDiv.

Yeah, that's true. The ceiling method will explicitly return the answer. Since this was a CS 101 question, then I assumed OP was learning about modulus, so I answered the question that way.
 
Instead of modulus or ceil, you can also take advantage of the fact that integer divisions return the floor of the division. By adding a positive constant to the input value before you divide, you can calculate the final number of units directly:

Code:
function calculate_units(int miles) {
   return (miles + CONSTANT) / 100;
}

The constant that you have to add is something you should be able to figure out yourself, if you think about it. This works for positive numbers, like in your problem.
 

Koren

Member
Strangely enough, there's a Math.floorDiv method that takes two ints and returns an int, but no Math.ceilDiv.
Well, for positive values at least, CeilDiv(x, y) is FloorDiv(x-1, y)+1...

So it's not really that useful. Having to many variations of functions in librairies isn't the best idea to me.

FloorDiv (or integer division, which is about the same) is there because you need an integer division, and it's usually an instruction in virtually all assembly languages (IDIV / DIV on x86, DIV / DIVU on MIPS, etc.). On the other hand, I don't remember seeing a CeilDiv instruction in assembly, so it would be written with DEC > DIV > INC or something like this anyway.
 

ZBR

Member
Thank you very much guys! I was able to get the assignments turned in! I got a C+ in the class, so I am kind of disappointed in myself but I'm going to do an Udacity course over the summer to get a better foundation for Java and if I'm not happy with my GPA later on, I'll retake the course.
 
Waiting on my final grade. There were six programming assignments and two exams. Three programming assignments really kicked my ass and I know lots here helped me so thank you, especially Koren. Definitely the toughest course I've ever taken. I've only been programming for about a year and somehow I got into a 600 level Algorithms course. It really crushed me.
 

Kelsdesu

Member
Hey everyone,

I have a project in Java that involves the user clicking on an image and getting the name of the State clicked and I have a question regarding mapping of coordinates in swing.

If I had a png image of the continental US and wanted to map each state individually; how would I go about it?

Do I place an on-click listener on each state, or maybe use google api.


This is the image I would like to use:
1243467_orig.png


I cant seem to find any documentation on this.
 
Hey everyone,

I have a project in Java that involves the user clicking on an image and getting the name of the State clicked and I have a question regarding mapping of coordinates in swing.

If I had a png image of the continental US and wanted to map each state individually; how would I go about it?

Do I place an on-click listener on each state, or maybe use google api.


This is the image I would like to use:
http://www.cccollegeconsultants.com/uploads/6/7/0/2/67027751/1243467_orig.png[/img

I cant seem to find any documentation on this.[/QUOTE]

Either each state would have to be its own UI element with its own onClick, or you'd have to act based on the coordinate where the user clicks.
 

Koren

Member
If I had a png image of the continental US and wanted to map each state individually; how would I go about it?

Do I place an on-click listener on each state, or maybe use google api.

Either each state would have to be its own UI element with its own onClick, or you'd have to act based on the coordinate where the user clicks.
Since you need to store the image anyway, with the coordinates, you can simply read the color from the image, and get the result from this. It's faster, possibly more precise (and simpler) than any method relying on polygons or anything like this.

Better: use steganography with the image... Encode the # of each state in the lower bits of each color.

You'll need a lossless compression for the image, but that's not a problem for images with homogeneous areas.

It's an old trick... I mean, even the first civilization already used this trick to know which cell you were clicking.


Waiting on my final grade. There were six programming assignments and two exams. Three programming assignments really kicked my ass and I know lots here helped me so thank you, especially Koren. Definitely the toughest course I've ever taken. I've only been programming for about a year and somehow I got into a 600 level Algorithms course. It really crushed me.
It's always a pleasure to help people eager to do their best... You're very welcome!
 

Kelsdesu

Member
Either each state would have to be its own UI element with its own onClick, or you'd have to act based on the coordinate where the user clicks.

Since you need to store the image anyway, with the coordinates, you can simply read the color from the image, and get the result from this. It's faster, possibly more precise (and simpler) than any method relying on polygons or anything like this.

Better: use steganography with the image... Encode the # of each state in the lower bits of each color.

You'll need a lossless compression for the image, but that's not a problem for images with homogeneous areas.

It's an old trick... I mean, even the first civilization already used this trick to know which cell you were clicking.


Thank you both for the GREAT advice. You have sent me on the right path for sure. I ended up also discovering a possible solution with XML .
 
Are there any good resources on writing custom allocators? The extent of my creativity is "have arenas for the most common types and their arrays".
 
Do you have a specific problem you're trying to solve or do you just want like a survey of interesting allocators?

I think a survey would be most helpful, yeah. No specific problem. Considered it for video game use but optimizing an allocator has come up every so often. I'm mostly interested in what problem space constraints lead to what kind of optimizations.
 
Next month I'm going to be starting my BSIT program at a new University, I can't wait. In the mean time, I want to get my feet wet. I got a little familiar with C++ last year, but I wanna try out some other languages to find out what would be a good starting point for me. Are there any articles/guides/books that compare the pros and cons of different languages for a beginner?
 

Jokab

Member
Next month I'm going to be starting my BSIT program at a new University, I can't wait. In the mean time, I want to get my feet wet. I got a little familiar with C++ last year, but I wanna try out some other languages to find out what would be a good starting point for me. Are there any articles/guides/books that compare the pros and cons of different languages for a beginner?

I don't have a guide, but I would suggest Java/C# (doesn't really matter) for general-purpose programming, Python for helpful tools and smaller scripts, and Javascript with Node.js for web work.
 

Koren

Member
Trying to install a Linux dualboot onto an Inspiron was a real pain in the ass.
I simply gave up on dual boots, especially on laptops. But even installing Linux alone is sometimes annoying as hell (or virtually impossible).

I hate UEFI and everything hardware makers do to basically support Microsoft.
 
I simply gave up on dual boots, especially on laptops. But even installing Linux alone is sometimes annoying as hell (or virtually impossible).

I hate UEFI and everything hardware makers do to basically support Microsoft.

Yeah, I was going at it for hours yesterday and only mananged to make it work because it hit me that my weird ass bios wanted me to do the opposite of literally every tutorial wanted me to do and keep safeboot ON during installation of the boot usb, then turn it off if I ever wanted to actually go past the sign-in page of the Linux OS.

I definitely had a feeling that I was being flat out trolled by the hardware maker.
 

Anduril

Member
I'm pretty noobish at programming, but I'm working on an Android app in Java and could use some help.

I have an arraylist of custom objects I managed to save to SharedPreferences via Gson (yay whole yesterday's evening for figuring that out), but now I'd like to implement a save/load function for these arraylists, making it possible to save a list of objects with a name and a date/time. How would I go about doing that? Can you save serialized objects in SQLite? An easier way I'm not thinking of?
 
Wondering if anyone can help me with my C# application. My first C# work...ever. Anyway, I am simply trying to do this:

I am connecting to a database that I constructed. One of my columns holds Integer values for all of the rows. However, these values need to be modified. I am doing some bit shifting. I created a simple class that will do the bit shifting. I need the values of this existing column to be bit-shifted. So, I want to, of course, loop through all of the rows and push these values to my function and update my database.

The problem is, my loop is not pushing down to the next row. I am nearly 100% certain it is my variable declaration but I am not sure what I am doing wrong. My method looks something like:

Code:
int extractedValue = 0;
String query = "SELECT dType FROM dTypeTable";
OdbcDataReader reader = null;
reader = command.ExecuteReader(query, conn);
while (reader.Read()) 
{
BitShifter shifter = new BitShifter() // my class that will bit shift
extractedValue = (int) reader["dValue"];
shifter.setFirstSet(extractedValue);
..
..
..

Basically, extractedValue will be assigned to the existing value of the row. Then, I will pass this value to the function that will be shift the bits. This works fine. The problem is, this value will be set for ALL of the rows of the database, rather than taking this variable assignment one row at a time.

I am not sure why this is a problem, considering a new BitShifter is instantiated for every iteration.

Any help would be appreciated!
 

Lister

Banned
Wondering if anyone can help me with my C# application. My first C# work...ever. Anyway, I am simply trying to do this:

I am connecting to a database that I constructed. One of my columns holds Integer values for all of the rows. However, these values need to be modified. I am doing some bit shifting. I created a simple class that will do the bit shifting. I need the values of this existing column to be bit-shifted. So, I want to, of course, loop through all of the rows and push these values to my function and update my database.

The problem is, my loop is not pushing down to the next row. I am nearly 100% certain it is my variable declaration but I am not sure what I am doing wrong. My method looks something like:

Code:
int extractedValue = 0;
String query = "SELECT dType FROM dTypeTable";
OdbcDataReader reader = null;
reader = command.ExecuteReader(query, conn);
while (reader.Read()) 
{
BitShifter shifter = new BitShifter() // my class that will bit shift
extractedValue = (int) reader["dValue"];
shifter.setFirstSet(extractedValue);
..
..
..

Basically, extractedValue will be assigned to the existing value of the row. Then, I will pass this value to the function that will be shift the bits. This works fine. The problem is, this value will be set for ALL of the rows of the database, rather than taking this variable assignment one row at a time.

I am not sure why this is a problem, considering a new BitShifter is instantiated for every iteration.

Any help would be appreciated!

Can you post how you are setting the values in the database? also, there shouldn't be a need or desire to instantiate that class every iteration.

Instantiate it somewhere else (preferably outside of this object entirely and pass it in via dependency injection), and just call the method you need each iteration.
 
Can you post how you are setting the values in the database? also, there shouldn't be a need or desire to instantiate that class every iteration.

Instantiate it somewhere else (preferably outside of this object entirely and pass it in via dependency injection), and just call the method you need each iteration.

I have another method that will do..

String query = "UPDATE dType FROM dTable WHERE dwValue = ?";
And then I am passing ("?", extractBit())

The extractBit method is the snippet I posted earlier.
 

uhnomoli

Neo Member
Next month I'm going to be starting my BSIT program at a new University, I can't wait. In the mean time, I want to get my feet wet. I got a little familiar with C++ last year, but I wanna try out some other languages to find out what would be a good starting point for me. Are there any articles/guides/books that compare the pros and cons of different languages for a beginner?

How familiar is a little?

If you're still learning basic programming concepts, I'd recommend sticking with C++ and trying to get more experience programming in general. It is more beneficial to learn the basic concepts first, as once you have, switching to another language is usually a matter of learning syntax and the standard library.

If you feel comfortable in C++ rather than just trying out new languages, I'd recommend exposing yourself to different paradigms such as OOP or functional programming. I'm not very familiar with C++, but I believe it supports both of these so you wouldn't necessarily have to move to a different language, but if you wanted to you could try OOP in Python or functional programming in Haskell.

Programming is all about finding the right tool for the problem. I like to think of programming languages as home improvement stores. There are many stores (languages) and they all sell tools (paradigms). Some tools you can find in every store, but to get to Home Depot you have to get on the highway for several miles and depending on traffic... Maybe the mom-and-pop store right down the road has what you need. Learning about all of the tools and what jobs each one is suited for or just gaining more experience using the tools you do know about is a lot more productive than knowing what aisle the wrenches are in at Home Depot and Lowes. Not a perfect analogy, but I feel it illustrates the point.
 
Seems good. Just work out a (wildly) better approach or was it poorly implemented before?

Both. A combination of algorithmic improvements and micro optimizations. Changing a hash function to a faster one was a 15-20% speedup. Then i realized that every time the hash table grows, every item needs to be rehashed. So i started caching hash values. That was good for another 20-30%. Then i realized a bunch of unnecessary shit was going into the table. That one was worth an additional 70%. Then some micro optimizations were worth another 50%.

I can probably get even more, new stuff keeps showing up on the profile
 

Koren

Member
Great job...

I wasn't understanding the "three orders of magnitude" before thinking it was in base 2 ^_^

Out of curiosity, how do you cache hashing values? Mostly to identify the object so you know it has been hashed before? I guess it's not a unique param since you could hash the param instead...

The only solution I found (haven't searched a lot though) was to store the hash in the object (or use an encapsulating class with hash and object)
 
Great job...

I wasn't understanding the "three orders of magnitude" before thinking it was in base 2 ^_^

Out of curiosity, how do you cache hashing values? Mostly to identify the object so you know it has been hashed before? I guess it's not a unique param since you could hash the param instead...

The only solution I found (haven't searched a lot though) was to store the hash in the object (or use an encapsulating class with hash and object)

Yea, instead of storing Xs in your hash table, store pointers to some object that contains X along with an optional hash value. The hash function then either computes hash of X and saves it in the encapsulating record or returns the previously computed hash. Without this you essentially get N^2 hash computations as the table continually grows and triggers full rehashes
 

Zoe

Member
Code:
int extractedValue = 0;
String query = "SELECT dType FROM dTypeTable";
OdbcDataReader reader = null;
reader = command.ExecuteReader(query, conn);
while (reader.Read()) 
{
BitShifter shifter = new BitShifter() // my class that will bit shift
extractedValue = (int) reader["dValue"];
shifter.setFirstSet(extractedValue);
..
..
..

Basically, extractedValue will be assigned to the existing value of the row. Then, I will pass this value to the function that will be shift the bits. This works fine. The problem is, this value will be set for ALL of the rows of the database, rather than taking this variable assignment one row at a time.

I am not sure why this is a problem, considering a new BitShifter is instantiated for every iteration.

Any help would be appreciated!

I have another method that will do..

String query = "UPDATE dType FROM dTable WHERE dwValue = ?";
And then I am passing ("?", extractBit())

The extractBit method is the snippet I posted earlier.

This is really hard to interpret because your vagueness seems to be causing your syntax to be off, but are you sure your update query is valid?
 
Here is my code. Same problem as before, the values are being overwritten to whatever the last row's value is.

Example:

If my initial column is:
1
2
3
4

And I am writing a method, say to multiple the existing values by 2 and update the table, my result set should be
2
4
6
8

But I am getting
8
8
8
8

This is my method as it stands:
Code:
private void extractBitsFromWordAndUpdate() 
{
  if(this.con.State != ConnectionState.Open) 
  {
    this.conn.Open(); 
   }
   PacketHelper helper = null; 
   String query = "SELECT device FROM dTable";
   OdbcCommand command = new OdbcCommand(query, this.conn);
    OdbcDataReader reader = null;
    reader = command.ExecuteReader();
    while(reader.Read())
     {
       helper = PacketHelper();
       int extractedVal = Convert.toInt32(reader["device"]); // col name
       helper.assembleWord(extractedVal); // all this does is call a getter() for a member variable
       }
       reader.Close();
       String update = "UPDATE dTable SET device = {0}",
       helper.getKeyPad()); // what I want to change the value to. This method is fine. 
       OdbcCommand commandTwo = new OdbcCommand(update, this.conn);
       commandTwo.ExecuteNonQuery();
       this.conn.Close();
}
 }

Again, written in C# and the problem is the rows are only updating based on the last row's value.
 
Top Bottom