Collection library

agraham

Expert
Licensed User
Longtime User
I assume you are using the db2000GetInfoDevice library. As I pointed out in my post above Basic4ppc v6.9 fundamentally changed the handling of variables, both regular ones and arrays. In order to work with version 6.9 the library would need to be recompiled to return a value of type String[] instead of the type Array that it does now.

I realise that although I have modified the Collections library to work with Basic4ppc v6.9 I forgot to post the updated library. I'll do that when I've checked it out again.
 

agraham

Expert
Licensed User
Longtime User
Basic4ppc version 6.90 significantly changed how variables are handled as it introduced typed variables. Version 1.5 of this library makes some required changes to ArraysEx and ArrayListEx to accommodate this.

ArrayLists, like all the other collections in Basic4ppc and this library hold their values as strings. Note that arrays are not collection objects in .NET but are more primitive structures that hold the types of values for which they are declared.

Owing to the changes made to array variables in Basic4ppc v6.90 the optimising compiler requires ArrayList.ToArray to return an array of the same type as that being assigned to, with the ArrayList contents coerced to that type. The ArrayListEx.ToStringArray, ToIntegerArray and ToNumberArray methods are provided to return the type of array required.

For the same reason ArraysEx now also provides CloneString, CloneInteger and CloneNumber that also return the array types that the optimising compiler requires.

ArrayListEx.CopyTo will now work with arrays of type Integer, Int32, Number, Double and String.
 

konisek

Member
Licensed User
Longtime User
To Agraham:
Please, will you re-compile the db2000GetInfoDevice library soon, or shall I downgrade to 6.8 version? Thanks, L.
 

agraham

Expert
Licensed User
Longtime User
It is not my library to recompile, it was written by maXim. You could try a workaround with the Door library. I haven't tested it but it should (famous last words!) work.

ObjArray is a Door library ObjectArray.
B4X:
   ObjArray.New1(0) ' size doesn't matter as we overwrite the value property
   ...
   ObjArray.Value = Getdevice.info
   Info0 = ObjArray.Get(0) ' 0 to 4
 

konisek

Member
Licensed User
Longtime User
Ok, your code works but I still need the "db2000 GetInfoDevice.dll library", right? So I cannot compile it in v6.9 until maXim revises it. (L)
 

mjcoon

Well-Known Member
Licensed User
Query & Crash in ArraysExDemoInteger.sbp

I haven't made much use of Collections.dll but have what I tought would be an ideal use of BinarySearch.

But I don't even understand the Help. It says
Otherwise, it is the index of the first element that is larger than value.
I think the "it" is
...the absolute value of this number
referring to a negative number returned for a mismatch. But since index values start from zero and index zero could contain a larger number than the search term, a zero result could be ambiguous. Is this correct; does a zero result have to be tested to see if it is a match or not?

I wondered if I could learn from running ArraysExDemoInteger.sbp, but under the desktop IDE it crashed per the thumbnail.

Mike.
 

Attachments

  • BinarySearch-crash.JPG
    BinarySearch-crash.JPG
    17 KB · Views: 3

agraham

Expert
Licensed User
Longtime User
Is this correct; does a zero result have to be tested to see if it is a match or not?
No, it's not correct but I am puzzled as the help is largely copied from the .NET documentation. I suspect that there was an error in the original .NET documentation which is now corrected.

The zero-based index of value in the sorted ArrayList, if value is found; otherwise, a negative number, which is the bitwise complement of the index of the next element that is larger than value or, if there is no larger element, the bitwise complement of Count.

ArrayList.BinarySearch Method (Object) (System.Collections)
 

mjcoon

Well-Known Member
Licensed User
No, it's not correct but I am puzzled as the help is largely copied from the .NET documentation. I suspect that there was an error in the original .NET documentation which is now corrected.

Thank you for your instant response, Andrew.

I guess I shall have to grapple with complements after decades (I remember there were ones-complement and twos-complement). The bitwise complement of zero is not zero; I think that it is the same as -1. But I was definitely getting zero as a value even though there was no hit.

I have tried changing to use IndexOf() since it should just work slightly slower on a sorted array. But I was getting an index equal to the count of entries, which should not be possible.

So still baffled. That is even withut the crash with the demo program.

BTW I wonder why the "next" in the text that you quoted (and gave the source for). Next from where? Surely they mean "first element", not next?

Mike.
 

mjcoon

Well-Known Member
Licensed User
Haven't got time to look at "next" at the moment but
B4X:
If index < 0 then
    index = 1 - index ' complement
End If

Thanks again; that was my conclusion too! But I was not negating or complementing the result. Merely testing for " > -1" which should have trapped all mismatches but didn't.

Please ignore my winge about IndexOf(); that problem was down to my not noticing that the order of arguments is quite different from BinarySearch()
 

mjcoon

Well-Known Member
Licensed User
Sorry, the perils of haste :(, dinner was ready to serve! The above is wrong, it should be

B4X:
If index < 0 then
    index = 0 - 1 - index ' complement
End If
so
0xffff (-1) => 0
0xfffe (-2) => 1
0cfffd (-3) => 2
etc.

Thanks again Andrew. It would be nice to have this explained in the Help, but maybe it is not worth doing that now it is enshrined in this relevant thread.

None of the above explains why I am getting zero returned from BinarySearch() whereas IndexOf() gives the results I expect and works fine. The demo program works OK if tweaked to look for missing values. BTW I think that the constants are singular as in the Help, not cNumbers. I suppose if you have the detection suppressed this means you will get a null string or zero passed to the operation instead of the intended constant, which could give strange results.

My look-up arrays are generated by sparsely storing a counter, so are sorted numerically from the outset; I do not perform a sort operation. If you wanted(?) to investigate I could write out a sample array. (It is derived from zooming an OS-GB vector road-map.)

Mike.
 

Attachments

  • zoomed-map.jpg
    zoomed-map.jpg
    24.9 KB · Views: 17

agraham

Expert
Licensed User
Longtime User
Time dims the memory. :( All is now clear! Unfortunately there is a bug in ArraysEx.BinarySearch and ArrayListEx.BinarySearch. The .NET documentation is correct but in attempting to make this statement in the help true
If value is not found a negative number is returned. If the absolute value of this number is greater than or equal to the size of the array, there are no elements larger than value in the array. Otherwise, it is the index of the first element that is larger than value.
and notionally make things simpler by compensating for the bitwise inversion I missed that zero would be rturned both for a match at index 0 and for a no match when all the elements are larger than the search value. So as you suggested earlier a zero return is ambiguous and needs a further check doing to be correctly interpreted.
 

asmag

Member
Licensed User
Longtime User
How do I use it?

:sign0013:

How do I use it? I pasted the dll library folder but it does not appear in Referenced Libraries

Regards

asmag
 
Top