Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Edit:</strong> I was under the mistaken impression that <code>NSMutableOrderedSet</code> would automatically maintain order. In the past I probably had used a subclass to achieve this effect. There is no benefit to using it over NSArray unless you want set semantics.</p> <p>keeping a collection sorted is a good way to keep searches fast. <s>use <code>NSOrderedSet</code> or <code>NSMutableOrderedSet</code> instead of an array object if you can, otherwise</s> you will have to keep the array sorted when you add to it, or if it is only created once then you just sort it then.</p> <p>Also you can enumerate any collection (that conforms to the protocol) faster by using <code>NSFastEnumeration</code>.</p> <p>example:</p> <pre><code>// for an NSOrdered set or NSArray of NSDates for (NSDate* date in self.times) { // do something with date // cleaner, shorter code too } </code></pre> <p>because your collection is sorted you now will be able to tell what the closest date is without having to iterate the entire collection (most of the time).</p> <pre><code>// searching for date closest to Tuesday [Sunday] &lt;- start search here [Monday] &lt;- one day before [Thursday] &lt;- two days after, we now know that Monday is closest [Friday] &lt;- never have to visit [Saturday] &lt;- never have to visit </code></pre> <p>As pointed out by @davecom you can search faster using a binary search. Normally you would achieve this using either <code>CFArrayBSearchValues</code> or the <code>indexOfObject:inSortedRange:options:usingComparator:</code> method on <code>NSArray</code> (it assumes the array is sorted so beware) and passing <code>NSBinarySearchingOptions</code> to the options parameter. In your case this won't work because you don't know the exact object or value you are looking for. You would have to roll your own binary search algorithm.</p> <hr /> <p>If this is not fast enough for your purposes we may need more information on context. It may be the best idea to use a C array/C++ list/NSPointerArray of timestamps instead. I feel like your biggest slowdown here is the Objective-C overhead, especially for the dates. If you don't use these as actual date objects anywhere then surely you would be better off using timestamps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload