Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
Been doing some work in Maple the last few days. Maple has a thing I haven't seen elsewhere, where you can define "sequences" (comma-separated lists of Maple values) and assign a sequence to a variable. So you can write

code:
x := 6, 7, 8, 9;
f(x);
and it has the same effect as writing

code:
f(6, 7, 8, 9);
because in passing x in to f, you actually passed 4 variables, because x is a sequence of 4 values.

Is there a name for this kind of behaviour?

Adbot
ADBOT LOVES YOU

coaxmetal
Oct 21, 2010

I flamed me own dad
Not sure. You can do it in python too though with slightly different syntax.

code:
args = (1,2,3)
func(*args)
is equivalent to
code:
func(1,2,3)

Zombywuf
Mar 29, 2008

Hammerite posted:

Is there a name for this kind of behaviour?

Flattening? Unpacking?

It's what happens when you pass arrays to functions in Perl.

ToxicFrog
Apr 26, 2008


JawnV6 posted:

gcc's being lazy about the stack. It's compiling a function and after the args are consumed it just slaps new data into [esp] and [esp+4] before calling another function. Are there any flags that will force it to play nice and actually push the args?

I'm doing some inline assembly that's getting mixed up and couldn't find any way to express "hey this is pushing something on the stack don't touch it". Right now I'm just modifying the machine code once it's done but that's annoying and I'd rather avoid it entirely.

That might be sibling call optimization (which permits it to reuse the stack frame when tail-calling a function with the same stack space requirements as the caller); perhaps -fno-optimize-sibling-calls?

If that doesn't work, I have no suggestions, but you can always try turning down the optimization settings until the problem goes away, then looking at the list of specific optimizations enabled by the setting you just turned down and doing a binary search on them to find the specific one causing the problem.

Mr. Crow
May 22, 2008

Snap City mayor for life

Strong Sauce posted:

First you should understand that Rails is a full-stack web framework built on Ruby and node.js is a collection of system functions/tools based in javascript designed for event-driven high-scale applications. Right now I believe the most popular web framework for node.js is Express.

If you are doing this in order to add to your list of skills in your resume (as in you want to use the most common languages/web frameworks), I would consider Django, then Rails, then node.js. I'm mainly doing Ruby now (not much Rails) and a lot of jobs are primarily looking for Python and Django developers. Not that there isn't positions with Rails/Ruby and Nodejs but from my own personal search these past couple of months there are a lot of people looking for Django and/or Python developers.

If you're just doing this to learn, then use whatever language you want. Express and node.js are relatively stable but for some libraries there is no definitive or even top 3 that you can choose from. A lot of it will rely on you evaluating the libraries yourself since nodejs is still relatively young. Rails and Ruby are pretty mature projects with lots of documentation and examples with only a couple of libraries for each specific category so if you need to choose one you can just use Google and read other people's evaluation of it.

In the end I think there is a future for node.js and Python and Ruby so just do the one you enjoy doing the most. When it comes time to getting a job employers will probably care more that you did something, even if its not in their preferred choice of stack/language.

Python has always rubbed the wrong way for some reason, and it's a combination of Job Skill/personnel project. Guess I'll go with Rails since I'm going to be learning everything from the ground up, don't want to frequently get into the situation where there's no documentation on what I want to know.

Thanks for the input!

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Hammerite posted:

Been doing some work in Maple the last few days. Maple has a thing I haven't seen elsewhere, where you can define "sequences" (comma-separated lists of Maple values) and assign a sequence to a variable. So you can write

code:
x := 6, 7, 8, 9;
f(x);
and it has the same effect as writing

code:
f(6, 7, 8, 9);
because in passing x in to f, you actually passed 4 variables, because x is a sequence of 4 values.

Is there a name for this kind of behaviour?

In R, we refer to that as vectorization. That's not exactly correct usage, but it's definitely a similar idea.

Edit: It's probably better to say that this isn't exactly what people in parallel computing refer to as vectorization, but it's a good word for it.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
The way you do certain things in Maple is... idiosyncratic. It is entertaining from time to time.

As well as sequences (comma-separated lists of values) you also have lists, which are sequences encased in a set of square brackets; and sets, which are sequences encased in a set of braces. So you can have

code:
x := 1, 2, 3;
y := [4, 5, 6];
z := {7, 8, 9};
and x is a sequence, y is a list and z is a set. You can go from a list or set to the sequence contained within using the "op" function (short for "operands", I think; it splits expressions of various sorts into their component parts).

There is no built-in function to append an element to a list. You can do that as follows:

code:
x := [1, 2, 3];
y := [op(x), 4];
z := [0, op(x)];
and then y will be the list [1, 2, 3, 4] and z will be the list [0, 1, 2, 3].

JawnV6
Jul 4, 2004

So hot ...

ToxicFrog posted:

That might be sibling call optimization (which permits it to reuse the stack frame when tail-calling a function with the same stack space requirements as the caller); perhaps -fno-optimize-sibling-calls?
No change with this option.

ToxicFrog posted:

If that doesn't work, I have no suggestions, but you can always try turning down the optimization settings until the problem goes away, then looking at the list of specific optimizations enabled by the setting you just turned down and doing a binary search on them to find the specific one causing the problem.
I haven't specified a -O arg, and -O0 didn't change the output.

The particular call is in a #define but that shouldn't change calling conventions, right? Something like this
code:
#define macroLabel( pointer ) \
if( ( pointer )->property ) \
{ \
functionCall(pointer, arg2) \
}

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
The convention is that the argument space is scratch space and can be used by the callee for whatever the hell it wants. If you're relying on the arguments not being touched, you are relying on promises not given.

JawnV6
Jul 4, 2004

So hot ...

rjmccall posted:

The convention is that the argument space is scratch space and can be used by the callee for whatever the hell it wants. If you're relying on the arguments not being touched, you are relying on promises not given.

Great, thanks for a load of theory bullshit that doesn't affect my problem or offer insight towards an answer. Got any thoughts on RISC/CISC while we're out here in the theoretical wastelands?

coaxmetal
Oct 21, 2010

I flamed me own dad

JawnV6 posted:

Great, thanks for a load of theory bullshit that doesn't affect my problem or offer insight towards an answer. Got any thoughts on RISC/CISC while we're out here in the theoretical wastelands?

RISC is going to change everything.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
"I have a square peg and I am inserting it in a round hole. Why is this a pain in the rear end?"
"Round holes are designed for round pegs. A square peg won't work in a general case."
"gently caress you. I HAVE a square peg and I'm putting it in the motherfucker. I don't care about theory."

pseudorandom name
May 6, 2007

JawnV6 posted:

Great, thanks for a load of theory bullshit that doesn't affect my problem or offer insight towards an answer. Got any thoughts on RISC/CISC while we're out here in the theoretical wastelands?

Your problem is that you're modifying the stack pointer in your inline assembly which (to my knowledge) gcc doesn't allow.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

JawnV6 posted:

Great, thanks for a load of theory bullshit that doesn't affect my problem or offer insight towards an answer. Got any thoughts on RISC/CISC while we're out here in the theoretical wastelands?

As far as I know, GCC never generates PUSH/POP instructions to deal with the stack on x86 architectures. It always prefers to track local values by their absolute offset from the stack frame, because that happened to be a much easier way to write the compiler and it's allowed by the calling convention. I admit it's been a while since I poked around inside GCC, but I think you're just out of luck if you want it to behave that way.

Maybe try putting your assembly stuff in an inline function?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

JawnV6 posted:

Great, thanks for a load of theory bullshit that doesn't affect my problem or offer insight towards an answer. Got any thoughts on RISC/CISC while we're out here in the theoretical wastelands?

I did miss that this is inline assembly instead of a separate function coded in assembly, so I will graciously cut you some slack. GCC does not make any promises at all about the layout of the frame, including the arguments area. You need to use inline assembly constraints to tell GCC that you're using values from the local context.

JawnV6
Jul 4, 2004

So hot ...

Internet Janitor posted:

"I have a square peg and I am inserting it in a round hole. Why is this a pain in the rear end?"
"Round holes are designed for round pegs. A square peg won't work in a general case."
"gently caress you. I HAVE a square peg and I'm putting it in the motherfucker. I don't care about theory."
I'm sorry, I thought my original question was quite clear in what I was trying to do. I'm having difficulty with how gcc is carving out the stack for a call. That really has nothing to do with caller/callee argument conventions and bringing up a basic topic in such a patronizing manner wasn't the least bit helpful.

More to the point, I'm certain you don't have a firm enough grasp on the discussion to be throwing stones.

ShoulderDaemon posted:

As far as I know, GCC never generates PUSH/POP instructions to deal with the stack on x86 architectures. It always prefers to track local values by their absolute offset from the stack frame, because that happened to be a much easier way to write the compiler and it's allowed by the calling convention. I admit it's been a while since I poked around inside GCC, but I think you're just out of luck if you want it to behave that way.

Maybe try putting your assembly stuff in an inline function?
It does generate some push/pop instructions, as well as leave without doing an enter prior with stack manipulation (:argh:). But all that is around the function beginning and ending. You're right, it's just subtracting out the biggest space it will need and using absolute references.

rjmccall posted:

I did miss that this is inline assembly instead of a separate function coded in assembly, so I will graciously cut you some slack. GCC does not make any promises at all about the layout of the frame, including the arguments area. You need to use inline assembly constraints to tell GCC that you're using values from the local context.
I'm familiar with inline constraints to use local variables. But I'm not using anything local in the asm. I just need space on the stack to remain untouched, which it doesn't look to support.

pseudorandom name posted:

Your problem is that you're modifying the stack pointer in your inline assembly which (to my knowledge) gcc doesn't allow.
I'm pretty sure I can work around it regardless of that limitation. It just seemed like there'd be a way to get it to emit the stack-based instructions for function calls as well.

pseudorandom name
May 6, 2007

JawnV6 posted:

I'm pretty sure I can work around it regardless of that limitation. It just seemed like there'd be a way to get it to emit the stack-based instructions for function calls as well.

Why? It's the most efficient way to allocate a stack frame.

And there's nothing wrong with using LEAVE without ENTER, I'm not sure why you'd be upset about that either.

Basically, you should probably step back and reconsider what you're trying to do, because from here it sounds like you're trying to solve the wrong problem.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
You've gotten patronized because you're communicating poorly and then being rude to people who misunderstand you, and because your approach reeks of a very rookie mistake with inline asm, which is to try to work around the compiler's code emission patterns instead of adequately expressing your intent to the compiler. Also, this is a very standard code-emission pattern for calls, so acting surprised like GCC is misbehaving somehow does not really give you much credibility.

Okay. You have an inline asm block which wants to access the stack. My initial guess was that you were trying to access your procedure arguments "directly". My second guess was that you were trying to access some other local variable. Apparently neither of those is true. My third guess is that you're trying to communicate between multiple inline asm blocks (or multiple executions of the same block). If that's not it, my fourth guess is going to be that you're trying to pass extra stack arguments to a call that GCC is otherwise emitting for you. If that's not it, my fifth guess is going to be that you're trying to set extra information in some canonical place in the frame for the use of some stack-crawling tool. At some point, maybe you should consider letting us in on the secret, because what you need to do varies a lot on what you're trying to do.

JawnV6
Jul 4, 2004

So hot ...

pseudorandom name posted:

Why? It's the most efficient way to allocate a stack frame.
Yeah, the compiler I wrote did it that way as well. But since then I've been doing a shitload of pure asm coding so I wasn't as quick to pick up on that as I should've been.

pseudorandom name posted:

And there's nothing wrong with using LEAVE without ENTER, I'm not sure why you'd be upset about that either.
The stack frame is manually adjusted, then leave; ret. It's weird to see one and not the other. Checking back in the PRM it's not explicitly warned against as was my impression, but it still doesn't look right.

rjmccall posted:

You've gotten patronized because you're communicating poorly and then being rude to people who misunderstand you, and because your approach reeks of a very rookie mistake with inline asm, which is to try to work around the compiler's code emission patterns instead of adequately expressing your intent to the compiler.
My problem's solved for the moment and this work is more in the proof-of-concept space so the relative cleanliness doesn't matter. This is a third party code base that works perfectly well on ARM and x86 with another compiler but I'll be sure to pass your 'rookie' assessment onto the authors. There is literally no way to adequately express this intent to gcc, but please keep telling me to do that.

I'm seriously not too concerned with your understanding of my problem. You haven't once given my posts an honest reading and now that I've got a working solution my drive to hold your hand through it is gone. Terribly sorry.

pseudorandom name
May 6, 2007

You haven't once explained your problem. You have been a rude rear end in a top hat on multiple occasions, though.

tef
May 30, 2004

-> some l-system crap ->

pseudorandom name posted:

You haven't once explained your problem.

Actually, he did. Hope this helps.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
I can't tell if I'd rather see the actual problem with a solution, or if I'd rather JawnV6 just gently caress off.

pseudorandom name
May 6, 2007

tef posted:

Actually, he did. Hope this helps.

His problem is (paraphrasing) "I'm doing something that isn't allowed under any circumstances for no reason I'm willing explain to anybody, how do I do this thing?!? No, I won't tell you what I'm really trying to do in order for you to help me find a solution that will actually work and do what I want. Shut up you condescending jerk, stop trying to help me."

tef
May 30, 2004

-> some l-system crap ->
"I have a specific problem with assembly and GCC". Shoulderdaemon replied with "Well, try turning off -O, otherwise you're hosed"

The rest of you started off by calling him an idiot for asking a question. Hope this helps.

I am pretty sure I know what being a patronising oval office on the internet looks like. It's how I post.

tef fucked around with this message at 21:56 on Jan 21, 2012

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

pokeyman posted:

I can't tell if I'd rather see the actual problem with a solution, or if I'd rather JawnV6 just gently caress off.

I think I can put it together. He has a function something like this:

code:
void foo() {
  asm("...\n  push blah");
  bar(x, y);
  asm("pop blah\n  ...");
}
There isn't a compiler in the world which is going to guarantee that this works, but you can see how it might happen to work on some specific compiler under specific settings. Now that he's trying to port to GCC, it's breaking. He has a "solution" which apparently rewrites the binary, which is ugly, so he'd like to tell GCC what's happening. However, there isn't actually a way to tell GCC what's happening, because a constraint like this would basically be "by the way, assume nothing about the layout of the stack frame below the local-variables area", and it would require a special code generation mode which reimplements large amounts of the call-site machinery.

The solution is to create a local variable (with alloca if it needs to be variably-sized) and use asm constraints to stash the data in that rather than pushing and popping around the call.

ETA: this is one of those situations where a little pseudocode goes a long way.

rjmccall fucked around with this message at 22:27 on Jan 21, 2012

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

I know jack poo poo about Windows programming. In fact, I know jack poo poo about C++ in general.

However, I'd like to get notifications of audio device changes as described in this MSDN article in a Python program I'm writing.

Is this possible in any way?

Quebec Bagnet
Apr 28, 2009

mess with the honk
you get the bonk
Lipstick Apathy

Thermopyle posted:

I know jack poo poo about Windows programming. In fact, I know jack poo poo about C++ in general.

However, I'd like to get notifications of audio device changes as described in this MSDN article in a Python program I'm writing.

Is this possible in any way?

pywin32 will bridge to COM, but having never used it, I can't say if it's advanced or suitable enough for you to implement a client interface.

The harder, but maybe more sensible option (depending on your needs) would be to implement the functionality you need in C++ (since it's pretty straightforward to work with COM that way) and expose it using Boost.Python.

JawnV6
Jul 4, 2004

So hot ...

pseudorandom name posted:

You haven't once explained your problem.
I apparently explained it with enough fidelity for others to understand, discuss, and answer. That's what really gets me about this chorus of "you didn't explain it," did y'all just completely miss the discussion I had? I've even got a solution (non binary patching) that's working and doesn't interfere with gcc's stack frame.

pseudorandom name posted:

You have been a rude rear end in a top hat on multiple occasions, though.
:confused: And? That hasn't stopped people from going over my posts with a microscope to reconstruct some fictional version of what I'm working on. Maybe in the future don't reward 'rear end in a top hat' behavior with increased fervor for answering questions??

rjmccall posted:

ETA: this is one of those situations where a little pseudocode goes a long way.
It's also a situation where I have enough background knowledge that specific, targeted questions were enough so that I didn't have to go through the legwork of making toy problems for arrogant jerks who are, for reasons beyond their control, compelled to grok every question in this thread to the finest detail.

Impotence
Nov 8, 2010
Lipstick Apathy
Not really a programming question but something I've been curious about for a while

how would you make a large MMO save state?

I mean, you'd be dealing with huge amounts of changes per second that need to be saved (every skill addition, every experience gain, every item move or transfer) that would completely gently caress over iowait if you did them realtime, do you just throw huge arrays of SSDs and hardware at the issue or do you do something like save state in memory then do a mass-dumpout to transaction log/database once every x seconds?

pseudorandom name
May 6, 2007

JawnV6 posted:

I apparently explained it with enough fidelity for others to understand, discuss, and answer. That's what really gets me about this chorus of "you didn't explain it," did y'all just completely miss the discussion I had? I've even got a solution (non binary patching) that's working and doesn't interfere with gcc's stack frame.

You explained your immediate difficulty with the fragile and broken method you use to solve your problem, but have never explained what your actual problem is.

"How do I get gcc to stop being gcc?" isn't your actual problem, your actual problem is whatever your final goal is that your broken inline assembly hack is trying to accomplish.

JawnV6
Jul 4, 2004

So hot ...

pseudorandom name posted:

You explained your immediate difficulty with the fragile and broken method you use to solve your problem, but have never explained what your actual problem is.

"How do I get gcc to stop being gcc?" isn't your actual problem, your actual problem is whatever your final goal is that your broken inline assembly hack is trying to accomplish.

I'm fine with the "broken inline assembly hack" and really don't care to explain the details. I can assure you, I've read the wiki page for the XY Problem and for this specific instance there's really no other way. If not, I'll just live with the shame of adding 6 asm instructions to a flow that used to be 3 that leave the stack untouched for gcc. I just thought there'd be an easier way.

JawnV6 fucked around with this message at 22:21 on Jan 22, 2012

pseudorandom name
May 6, 2007

But how do you know there's no other way if you refuse to explain what the problem is to anybody else who might know an answer? That's the crux of the issue. Worst case scenario, you get people agreeing with you that, yeah, that's the only way to do it and commiserating with the difficulties you've encountered implementing it. Best case, you get an elegant solution that will work without trouble regardless of compiler version changes, optimization flags and whatever else may come up.

Oh, and thanks for bringing up the "XY problem", I've been trying to remember what this was called since this conversation started, and the best I was able to find on Google was the "A-not-B error", which is a development psychology thing.

pseudorandom name fucked around with this message at 22:55 on Jan 22, 2012

The Gripper
Sep 14, 2004
i am winner

Biowarfare posted:

Not really a programming question but something I've been curious about for a while

how would you make a large MMO save state?

I mean, you'd be dealing with huge amounts of changes per second that need to be saved (every skill addition, every experience gain, every item move or transfer) that would completely gently caress over iowait if you did them realtime, do you just throw huge arrays of SSDs and hardware at the issue or do you do something like save state in memory then do a mass-dumpout to transaction log/database once every x seconds?
There's no real answer to this, there's a lot of different ways to do it and all of them have cost/benefit involved that makes them more or less suitable to your problem.

For things I've worked on (not MMOs), we had clear definitions of what was critical and non-critical, as well as thresholds for how much data was an acceptable loss e.g. for an MMO you'd consider absolute position of a character as non-critical as if something bad happens it's low-impact for player to lose [threshold] seconds of movement vs. losing any experience or an item they've gained.

I think most decide to keep everything in memory and write to disk in bulk at a reasonable rate, making sure to have mechanisms to recover/write to disk immediately if there is a fault (Diablo 2 is an example of the other side of this, each game server kept it's state in memory and wrote it periodically, unfortunately if a server/game crashed the state would be lost and up to a few minutes of progress could disappear).

It's something that takes a lot of planning and a lot of developers get it wrong (understandably) by under or overestimating requirements (whether that be the hardware requirements, or the tolerance of it's users to losing an amount of progress if something breaks).

rolleyes
Nov 16, 2006

Sometimes you have to roll the hard... two?
Can we let the JawnV6 stuff go? Yes it's annoying, but at this point you're just getting trolled and genuine questions from people without such an attitude problem are in danger of getting lost in the noise.

Computer viking
May 30, 2011
Now with less breakage.

Biowarfare posted:

Not really a programming question but something I've been curious about for a while

how would you make a large MMO save state?

I mean, you'd be dealing with huge amounts of changes per second that need to be saved (every skill addition, every experience gain, every item move or transfer) that would completely gently caress over iowait if you did them realtime, do you just throw huge arrays of SSDs and hardware at the issue or do you do something like save state in memory then do a mass-dumpout to transaction log/database once every x seconds?

At a guess, you push the rarer events (item gains, achievements, quest flags, experience) to the DB server immediately, while the constant-stream events (positions, mostly) can be polled on a timer. As for how bad it is, hmm. Say you've got a WoW server just after an expansion. They let something like 2000 people on at the time, and I don't think they will kill more than one creature every 10 sec on average. That's 200 exp gain updates/second, plus about the same amount of gold and item updates, and somewhat fewer quest flag updates. If you poll positions every 10 sec as well, you get something like 1000 updates/second. That sounds manageable enough for a decent DB server?

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
Does anybody have a good book recommendation for concurrency and asynchronous programming? I'm hoping to find some structured material that goes beyond semaphores, mutexes, producer/consumer, and basic crap like that. I'm thinking like using tasks, promises/futures, thread pools, testing methodologies, and common ways that different things tend to end up colliding while trying to use a resource.

Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

This should be a simple question but I've just been at work to long.

I'm trying to arrange some images like so:
code:
    1 | 3 | 5 | 7                   1 | 2 | 3 | 4
    -------------     instead of    -------------
    2 | 4 | 6 | 8                   5 | 6 | 7 | 8
What's a good way to iterate through in the Z-pattern? Ideally it would work both ways, so that if I said the image at x,y and it would tell me which # it was, and I could loop through them 1-8 and place them accordingly. Although this would go up to infinity images (well, more like a hundred) so it's not limited to just 8.

Right now I am just going to do 1, 2, 3, 4... and have each of those be a group of 4. I just hate hardcoding stuff. That way if you've touched inside group 1, where in the group was it, area 1 2 3 or 4? I'm just looking for a cleaner or more elegant way that isn't obvious to me right now.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Bob Morales posted:

This should be a simple question but I've just been at work to long.

I'm trying to arrange some images like so:
code:
    1 | 3 | 5 | 7                   1 | 2 | 3 | 4
    -------------     instead of    -------------
    2 | 4 | 6 | 8                   5 | 6 | 7 | 8
What's a good way to iterate through in the Z-pattern? Ideally it would work both ways, so that if I said the image at x,y and it would tell me which # it was, and I could loop through them 1-8 and place them accordingly. Although this would go up to infinity images (well, more like a hundred) so it's not limited to just 8.

Right now I am just going to do 1, 2, 3, 4... and have each of those be a group of 4. I just hate hardcoding stuff. That way if you've touched inside group 1, where in the group was it, area 1 2 3 or 4? I'm just looking for a cleaner or more elegant way that isn't obvious to me right now.

code:
coords :: Int -> (Int, Int)
coords i | odd i     = (i `div` 2 + 1, 1)
         | otherwise = (i `div` 2, 2)

index :: (Int, Int) -> Int
index (n, 1) = n * 2 - 1
index (n, 2) = n * 2
Edit: If you don't speak Haskell...

For converting an image index to coordinates, you first check if the index is odd. If so, the x coordinate is the index, divided by two (round down), plus one. The y coordinate is 1. Otherwise, the x coordinate is the index divided by two, and the y coordinate is 2.

For converting a coordinate pair to an index, you first check the y coordinate. If the y coordinate is 1, then the index is the x coordinate, multiplied by two, minus 1. Otherwise, the index is the x coordinate multiplied by 2.

ShoulderDaemon fucked around with this message at 22:53 on Jan 24, 2012

No Safe Word
Feb 26, 2005

Just add a y offset if it's even?

Pseudocode:
code:
for num 1 through 8
  if num is even
    y = 1
  else
    y = 0
  x = floor((num + 1) /2)
The Python (3.x) version (for fun)
code:
for i in range(1,9):
	if (i % 2):
		y = 0
	else:
		y = 1
	x = (i + 1) // 2
	print(i, x, y)

1 1 0
2 1 1
3 2 0
4 2 1
5 3 0
6 3 1
7 4 0
8 4 1

Adbot
ADBOT LOVES YOU

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



Bob Morales posted:

This should be a simple question but I've just been at work to long.

I'm trying to arrange some images like so:
code:
    1 | 3 | 5 | 7                   1 | 2 | 3 | 4
    -------------     instead of    -------------
    2 | 4 | 6 | 8                   5 | 6 | 7 | 8
What's a good way to iterate through in the Z-pattern? Ideally it would work both ways, so that if I said the image at x,y and it would tell me which # it was, and I could loop through them 1-8 and place them accordingly. Although this would go up to infinity images (well, more like a hundred) so it's not limited to just 8.

Right now I am just going to do 1, 2, 3, 4... and have each of those be a group of 4. I just hate hardcoding stuff. That way if you've touched inside group 1, where in the group was it, area 1 2 3 or 4? I'm just looking for a cleaner or more elegant way that isn't obvious to me right now.

This works but is probably horrible.

code:
values = range(1,19)
height = 3

for row in range(0,height):
	line = []
	for col in range(0,len(values)/height):
		line.append(values[(height*col)+row])
	print ' | '.join([str(x) for x in line])
	print '-------------------------------'
Output:

code:
1 | 4 | 7 | 10 | 13 | 16
-------------------------------
2 | 5 | 8 | 11 | 14 | 17
-------------------------------
3 | 6 | 9 | 12 | 15 | 18
-------------------------------

With your input (values being 1 through 8, height of 2):

1 | 3 | 5 | 7
-------------------------------
2 | 4 | 6 | 8
-------------------------------

Carthag Tuek fucked around with this message at 23:10 on Jan 24, 2012

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply