Advertisement
expired6978

OBJNET

Mar 12th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1.  
  2. void NiObjectNET::AddExtraData(NiExtraData * extraData)
  3. {
  4.     extraData->IncRef();
  5.  
  6.     // No capacity, allocate and grow
  7.     UInt32 newSize = 0;
  8.     if(m_extraDataCapacity == 0) {
  9.         newSize = 1;
  10.         NiExtraData ** extraDataList = (NiExtraData **)FormHeap_Allocate(newSize * sizeof(NiExtraData*));
  11.         extraDataList[0] = extraData;
  12.         m_extraData = extraDataList;
  13.         m_extraDataCapacity = newSize;
  14.         m_extraDataLen = m_extraDataCapacity;
  15.         return;
  16.     }
  17.  
  18.     // Reached capacity, reallocate and grow
  19.     if(m_extraDataLen == m_extraDataCapacity) {
  20.         newSize = (m_extraDataCapacity * 2) + 1;
  21.         NiExtraData ** extraDataList = (NiExtraData **)FormHeap_Allocate(newSize * sizeof(NiExtraData*));
  22.         // Copy the entries over
  23.         memcpy_s(extraDataList, newSize * sizeof(NiExtraData*), m_extraData, m_extraDataLen * sizeof(NiExtraData*));
  24.         // Zero the allocated entries
  25.         for(UInt16 i = m_extraDataLen; i < newSize; i++)
  26.             extraDataList[i] = NULL;
  27.  
  28.         FormHeap_Free(m_extraData);
  29.         m_extraData = extraDataList;
  30.         m_extraDataCapacity = newSize;
  31.     }
  32.  
  33.     m_extraData[++m_extraDataLen] = extraData;
  34.     qsort(m_extraData, m_extraDataLen, sizeof(NiExtraData*), ExtraDataCompare);
  35. }
  36.  
  37. SInt32 NiObjectNET::GetIndexOf(NiExtraData * extraData)
  38. {
  39.     if(!extraData)
  40.         return -1;
  41.     if(!extraData->m_pcName)
  42.         return -1;
  43.  
  44.     UInt16 min = 0;
  45.     UInt16 max = m_extraDataLen;
  46.     SInt32 r = -1;
  47.     while (max >= min) { // Iterative binary search
  48.         UInt16 mid = (min + max) >> 1;
  49.         if(m_extraData[mid]->m_pcName == extraData->m_pcName)
  50.             r = mid; // Found entry
  51.         else if (m_extraData[mid]->m_pcName < extraData->m_pcName)
  52.             min = mid + 1;
  53.         else
  54.             max = mid - 1;
  55.     }
  56.  
  57.     return r;
  58. }
  59.  
  60. bool NiObjectNET::RemoveExtraData(NiExtraData * extraData)
  61. {
  62.     UInt16 min = 0;
  63.     UInt16 max = m_extraDataLen;
  64.     SInt32 index = GetIndexOf(extraData);
  65.     if(index >= 0) { // Remove the entry
  66.         extraData->DecRef();
  67.         // Shift everything up
  68.         for (UInt16 i = index; i < m_extraDataLen - 1; i++)
  69.             m_extraData[i] = m_extraData[i + 1];
  70.         // Remove the duplicate tail entry
  71.         m_extraData[--m_extraDataLen] = NULL;
  72.         return true;
  73.     }
  74.  
  75.     return false;
  76. }
  77.  
  78. NiExtraData * NiObjectNET::GetExtraData(const char * name)
  79. {
  80.     UInt16 min = 0;
  81.     UInt16 max = m_extraDataLen;
  82.     while (max >= min) { // Iterative binary search
  83.         UInt16 mid = (min + max) >> 1;
  84.         if(m_extraData[mid]->m_pcName == name)
  85.             return m_extraData[mid];
  86.         else if (m_extraData[mid]->m_pcName < name)
  87.             min = mid + 1;
  88.         else
  89.             max = mid - 1;
  90.     }
  91.  
  92.     return NULL;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement