Ultimate Amiga

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Array shifting  (Read 5682 times)

0 Members and 1 Guest are viewing this topic.

Umpal

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 37
  • Programming and graphic
Array shifting
« on: May 02, 2019, 02:20:08 PM »

Is there a fast way to shift an array (down/up), preferible using an asm insertion? I can do it with For...Next, but need something faster. Can't find any info in the manual regarding (Amcaf doesn't seem to have one either).
Logged

SamuraiCrow

  • compile-time wierdo
  • Forum Mod
  • A1200
  • *****
  • Karma: 5
  • Offline Offline
  • Gender: Male
  • Posts: 946
  • Compile-time wierdo
Re: Array shifting
« Reply #1 on: May 02, 2019, 03:52:18 PM »

I seem to recall a memory copy function but you're going to have to compute the start and end addresses yourself using the VarPtr function and take into account the copy direction needed.
Logged

adrazar

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 69
  • Generic Amiga User
Re: Array shifting
« Reply #2 on: May 02, 2019, 05:04:09 PM »

If you mean moving all elements in an array up or down one position, I think that's something most programmers generally avoid. You can probably solve the problem a lot more efficiently by "redefining" where the array begins, keeping track of the starting position in a separate variable. If the starting position goes below 0 you can set it equal to N instead (where N is the size of the array) and treat 0 as the next element after N in the array. It's most efficient if you don't need that kind of wrapover though, so if possible you should look for an N and starting index that makes it unnecessary.
Logged

adrazar

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 69
  • Generic Amiga User
Re: Array shifting
« Reply #3 on: May 02, 2019, 05:18:38 PM »

I seem to recall a memory copy function but you're going to have to compute the start and end addresses yourself using the VarPtr function and take into account the copy direction needed.
Yes, Copy :P
Logged

Umpal

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 37
  • Programming and graphic
Re: Array shifting
« Reply #4 on: May 02, 2019, 06:07:23 PM »

Thanks guys for your quick reply.
SamuraiCrow - Before I posted I was reading about VarPtr use in this case and I concluded same thing as you proposed. However, the reason I ruled it out gave Adrazar.
Adrazar - Keeping track of starting position and redefining it makes a lot of sense but because I use zero cell of each array it can not be applied in my case (I shift it down starting from position 1 to n but I keep the 0 cell intact at a time; I'd need to keep track of the array starting from position 1 but that would become too complicated and would lose the point of making it fast and simple). Fortunately my arrays are not big (just 25 position at most but typically around 10 [they are dynamical, thus the difference]) so I'll probably stick with For...Next solution.
Thank you both again for your interest.
Logged

Volvo_0ne

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 71
  • A1200 lover
Re: Array shifting
« Reply #5 on: May 08, 2019, 08:35:23 PM »

Thanks guys for your quick reply.
SamuraiCrow - Before I posted I was reading about VarPtr use in this case and I concluded same thing as you proposed. However, the reason I ruled it out gave Adrazar.
Adrazar - Keeping track of starting position and redefining it makes a lot of sense but because I use zero cell of each array it can not be applied in my case (I shift it down starting from position 1 to n but I keep the 0 cell intact at a time; I'd need to keep track of the array starting from position 1 but that would become too complicated and would lose the point of making it fast and simple). Fortunately my arrays are not big (just 25 position at most but typically around 10 [they are dynamical, thus the difference]) so I'll probably stick with For...Next solution.
Thank you both again for your interest.

Surely it isn't a big problem to re-dimension your arrays  and re-number your array subscripts?

If they exist, then they can be manipulated to suit with minimal changes and a little logic/arithmetic

If you are moving by one, then dimension +1 and start at 1 instead of 0 likewise for the max

You ask for a solution, but reject it because it sounds like too much work???

No Pain No Gain.
Logged
Transuranic heavy elements may not be used where there is life.

ColdWarTom

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 4
  • Generic Amiga User
Re: Array shifting
« Reply #6 on: July 20, 2020, 01:27:50 PM »

I’ve read the repeat... until Is faster than for... next. Not by a lot but every computing cycle counts.
Logged
Pages: [1]   Go Up
 

TinyPortal 2.2.2 © 2005-2022