January 16, 2021, 06:38:03 AM

Author Topic: [bmx] ShuffleArray and ShuffleList by Pineapple [ 1+ years ago ]  (Read 501 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : ShuffleArray and ShuffleList
Author : Pineapple
Posted : 1+ years ago

Description : Requires this archive entry:
<a href="codearcs95e6.html?code=3066" target="_blank">http://blitzbasic.com/codearcs/codearcs.php?code=3066[/url]
<a href="https://dl.dropboxusercontent.com/u/10116881/blitz/code/mergesortarray.bmx" target="_blank">https://dl.dropboxusercontent.com/u/10116881/blitz/code/mergesortarray.bmx[/url] (Mirror)

Copies the list into an array and assigns random doubles to each object, merge sorts the array by the doubles, and returns a list with the same elements in the new, random order.

Now with bonus ShuffleArray function, as well!


Code :
Code: BlitzMax
  1. '       --+-----------------------------------------------------------------------------------------+--
  2. '         |   This code was originally written by Sophie Kirschner (sophiek@pineapplemachine.com)   |  
  3. '         | It is released as public domain. Please don't interpret that as liberty to claim credit |  
  4. '         |   that isn't yours, or to sell this code when it could otherwise be obtained for free   |  
  5. '         |                because that would be a really shitty thing of you to do.                |
  6. '       --+-----------------------------------------------------------------------------------------+--
  7.  
  8. SuperStrict
  9.  
  10. Import brl.linkedlist
  11. Import brl.random
  12. Import "mergesortarray.bmx"     ' http://blitzbasic.com/codearcs/codearcs.php?code=3066
  13.  
  14.  
  15. ' Example code
  16.  
  17. Rem
  18.  
  19. Import brl.standardio
  20.  
  21. SeedRnd Millisecs()
  22.  
  23. ' Generate a list of strings containing the first few letters of the alphabet
  24. Local stringlist:TList=CreateList()
  25. For Local a%=Asc("A") To Asc("F")
  26.         stringlist.addlast Chr(a)
  27. Next
  28.  
  29. ' Shuffle the list
  30. Local shuffledlist:TList=ShuffleList(stringlist)
  31.  
  32. ' Display the results
  33. Print "~nList before ShuffleList:"
  34. For Local str$=EachIn stringlist
  35.         Print str
  36. Next
  37. Print "~nList after ShuffleList:"
  38. For Local str$=EachIn shuffledlist
  39.         Print str
  40. Next
  41.  
  42. EndRem
  43.  
  44.  
  45. ' Takes a list as input (and optional arguments for a sorting function that you shouldn't need to touch) and returns a new list with the same contents in a random order.
  46. ' The algorithm should be reasonably fast and without any bias.
  47.  
  48. Function ShuffleList:TList(list:TList,sortfunc(array:Object[],ascending%,comparefunc(o1:Object,o2:Object))=MergeSortArray,comparefunc(o1:Object,o2:Object)=_Array_CompareObjects)
  49.         Local listlength%=list.count()
  50.         Local nodes:ShuffleNode[]=New ShuffleNode[listlength]
  51.         Local listindex%=0
  52.         For Local obj:Object=EachIn list
  53.                 nodes[listindex]=New shufflenode
  54.                 nodes[listindex].value=obj
  55.                 listindex:+1
  56.         Next
  57.         sortfunc(nodes,1,comparefunc)
  58.         Local retlist:TList=CreateList()
  59.         For Local index%=0 Until nodes.length
  60.                 retlist.addlast nodes[index].value
  61.         Next
  62.         Return retlist
  63. End Function
  64.  
  65. Function ShuffleArray:Object[](array:Object[],sortfunc(array:Object[],ascending%,comparefunc(o1:Object,o2:Object))=MergeSortArray,comparefunc(o1:Object,o2:Object)=_Array_CompareObjects)
  66.         Local nodes:ShuffleNode[]=New ShuffleNode[array.length]
  67.         Local listindex%=0
  68.         For Local obj:Object=EachIn array
  69.                 nodes[listindex]=New shufflenode
  70.                 nodes[listindex].value=obj
  71.                 listindex:+1
  72.         Next
  73.         sortfunc(nodes,1,comparefunc)
  74.         Local retarray:Object[]=New Object[array.length]
  75.         For Local index%=0 Until nodes.length
  76.                 retarray[index]=nodes[index].value
  77.         Next
  78.         Return retarray
  79. End Function
  80.  
  81. Type ShuffleNode
  82.         Field value:Object
  83.         Field num!=Rnd()
  84.         Method compare%(obj:Object)
  85.                 Local node:ShuffleNode=ShuffleNode(obj)
  86.                 If num>node.num Return 1
  87.                 Return -1
  88.         End Method
  89. End Type


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal