Cocoa: Check if two NSArrays are equal - cocoa

I have two NSArrays of NSRects (stored using NSStringFromRect(NSRect)). Is there a quick way to check and see if the items in the array are equal or will I have to do a loop? So item 1 in array 1 = item 1 in array 2, etc. etc.
Thanks

If you check the NSArray Reference, you'll find a handy -isEqualToArray: method that should do just what you want

From the documentation for -[NSArray isEqualToArray:]:
Compares the receiving array to another array. Two arrays have equal contents if they each hold the same number of objects and objects at a given index in each array satisfy the isEqual: test.
This is exactly what you are looking for.

Related

Finding an element by AccessibilityId and index

I have multiple buttons with the same accessibilityID. I want to get the 3rd one.
Getting the first one is easy with
let severityItems = app.buttons["MyListItems"].firstMatch
But if I want the 3rd one and do it this way
let severityItems = app.buttons["MyListItems"].element(boundBy: 2)
I get an error: Value of type 'XCUIElement' has no member 'element'
Strange. I seems to get an array. If I po it in the debugger it shows me the elements correctly. How can I get the 3rd element?
app.buttons["MyListItems"] returns an XCUIElementQuery, not an array of elements. If you look at the documentation for that you'll see a property called allElementsBoundByIndex, which returns an array of elements.

Why can't see full pointer array value after passing into a function [duplicate]

If you have a statically allocated array, the Visual Studio debugger can easily display all of the array elements. However, if you have an array allocated dynamically and pointed to by a pointer, it will only display the first element of the array when you click the + to expand it. Is there an easy way to tell the debugger, show me this data as an array of type Foo and size X?
Yes, simple.
say you have
char *a = new char[10];
writing in the debugger:
a,10
would show you the content as if it were an array.
There are two methods to view data in an array m4x4:
float m4x4[16]={
1.f,0.f,0.f,0.f,
0.f,2.f,0.f,0.f,
0.f,0.f,3.f,0.f,
0.f,0.f,0.f,4.f
};
One way is with a Watch window (Debug/Windows/Watch). Add watch =
m4x4,16
This displays data in a list:
Another way is with a Memory window (Debug/Windows/Memory). Specify a memory start address =
m4x4
This displays data in a table, which is better for two and three dimensional matrices:
Right-click on the Memory window to determine how the binary data is visualized. Choices are limited to integers, floats and some text encodings.
In a watch window, add a comma after the name of the array, and the amount of items you want to be displayed.
a revisit:
let's assume you have a below pointer:
double ** a; // assume 5*10
then you can write below in Visual Studio debug watch:
(double(*)[10]) a[0],5
which will cast it into an array like below, and you can view all contents in one go.
double[5][10] a;
For,
int **a; //row x col
add this to watch
(int(**)[col])a,row
Yet another way to do this is specified here in MSDN.
In short, you can display a character array as several types of string. If you've got an array declared as:
char *a = new char[10];
You could print it as a unicode string in the watch window with the following:
a,su
See the tables on the MSDN page for all of the different conversions possible since there are quite a few. Many different string variants, variants to print individual items in the array, etc.
You can find a list of many things you can do with variables in the watch window in this gem in the docs:
https://msdn.microsoft.com/en-us/library/75w45ekt.aspx
For a variable a, there are the things already mentioned in other answers like
a,10
a,su
but there's a whole lot of other specifiers for format and size, like:
a,en (shows an enum value by name instead of the number)
a,mb (to show 1 line of 'memory' view right there in the watch window)
For MFC arrays (CArray, CStringArray, ...)
following the next link in its Tip #4
http://www.codeproject.com/Articles/469416/10-More-Visual-Studio-Debugging-Tips-for-Native-De
For example for "CArray pArray", add in the Watch windows
pArray.m_pData,5
to see the first 5 elements .
If pArray is a two dimensional CArray you can look at any of the elements of the second dimension using the next syntax:
pArray.m_pData[x].m_pData,y
I haven't found a way to use this with a multidimensional array. But you can at least (if you know the index of your desired entry) add a watch to a specific value. Simply use the index-operator.
For an Array named current, which has an Array named Attribs inside, which has an Array named Attrib inside, it should look like this if you like to have to position 26:
((*((*current).Attribs)).Attrib)[26]
You can also use an offset
((*((*current).Attribs)).Attrib)+25
will show ne "next" 25 elements.
(I'm using VS2008, this shows only 25 elements maximum).

OCL group by element attribute

In Acceleo I have an OrderedSet of objects, where each object has a string as attribute.
I want to get a container(e.g. OrderedSet) of those strings, where each string is unique.
First, I collect all the strings into an collection ->collect(attribute). Then I convert to an Ordered Set ->asOrderedSet(). This will remove all duplicates.
A String is an (E)DataType rather than an (E)Class instance consequently it has no (e)container. You could do a total model search for all String-typed attributes and check their values - very expensive. Much better to revisit the OrderedSet construction so that the 'container' knowledge is not discarded requiring rediscovery.

Condense nested for loop to improve processing time with text analysis python

I am working on an untrained classifier model. I am working in Python 2.7. I have a loop. It looks like this:
features = [0 for i in xrange(len(dictionary))]
for bgrm in new_scored:
for i in xrange(len(dictionary)):
if bgrm[0] == dictionary[i]:
features[i] = int(bgrm[1])
break
I have a "dictionary" of bigrams that I have collected from a data set containing customer reviews and I would like to construct feature arrays of each review corresponding to the dictionary I have created. It would contain the frequencies of the bigrams found within the review of the features in the dictionary (I hope that makes sense). new_scored is a list of tuples which contains the bigrams found within a particular review paired with their relative frequency of occurrence in that review. The final feature arrays will be the same length as the original dictionary with few non zero entries.
The above works fine but I am looking at a data set of 13000 reviews, for each review to loop through this code is going to take for eeever (if my computer doesnt run out of RAM first). I have been sitting with it for a while and cannot see how I can condense it.
I am very new to python so I was hoping a more experienced could help with condensing it or perhaps point me in the right direction towards a library that will contain the function I need.
Thank you in advance!
Consider making dictionary an actual dict object (or some fancier subclass of dict if it better suits your needs), as opposed to an iterable (list or tuple seems like what it is now). dictionary could map bigrams as keys to an integer identifier that would identify a feature position.
If you refactor dictionary that way, then the loop can be rewritten as:
features = [0 for key in dictionary]
for bgram in new_scored:
try:
features[dictionary[bgram[0]]] = int(bgrm[1])
except KeyError:
# do something if the bigram is not in the dictionary for some reason
This should convert what was an O(n) traversal through dictionary into a hash lookup.
Hope this helps.

thrust::sort_by_key: How to store result in separate array?

I am currently sorting values by key the following way
thrust::sort_by_key(thrust::device_ptr<int>(keys),
thrust::device_ptr<int>(keys + numKeys),
thrust::device_ptr<int>(values);
which sorts the "values" array according to "keys".
Is there a way to leave the the "values" array untouched and instead store the result of sorting "values" in a separate array?
Thanks in advance.
There isn't a direct way to do what you are asking. You have two options to functionally achieve the same thing.
The first is make a copy of the values array before the call, leaving you with a sorted and unsorted version of the original data. So your example becomes
thrust::device_vector<int> values_sorted(thrust::device_ptr<int>(values),
thrust::device_ptr<int>(values + numKeys));
thrust::sort_by_key(thrust::device_ptr<int>(keys),
thrust::device_ptr<int>(keys + numKeys),
values_sorted.begin());
The second alternative is not to pass the values array to the sort at all. Thrust has a very useful permutation iterator which allows for seamless permuted access to an array without modifying the order in which that array is stored (so an iterator based gather operation, if you will). To do this, create an index vector and sort that by key instead, then instantiate a permutation iterator with that sorted index, something like
typedef thrust::device_vector<int>::iterator iit;
thrust::device_vector<int> index(thrust::make_counting_iterator(int(0)),
thrust::make_counting_iterator(int(numKeys));
thrust::sort_by_key(thrust::device_ptr<int>(keys),
thrust::device_ptr<int>(keys + numKeys),
index.begin());
thrust::permutation_iterator<iit,iit> perm(thrust::device_ptr<int>(values),
index.begin());
Now perm will return values in the keys sorted order held by index without ever changing the order of the original data.
[standard disclaimer: all code written in browser, never compiled or tested. Use at own risk]

Resources