<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[SortedArray - bug or user mistake]]></title><description><![CDATA[<p dir="auto"><em>On 23/03/2018 at 04:59, <strong>xxxxxxxx</strong> wrote:</em></p>
<p dir="auto">User Information:<br />
Cinema 4D Version:   R19 <br />
Platform:   Windows  ;   <br />
Language(s) :     C++  ;</p>
<p dir="auto">---------<br />
Hi,</p>
<p dir="auto">I was playing around with the SortedArray, writing a little helper class to sort Int32 values</p>
<pre><code>  
class SortedInt32Array : public maxon::SortedArray&lt;SortedInt32Array, maxon::BaseArray&lt;Int32&gt;&gt;  
{  
public:  
  SortedInt32Array() {}  
    
  static inline maxon::Bool LessThan(Int32 a, Int32 b)    { return a &lt; b; }  
  static inline maxon::Bool IsEqual(Int32 a, Int32 b)    { return a == b; }  
  static void InitInsertData(Int32&amp; initme, const Int32&amp; key) { initme = key; }  
};  
</code></pre>
<pre><code>  
void SortInt32(const maxon::BaseArray&lt;Int32&gt;&amp; numbers)  
{  
  SortedInt32Array sorted;  
  for (const Int32&amp; nbr : numbers)  
  {  
      maxon::Bool inserted;  
      Int32* item = sorted.FindOrInsert(nbr, inserted);  
      if (inserted)  
      {  
          // why do I need following line, shouldn't this be part of FindOrInsert implementation ???  
          sorted.InitInsertData(*item, nbr);   
      }  
  }  
  
// some more ...  
}  
  
</code></pre>
<p dir="auto">If I don't provide a call to sorted.InitInsertData() the value actually inserted into sorted is a zero, and I end up with sorted being a list of zeros.</p>
<p dir="auto">When I am looking at the actual SortedArray implementation</p>
<pre><code>  
  template &lt;typename SEARCH&gt; T* FindOrInsert(const SEARCH&amp; key, const T* ptr = nullptr)  
  {  
      CheckSort();  
      BaseSort&lt;MYSELF&gt; sort;  
      Int insertIdx = NOTOK;  
      T*    e = sort.FindInsertionIndex(key, _array, insertIdx);  
      if (!e)  
      {  
          e = _array.Insert(insertIdx);  
          if (e)  
              MYSELF::InitInsertData(*e, key);  
      }  
      return e;  
  }  
  
  template &lt;typename SEARCH&gt; T* FindOrInsert(const SEARCH&amp; key, Bool&amp; newElement, const T* ptr = nullptr)  
  {  
      CheckSort();  
      BaseSort&lt;MYSELF&gt; sort;  
      Int insertIdx = NOTOK;  
      T*    e = sort.FindInsertionIndex(key, _array, insertIdx);  
      if (!e)  
      {  
          e = _array.Insert(insertIdx);  
          newElement = true;  
      }  
      else  
      {  
          newElement = false;  
      }  
      return e;  
  }  
</code></pre>
<p dir="auto">I notice that the function</p>
<pre><code>  
[T](https://developers.maxon.net/docs/cpp/2023_2/classmaxon_1_1_sorted_array.html#ad3a1bd799977d25c6e2f36919a4b25bd) * [FindOrInsert](https://developers.maxon.net/docs/cpp/2023_2/classmaxon_1_1_sorted_array.html#a5356f3c1132c7dd74218374f6bec26fe) (const SEARCH &amp;key, const [T](https://developers.maxon.net/docs/cpp/2023_2/classmaxon_1_1_sorted_array.html#ad3a1bd799977d25c6e2f36919a4b25bd) *ptr=[nullptr](https://developers.maxon.net/docs/cpp/2023_2/compilerdetection_8h.html#ac3c36c503cc2ba5594bb924b009713fc))  
</code></pre>
<p dir="auto">does actually perform a MYSELF::InitInsertData(*e, key);<br />
while function</p>
<pre><code>  
[T](https://developers.maxon.net/docs/cpp/2023_2/classmaxon_1_1_sorted_array.html#ad3a1bd799977d25c6e2f36919a4b25bd) * [FindOrInsert](https://developers.maxon.net/docs/cpp/2023_2/classmaxon_1_1_sorted_array.html#a630e3073602651759e7418541f2a2b64) (const SEARCH &amp;key, [Bool](https://developers.maxon.net/docs/cpp/2023_2/namespacemaxon.html#a76a8b016e5ad61faf9062cc387df5016) &amp;newElement, const [T](https://developers.maxon.net/docs/cpp/2023_2/classmaxon_1_1_sorted_array.html#ad3a1bd799977d25c6e2f36919a4b25bd) *ptr=[nullptr](https://developers.maxon.net/docs/cpp/2023_2/compilerdetection_8h.html#ac3c36c503cc2ba5594bb924b009713fc))  
</code></pre>
<p dir="auto">does not, while I would expect both function would perform a MYSELF::InitInsertData right after the item was inserted into the array.</p>
<p dir="auto">Am I missing something from the documentation, that as a user I should perform the InitInsertData after using the second function, but not when calling the first function ???</p>
]]></description><link>http://developers.maxon.net/forum/topic/10702/14156_sortedarray--bug-or-user-mistake</link><generator>RSS for Node</generator><lastBuildDate>Wed, 10 Jun 2026 18:46:13 GMT</lastBuildDate><atom:link href="http://developers.maxon.net/forum/topic/10702.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 01 Sep 2018 10:11:39 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to SortedArray - bug or user mistake on Sat, 01 Sep 2018 10:11:45 GMT]]></title><description><![CDATA[<p dir="auto"><em>On 26/03/2018 at 12:12, <strong>xxxxxxxx</strong> wrote:</em></p>
<p dir="auto">Thanks for the info, Andreas.</p>
<p dir="auto">I made a copy/paste mistake when providing my sort method to describe the problem.<br />
Where I originally had put the "// some more" outside of the "if (inserted)", I actually intended to have it inside.<br />
Hence the shorter suggestion from developer (while still a useful example) isn't really of use here.</p>
<pre><code>  
void SortInt32(const maxon::BaseArray&lt;Int32&gt;&amp; numbers)  
{  
  SortedInt32Array sorted;  
  for (const Int32&amp; nbr : numbers)  
  {  
      maxon::Bool inserted;  
      Int32* item = sorted.FindOrInsert(nbr, inserted);  
      if (inserted)  
      {  
          // why do I need following line, shouldn't this be part of FindOrInsert implementation ???  
          sorted.InitInsertData(*item, nbr);  
  
          // some more ... [intended]  
      }  
  }  
  
  // some more ... [original]  
}  
  
</code></pre>
<p dir="auto">I guess, I could simply do a FindValue, and if not found, perform a FindOrInsert with 2 parameters (instead of the one with 3)</p>
<p dir="auto">Daniel</p>
]]></description><link>http://developers.maxon.net/forum/post/54611</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/54611</guid><dc:creator><![CDATA[Helper]]></dc:creator><pubDate>Sat, 01 Sep 2018 10:11:45 GMT</pubDate></item><item><title><![CDATA[Reply to SortedArray - bug or user mistake on Sat, 01 Sep 2018 10:11:42 GMT]]></title><description><![CDATA[<p dir="auto"><em>On 26/03/2018 at 08:45, <strong>xxxxxxxx</strong> wrote:</em></p>
<p dir="auto">Hi,</p>
<p dir="auto">after discussion with development this seems to be intentional. Of course then the documentation is a bit misleading and needs a fix.<br />
The <a href="https://developers.maxon.net/docs/cpp/2023_2/classmaxon_1_1_sorted_array.html#a630e3073602651759e7418541f2a2b64" target="_blank" rel="noopener noreferrer nofollow ugc">FindOrInsert()</a> with three parameters expects the value to be filled in after return.</p>
<p dir="auto">The developer suggested a shorter and more efficient implementation:</p>
<pre><code>void SortInt32(const maxon::BaseArray&lt;Int32&gt;&amp; numbers)
{
    SortedInt32Array sorted;
    Bool res = sorted.CopyFrom(numbers);
    if (!res)
        // handle error...
}
</code></pre>
<p dir="auto">With this implementation there's also no need for InitInsertData().</p>
<p dir="auto">Or to make use of <a href="https://developers.maxon.net/docs/cpp/2023_2/classmaxon_1_1_simple_sort.html" target="_blank" rel="noopener noreferrer nofollow ugc">SimpleSort</a> class, if sorting the array once is sufficient:</p>
<pre><code>    SimpleSort&lt;Int32&gt; sort;
    sort.Sort(numbers); // with numbers again being a BaseArray&lt;Int32&gt;
</code></pre>
]]></description><link>http://developers.maxon.net/forum/post/54610</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/54610</guid><dc:creator><![CDATA[Helper]]></dc:creator><pubDate>Sat, 01 Sep 2018 10:11:42 GMT</pubDate></item></channel></rss>