PDA

View Full Version : Idea for ordered SWAP



Robert Hodge
15-07-2013, 18:04
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.

ReneMiner
15-07-2013, 18:41
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

Robert Hodge
15-07-2013, 19:39
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.

ReneMiner
15-07-2013, 19:48
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

Robert Hodge
15-07-2013, 20:22
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]

ReneMiner
15-07-2013, 20:33
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.

Robert Hodge
15-07-2013, 20:48
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.

ReneMiner
15-07-2013, 20:54
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