Anq_CivFanatics

more packed array?

Jun 9th, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.53 KB | None | 0 0
  1. Design ( https://pastebin.com/Wcn4D0pm )
  2. Arr[0] [1] [2] [3] ... [N] => len(Array)=N+1
  3. Count=N 1st 2nd 3rd ... Nth
  4. Get(-1) (0) (1) (2) ...(N-1) => for (i=0;i<p->Get(-1);i++) p->Get(i) [...];
  5. Count=N 1st 2nd 3rd ... Nth Appears like a normal array.
  6.  
  7. Flattened key-value pairs
  8. Arr[0] [1] [2] [3] ... [2N] => len(Array)=2N+1
  9. Count=N K-1 V-1 K-2 ... V-N
  10. Get(-1) (K-1) ...(K-N) => sorry you have to loop through all enums...
  11. Count=N V-1 ... V-N
  12.  
  13. # category 1: input--index. -1 for count. output--enum key at index. eg. int CvBuildingInfo::getPrereqOrBonuses(int)
  14. use int arrays internally
  15. # category 2: input--enum key. -1 for count. output--value set for key. eg. int CvBuildingInfo::getSpecialistCount(int)
  16. use int arrays internally
  17. # category 3: input--enum key. output--whether the key belongs to the set. eg. bool CvBuildingInfo::isBuildingClassNeededInCity(int)
  18. --> new fn for cat.3: same as cat.1 eg. int CvBuildingInfo::getBuildingClassNeededInCity(int)
  19. used to use bool arrays internally, I change it to int array
  20.  
  21. ## syntax highlighting doesn't work well within macro blocks. I'm turning it off.
  22.  
  23. ========================================
  24. Get Method Macros
  25. ========================================
  26. // category 1
  27. #define PACKED_ARRAY_GET_KEY(p, i) if (p) { \
  28. int size = p[0]; \
  29. if (i == -1) return size; \
  30. if (i < size) return p[i+1]; \
  31. return -1; \
  32. } return -1;
  33.  
  34. // usage cat.1
  35. int CvBuildingInfo::getPrereqAndTechs(int i) const
  36. { // [...FAssertMsg's...]
  37. PACKED_ARRAY_GET_KEY(m_piPrereqOrBonuses, i);
  38. }
  39.  
  40. // category 2
  41. #define PACKED_ARRAY_GET_LOOKUP(p, i) if (p) { \
  42. int size = p[0]; if (i == -1) return size; \
  43. for (int j = 0; j < size; j++) \
  44. if (i == p[j*2-1]) return p[j*2]; \
  45. return 0; \
  46. } return 0;
  47.  
  48. // usage cat.2
  49. int CvBuildingInfo::getSpecialistCount(int i) const
  50. { // [...FAssertMsg's...]
  51. PACKED_ARRAY_GET_LOOKUP(m_piSpecialistCount, i);
  52. }
  53.  
  54. // category 3
  55. #define PACKED_ARRAY_GET_CONTAINS(p, i) if (p) {\
  56. if (i==-1) return true; \
  57. for (int j=1; j<=p[0];j++) if (i==p[j]) return true;\
  58. return false; \
  59. } return false;
  60.  
  61. // usage cat.3
  62. bool CvBuildingInfo::isBuildingClassNeededInCity(int i) const
  63. { // [...FAssertMsg's...]
  64. PACKED_ARRAY_GET_CONTAINS(m_piBuildingClassNeededInCity, i);
  65. }
  66.  
  67. // added function for cat.3 that uses cat.1 macro
  68. int CvBuildingInfo::getBuildingClassNeededInCity(int i) const
  69. { // [...FAssertMsg's...]
  70. PACKED_ARRAY_GET_KEY(m_piBuildingClassNeededInCity, i);
  71. }
  72.  
  73. ========================================
  74. Checksum Macros
  75. ========================================
  76. // category 1
  77. #define CheckSumPackedArrayKeys(A, B) if (B) CheckSum(A, B, B[0]+1);
  78. CheckSumPackedArrayKeys(iSum, m_piPrereqOrBonuses);
  79. // used for cat.3 also
  80. CheckSumPackedArrayKeys(iSum, m_piBuildingClassNeededInCity);
  81.  
  82. // category 2
  83. #define CheckSumPackedArrayPair(A, B) if (B) CheckSum(A, B, B[0]*2+1);
  84. CheckSumPackedArrayPair(iSum, m_piSpecialistCount);
  85.  
  86. ========================================
  87. XML Parse (read(*pXML)) Macros
  88. ========================================
  89. // note: iNumChildren and szTextVal have been declared in the scope of read(*pXML).
  90. // category 1
  91. #define PACKED_ARRAY_READ(p, pXML, tagName, limit) \
  92. if (pXML->TryMoveToXmlFirstChild(tagName)) { \
  93. iNumChildren = pXML->GetXmlChildrenNumber(); \
  94. FAssertMsg((iNumChildren<=limit),"Too many children tags.");\
  95. int *tmp = new int[iNumChildren], iTotal = 0; \
  96. if (0 < iNumChildren && pXML->GetChildXmlVal(szTextVal)) { \
  97. for (int i=0; i<iNumChildren; i++) { \
  98. int eInfo = pXML->GetInfoClass(szTextVal); \
  99. if (eInfo != -1) tmp[iTotal++] = eInfo; \
  100. if (!pXML->GetNextXmlVal(szTextVal)) break; } \
  101. if (iTotal != 0) { \
  102. p = new int[iTotal+1]; p[0] = iTotal; \
  103. for (int i=0; i<iTotal; i++) p[i+1] = tmp[I]; } \
  104. pXML->MoveToXmlParent(); } pXML->MoveToXmlParent(); }
  105. // usage
  106. PACKED_ARRAY_READ(m_piPrereqOrBonuses, pXML, L"PrereqBonuses", GC.getNUM_BUILDING_PREREQ_OR_BONUSES());
  107.  
  108. // category 2
  109. // revised to eliminate 0-value pairs being stored
  110. #define PACKED_ARRAY_READ_PAIR_INT(p, pXML, tagName, limit) \
  111. if (pXML->TryMoveToXmlFirstChild(tagName)) { \
  112. iNumChildren = pXML->GetXmlChildrenNumber(); \
  113. FAssertMsg((iNumChildren<=limit),"Too many children tags.");\
  114. int *tmp = new int[iNumChildren*2], iTotal = 0; \
  115. if (0 < iNumChildren && pXML->TryMoveToXmlFirstChild()) { \
  116. for (int i=0; i<iNumChildren; i++) { \
  117. if(pXML->GetChildXmlVal(szTextVal)) { \
  118. int eInfo = pXML->GetInfoClass(szTextVal), val; \
  119. pXML->GetNextXmlVal(&val); if (eInfo!=-1 && val)\
  120. { tmp[iTotal*2]=eInfo; tmp[iTotal++*2+1]=val; } \
  121. pXML->MoveToXmlParent(); } \
  122. if (!pXML->TryMoveToXmlNextSibling()) break; } \
  123. if (iTotal != 0) { \
  124. p = new int[iTotal*2+1]; p[0] = iTotal; \
  125. for (int i=0; i<iTotal*2; i++) p[i+1] = tmp[I]; } \
  126. pXML->MoveToXmlParent(); } pXML->MoveToXmlParent(); }
  127. // usage
  128. PACKED_ARRAY_READ_PAIR_INT(m_piSpecialistCount, pXML, L"SpecialistCounts", GC.getNumSpecialistInfos());
  129. // watch how it compares to original line
  130. pXML->SetVariableListTagPair(&m_piSpecialistCount, L"SpecialistCounts", GC.getNumSpecialistInfos());
  131.  
  132. // category 3
  133. #define PACKED_ARRAY_READ_PAIR_BOOL(p, pXML, tagName, limit) \
  134. if (pXML->TryMoveToXmlFirstChild(tagName)) { \
  135. iNumChildren = pXML->GetXmlChildrenNumber(); \
  136. FAssertMsg((iNumChildren<=limit),"Too many children tags.");\
  137. int *tmp = new int[iNumChildren], iTotal = 0; \
  138. if (0 < iNumChildren && pXML->TryMoveToXmlFirstChild()) { \
  139. for (int i=0; i<iNumChildren; i++) { \
  140. if(pXML->GetChildXmlVal(szTextVal)) { \
  141. int eInfo = pXML->GetInfoClass(szTextVal); \
  142. bool b; pXML->GetNextXmlVal(&b); \
  143. if (eInfo != -1 && b) tmp[iTotal++] = eInfo; \
  144. pXML->MoveToXmlParent(); } \
  145. if (!pXML->TryMoveToXmlNextSibling()) break; } \
  146. if (iTotal != 0) { \
  147. p = new int[iTotal+1]; p[0] = iTotal; \
  148. for (int i=0; i<iTotal; i++) p[i+1] = tmp[I]; } \
  149. pXML->MoveToXmlParent(); } pXML->MoveToXmlParent(); }
  150. // usage
  151. PACKED_ARRAY_READ_PAIR_BOOL(m_piBuildingClassNeededInCity, pXML, L"BuildingClassNeededs", GC.getNumBuildingClassInfos());
  152. // watch how it compares to original line
  153. pXML->SetVariableListTagPair(&m_pbBuildingClassNeededInCity, L"BuildingClassNeededs", GC.getNumBuildingClassInfos());
  154.  
  155. ========================================
  156. CopyNonDefaults Macros
  157. ========================================
  158. // category 1
  159. #define PACKED_ARRAY_COPYNONDEFAULTS(theirFn, ourArr) \
  160. if (theirFn(-1) != -1) { int N1 = theirFn(-1); \
  161. int N2 = 0; if (ourArr) N2 = ourArr[0]; \
  162. int* tmp = new int[N1+N2+1]; tmp[0] = N1+N2; \
  163. for (int i=0; i<N1; i++) tmp[i+1] = theirFn(i); \
  164. for (int i=0; i<N2; i++) tmp[i+N1+1] = ourArr[i+1]; \
  165. ourArr = tmp; }
  166. // usage
  167. PACKED_ARRAY_COPYNONDEFAULTS(pClassInfo->getPrereqOrBonuses, m_piPrereqOrBonuses);
  168. // used for cat.3 also
  169. PACKED_ARRAY_COPYNONDEFAULTS(pClassInfo->getBuildingClassNeededInCity, m_piBuildingClassNeededInCity);
  170.  
  171. // category 2
  172. #define PACKED_ARRAY_COPYNONDEFAULTS_PAIR(theirFn, ourArr, limit) \
  173. if (theirFn(-1) != 0) { int N1=theirFn(-1),N2=0;if(ourArr) N2=ourArr[0];\
  174. int* tmp = new int[N1*2+N2*2+1]; tmp[0] = N1+N2; \
  175. for (int i=0,j=0; i<limit; i++) if (theirFn(i) != 0)\
  176. { tmp[j++*2+1] = i; tmp[j*2] = theirFn(i); } \
  177. for (int i=0;i<N2*2;i++) tmp[i+N1*2+1]=ourArr[i+1];\
  178. ourArr = tmp; }
  179. // usage
  180. PACKED_ARRAY_COPYNONDEFAULTS_PAIR(pClassInfo->getSpecialistCount, m_piSpecialistCount, GC.getNumSpecialistInfos());
Advertisement
Add Comment
Please, Sign In to add comment