Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
19 June, 2013, 22:03:25 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: [1]   Go Down
  Print  
Author Topic: Exiting For-loop in a search-subroutine -  (Read 403 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
MGOS
LV5 Advanced (Next: 300)
*****
Offline Offline

Gender: Male
Last Login: Today at 20:43:31
Date Registered: 29 July, 2011, 16:54:53
Location: Germany
Posts: 297


Topic starter
Total Post Ratings: +74

View Profile
« on: 09 December, 2011, 14:27:53 »
0

I have some question for a routine I was about to write:
The function should search the array L1 with the dimension G for the value r1. It should return the index of the first found value, or 0 if not found.

I had these two ideas how to do this, but Is the second legal axe code?
If so, which one is faster?
Do I have to put a space between Return and I?


1
2
3
4
5
6
7
8
9
10
11
12
Lbl S
If G
For (I,1,G)
If {L1+I-1} = r1
Goto F
End
End
End
0->I
Lbl F
Return I


1
2
3
4
5
6
7
8
9
10
Lbl S
If G
For (I,1,G)
If {L1+I-1} = r1
Return I
End
End
End
Return 0
« Last Edit: 09 December, 2011, 15:24:50 by MGOS » Logged

Click those to see more information Smiley
Current   
Projects: 
aeTIos
You got stair'd!
LV12 Extreme Poster (Next: 5000)
************
Offline Offline

Gender: Male
Last Login: Today at 16:47:44
Date Registered: 15 September, 2010, 06:00:00
Location: Netherlands, Overijssel
Posts: 3146


Total Post Ratings: +120

View Profile
« Reply #1 on: 09 December, 2011, 15:39:07 »
0

So what you want to do is stopping the for() loop?
In that case, do this:

1
2
3
4
5
6
for(A,0,G)
blah blah
if condition to stop loop = 1
G->A
end
end
Logged

If something above sounds rude, feel free to vote it down, it was not meant to be rude<<lolol
--Always stay relAXEd!--


Spoiler for Hidden:


[







Spoiler for Still Alive:
This was a triumph.
I'm making a note here: HUGE SUCCESS.
It's hard to overstate my satisfaction.

Aperture Science:
We do what we must because we can.
For the good of all of us
Except the ones who are dead.

But there's no sense crying over every mistake
You just keep on trying till you run out of cake
And the science gets done and you make a neat gun
For the people who are still alive.

I'm not even angry.
I'm being so sincere right now.
Even though you broke my heart and killed me.
And tore me to pieces.
And threw every piece into a fire.
As they burned it hurt because
I was so happy for you.

Now these points of data make a beautiful line
And we're out of beta we're releasing on time.
So I'm GLaD I got burned think of all the things we learned
For the people who are still alive.

Go ahead and leave me.
I think I prefer to stay inside.
Maybe you'll find someone else to help you.
Maybe Black Mesa -
THAT WAS A JOKE. HA HA, FAT CHANCE.
Anyway, this cake is great:
It's so delicious and moist.

Look at me still talking when there's science to do.
When I look out there it makes me GLaD I'm not you.
I've experiments to run there is research to be done
On the people who are still alive

And believe me I am still alive.
I'm doing science and I'm still alive.
I feel FANTASTIC and I'm still alive.
While you're dying I'll be still alive.
And when you're dead I will be still alive.

Still alive
Still alive
MGOS
LV5 Advanced (Next: 300)
*****
Offline Offline

Gender: Male
Last Login: Today at 20:43:31
Date Registered: 29 July, 2011, 16:54:53
Location: Germany
Posts: 297


Topic starter
Total Post Ratings: +74

View Profile
« Reply #2 on: 09 December, 2011, 15:51:55 »
0

Not only. I want to stop the for loop if the condition is true. But the function should return the value the index variable of the loop had when it was stopped.
So lets say the Array has the dimension of 5 and is {1,2,0,9,7} and r1 is 9. Then my function goes through the loop, stops at I = 4 and returns 4.
If I looked for let's say 5, the function would return 0, becouse none of the elements in the array had the value 5.

Both of the functions I posted SHOULD do this, but I don't know which one is better / legal axe code.

I know that array indizes usally start with 0, but starting with 1 has some advantages in my case, so I use that.
« Last Edit: 04 September, 2012, 20:50:18 by MGOS » Logged

Click those to see more information Smiley
Current   
Projects: 
Builderboy
Physics Guru
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: Today at 08:32:53
Date Registered: 20 April, 2009, 00:28:53
Location: Ravenholm
Posts: 5645


Total Post Ratings: +589

View Profile
« Reply #3 on: 09 December, 2011, 20:50:31 »
0

You could use InData() Cheesy It does exactly what you describe, and is a built in Axe command ^^
Logged

jacobly
LV5 Advanced (Next: 300)
*****
Online Online

Last Login: Today at 21:53:51
Date Registered: 09 October, 2011, 01:53:09
Posts: 200

Total Post Ratings: +150

View Profile
« Reply #4 on: 09 December, 2011, 21:07:28 »
0

Well, InData() works with zero terminated data, and his routine works with G bytes of data and embedded zeros.

First of all, {L1+I-1} can be optimized to {I-1+L1}. Other than that, the second one should work fine. Also, the space should be optional.

Edit:
Spoiler for Crazy Optimizations:
Lbl S
Return!If G .returns G if G=0
For(I,1,G)
!If {I-1+L1}-r1
Return I
End
End
.as long as I is not modified inside the for loop, and G >= 1 (true), ans = -1
Return+1
« Last Edit: 09 December, 2011, 21:26:55 by jacobly » Logged
AngelFish
This is my custom title
Administrator
LV12 Extreme Poster (Next: 5000)
*
Offline Offline

Gender: Male
Last Login: Today at 01:08:43
Date Registered: 15 August, 2010, 09:18:54
Posts: 3192


Total Post Ratings: +222

View Profile
« Reply #5 on: 09 December, 2011, 21:26:08 »
0

Isn't this exactly what the CPIR instruction in z80 does?
Logged

∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ
jacobly
LV5 Advanced (Next: 300)
*****
Online Online

Last Login: Today at 21:53:51
Date Registered: 09 October, 2011, 01:53:09
Posts: 200

Total Post Ratings: +150

View Profile
« Reply #6 on: 09 December, 2011, 21:42:29 »
0

Almost. CPIR returns an address, whereas this routine returns an index. Also, this routine is much easier to extend to arrays of words or objects (but I'm not saying that access to CPIR through an axe command wouldn't be cool Wink).
Logged
AngelFish
This is my custom title
Administrator
LV12 Extreme Poster (Next: 5000)
*
Offline Offline

Gender: Male
Last Login: Today at 01:08:43
Date Registered: 15 August, 2010, 09:18:54
Posts: 3192


Total Post Ratings: +222

View Profile
« Reply #7 on: 09 December, 2011, 21:47:15 »
0

An address is pretty easily converted into an index by simply subtracting the base address of the array. As for the flexibility of the routine, yes. However, a single CPU instruction with a single subtraction afterwards is going to be far faster and smaller than anything Axe compiles to.
Logged

∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ
squidgetx
Food.
Coder Of Tomorrow
LV10 31337 u53r (Next: 2000)
*
Offline Offline

Gender: Male
Last Login: Today at 17:53:05
Date Registered: 30 May, 2010, 19:54:18
Location: eating somewhere
Posts: 1849


Total Post Ratings: +480

View Profile
« Reply #8 on: 09 December, 2011, 22:33:53 »
0

Side note that there is almost no such thing as illegal Axe code. If it works, then it's legal. Some of Axe's side effects created through 'hackish' code can be used to your advantage.

Goto-ing out of a For() loop is totally acceptable, but I like the method you used with Returns better
Logged

Read my webcomic! | My SoundCloud
Projects:

Check out the demo now!- Current progress: battle engine and stuff
Proud author of: Cuberunner | SpaceDash | The Psyche | XXEdit | AxeSynth | StickNinja | Gravity Guy | Embers:Phoenix | Zombie Gun
Axe: Need help optimizing?
User of Axe | zStart | TokenIDE | CalcGS | MirageOS
Quigibo
The Executioner
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: 31 May, 2013, 10:48:29
Date Registered: 22 January, 2010, 05:02:37
Location: Los Angeles
Posts: 2022


Total Post Ratings: +1019

View Profile
« Reply #9 on: 09 December, 2011, 22:54:06 »
0

The only problem with using hackish effects is that anything not specified explicitly by the commands list or documentation (like the inside/outside "ans" value in a for loop) might change from version to version, especially as more optimizations are made.  So use at your own risk, you just have a small possibility of losing the portability. Smiley
Logged

___Axe_Parser___
Today the calculator, tomorrow the world!
MGOS
LV5 Advanced (Next: 300)
*****
Offline Offline

Gender: Male
Last Login: Today at 20:43:31
Date Registered: 29 July, 2011, 16:54:53
Location: Germany
Posts: 297


Topic starter
Total Post Ratings: +74

View Profile
« Reply #10 on: 09 December, 2011, 23:15:37 »
0

Huge thanks to all of you, guys. I think I'm gonna use the version with the returns. That with searching the array for values is just a simplyfied version of what I want to do, actually the condition is a lot more than just that.
« Last Edit: 04 September, 2012, 20:52:47 by MGOS » Logged

Click those to see more information Smiley
Current   
Projects: 
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.194 seconds with 31 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.