Results 1 to 8 of 8

Thread: Idea for ordered SWAP

  1. #1

    Idea for ordered SWAP

    It is common to use SWAP to put two items in ascending order, like this:

    DIM ONE, TWO AS LONG
    ' ...
    
    IF ONE > TWO THEN SWAP ONE, TWO
    
    This type of code is so common, it would be convenient to combine the two actions into one. Suppose we called this capability ORDER. Then, the code above could be shortened to,

    ORDER ONE, TWO
    
    It would not make sense for SWAP to take anything other than two operands, but for ORDER, it would. So, there is no reason why ORDER couldn't arrange the values of multiple variables in ascending order:

    ORDER ONE, TWO, THREE
    
    In the ORDER statement (or function) each variable would have to be of the same type.

    If ORDER were designed as a function rather than a statement, the returned value could be boolean, with TRUE being returned if any rearrangement of values occurred, otherwise FALSE.

  2. #2
    thinBasic MVPs ReneMiner's Avatar
    Join Date
    Oct 2012
    Location
    Germany
    Age
    51
    Posts
    1,398
    Blog Entries
    1
    Rep Power
    154
    Extended:
    String One, Two, Three
    One = "I don't care"
    Two = "1000"
    Three = MKL$(123)
    String myArray() = Order [One, Two, Three, "and also this"], Collate Ucase
    
    Last edited by ReneMiner; 15-07-2013 at 18:51.
    running thinBasic in HP Desktop Pavillon 8GB Ram AMD A6-9225 Radeon R4 5Compute Cores 2C+3G @2.6GHz

    OS1: Windows x64 Pro 10.0.18363.418
    OS2: ThinDE (WinRE+.NetCore-based customized Developement Environment)

  3. #3
    After my initial post, it did occur to me that ORDER could/should be applicable to arrays, and that for strings, a case-insensitive version might be handly, and you noted. I am not quite sure how the syntax would work in practice, but it seems like a good idea to attempt it.

    It turns out that when people say "case insensitive", the expression is curiously backwards. When an operation like sorting is "case sensitive" it means that upper/lower case data is considered distinct, and therefore while handling characters, no tests for upper/lower case or case conversions of data are performed, but rather, the character encoding of the data values is used "as is". The very lack of 'action' with respect to case means that "case sensitive" processing is actually 'IN-sensitive' to the data's content. Conversely, "case insensitive" means that upper/lower case data is considered equivalent, but to obtain such an equivalence, a case-conversion function is required which must take into account the difference between upper and lower case, and so it must be 'sensitive' to those differences. As I say, it's backwards, but everyone seems to know what is meant ...

    If we really wanted special handling for strings, perhaps we need ORDER for numbers and ORDER$ for strings. If so, a "case insensitive" version could be made by translating the data to upper case for the compare phase of the swaps that are done. That would mean there might perhaps be a U_ORDER$ function. And, since there could be slight differences between how data is ordered if the comparisons are done in lower case (that is, if non-alpha data were interspersed) there could be an argument for an L_ORDER$ function too. Most people don't care much about that degree of subtlety though, and a U_ORDER$ function is probably enough.

  4. #4
    thinBasic MVPs ReneMiner's Avatar
    Join Date
    Oct 2012
    Location
    Germany
    Age
    51
    Posts
    1,398
    Blog Entries
    1
    Rep Power
    154
    ok, you got me- I just would like to have the multiple usable array-constructor
    ["treat", "or", "make", "this", "to", "be", "an", "array" ]
    
    as well as sub or function-parameter as also to be a constructor as this:
    String A() = ["Hello", "this", "is", "the", "first", "dimension"] _
                     ["and", "this", "is", "the", "second", "dimension"]
    
    which should result in a matrix as A(2,6)
    while this:
    String B() = ["This", "has", "only", "one", "dimension"]
    
    creates B(5)

    and perhaps some optional switches as this:
    String C() = ["One", "Two", "Three"], Sort Ascend| Descent, Collate Ucase
    
    Last edited by ReneMiner; 15-07-2013 at 20:25.
    running thinBasic in HP Desktop Pavillon 8GB Ram AMD A6-9225 Radeon R4 5Compute Cores 2C+3G @2.6GHz

    OS1: Windows x64 Pro 10.0.18363.418
    OS2: ThinDE (WinRE+.NetCore-based customized Developement Environment)

  5. #5
    My goals for ORDER were less ambitious. There already exists an ARRAY SORT, so having ORDER apply to arrays is probably not necessary.

    However, I think there was a recent idea about using [ ] brackets for an "array constructor", as your prior comment suggests. That would be a separate thing, but I don't see why there's any reason you shouldn't be able to create an array with [ ] and then sort it with ARRAY SORT.

    That means you might be able create an array from individual items, then sort the array:

    DIM ONE, TWO, THREE, A() AS STRING
    ' ... set values for ONE, TWO and THREE
    A() = [ONE, TWO, THREE]
    ARRAY SORT A()
    
    or, if you wanted the item values sorted first, you could do it this way:

    DIM ONE, TWO, THREE, A() AS STRING
    ' ... set values for ONE, TWO and THREE
    ORDER ONE, TWO, THREE
    A() = [ONE, TWO, THREE]
    

  6. #6
    thinBasic MVPs ReneMiner's Avatar
    Join Date
    Oct 2012
    Location
    Germany
    Age
    51
    Posts
    1,398
    Blog Entries
    1
    Rep Power
    154
    yes- I edited already because I remembered sort-method. But my ambition is more about having passed parameters instantly ordered before a function uses them and it would be great to pass them in random order - while they could get proceeded sorted then. So maybe it's some totally different thing what you meant intentionally from that what my first idea of its use was. Especially I came upon the idea to use the brackets again because it could probably handle an arbitrary number of elements then.
    running thinBasic in HP Desktop Pavillon 8GB Ram AMD A6-9225 Radeon R4 5Compute Cores 2C+3G @2.6GHz

    OS1: Windows x64 Pro 10.0.18363.418
    OS2: ThinDE (WinRE+.NetCore-based customized Developement Environment)

  7. #7
    I am not sure who came up with the idea of using [ ] brackets as an array constructor, but I do like it.

    If the notion of having a sorted array is useful enough, suppose the two ideas could be combined. Let's say there was not only an array constructor, but a SORTED array constructor. For laughs, let's say the notation is [^ ]. That would put the array contents in sorted order into the array:
    DIM ONE, TWO, THREE, A() AS LONG
    ' ... set values for ONE, TWO and THREE; may be out of order
    A() = [^ ONE, TWO, THREE] 
    ' array A now has values for ONE, TWO and THREE in order.
    
    For handling strings, arrays where the order is reversed, etc. might require more involved syntax like [^^ ] or [! ] or something; who knows? Probably only the more important cases need to be handled, and the others dealt with using a combination of plain a [ ] constructor plus an ARRAY SORT statement.

    For ordering individual, non-array items, I still think an ORDER statement or function would be useful.

  8. #8
    thinBasic MVPs ReneMiner's Avatar
    Join Date
    Oct 2012
    Location
    Germany
    Age
    51
    Posts
    1,398
    Blog Entries
    1
    Rep Power
    154
    Yes, I like the idea of ordering variable content without the need of additional local variables and stuff too. But Swap always has to deal with two variables while ORDER can affect two or at best countless more variables, so there is some way needed to pass or buffer them if we don't want to have a 32-parameter-limit

    EDIT:

    Maybe using SORT-Keyword whithout ARRAY-keyword but like a sign for {"here", "is", "some" , "row", "of", "elements"}, alike
    Long One = 3
    Long Two = 2
    Long Three = 1
    Sort Long {One, Two, Three}
    
    EDIT2:
    "ambitious"
    Sort Long At { VarPtr(One), Varptr(Two), VarPtr(Three) }
    '...
    
    String foo = mkl$(3) + mkl$(2) + mkl$(1)
    Sort Long At { StrPtr(foo), StrPtr(foo) + SizeOf(Long), StrPtr(foo) + 2 * SizeOf(Long) }
    
    'extraordinary 
    Memory_Sort At StrVar "foo" As Long
    'Memory_Sort At Heap(hPtr) As Dword
    Memory_Sort At {One, Two, Three} As Long, Ascend
    
    Last edited by ReneMiner; 15-07-2013 at 21:38.
    running thinBasic in HP Desktop Pavillon 8GB Ram AMD A6-9225 Radeon R4 5Compute Cores 2C+3G @2.6GHz

    OS1: Windows x64 Pro 10.0.18363.418
    OS2: ThinDE (WinRE+.NetCore-based customized Developement Environment)

Similar Threads

  1. Replies: 5
    Last Post: 30-07-2020, 10:53
  2. My Idea
    By peter in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 4
    Last Post: 26-10-2012, 22:15
  3. New mad idea on UI module
    By ErosOlmi in forum thinBasic vaporware
    Replies: 17
    Last Post: 07-08-2008, 15:04
  4. Another idea
    By kryton9 in forum thinBasic General
    Replies: 2
    Last Post: 18-02-2008, 05:43

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •