Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Design ( https://pastebin.com/Wcn4D0pm )
- Arr[0] [1] [2] [3] ... [N] => len(Array)=N+1
- Count=N 1st 2nd 3rd ... Nth
- Get(-1) (0) (1) (2) ...(N-1) => for (i=0;i<p->Get(-1);i++) p->Get(i) [...];
- Count=N 1st 2nd 3rd ... Nth Appears like a normal array.
- Flattened key-value pairs
- Arr[0] [1] [2] [3] ... [2N] => len(Array)=2N+1
- Count=N K-1 V-1 K-2 ... V-N
- Get(-1) (K-1) ...(K-N) => sorry you have to loop through all enums...
- Count=N V-1 ... V-N
- # category 1: input--index. -1 for count. output--enum key at index. eg. int CvBuildingInfo::getPrereqOrBonuses(int)
- use int arrays internally
- # category 2: input--enum key. -1 for count. output--value set for key. eg. int CvBuildingInfo::getSpecialistCount(int)
- use int arrays internally
- # category 3: input--enum key. output--whether the key belongs to the set. eg. bool CvBuildingInfo::isBuildingClassNeededInCity(int)
- --> new fn for cat.3: same as cat.1 eg. int CvBuildingInfo::getBuildingClassNeededInCity(int)
- used to use bool arrays internally, I change it to int array
- ## syntax highlighting doesn't work well within macro blocks. I'm turning it off.
- ========================================
- Get Method Macros
- ========================================
- // category 1
- #define PACKED_ARRAY_GET_KEY(p, i) if (p) { \
- int size = p[0]; \
- if (i == -1) return size; \
- if (i < size) return p[i+1]; \
- return -1; \
- } return -1;
- // usage cat.1
- int CvBuildingInfo::getPrereqAndTechs(int i) const
- { // [...FAssertMsg's...]
- PACKED_ARRAY_GET_KEY(m_piPrereqOrBonuses, i);
- }
- // category 2
- #define PACKED_ARRAY_GET_LOOKUP(p, i) if (p) { \
- int size = p[0]; if (i == -1) return size; \
- for (int j = 0; j < size; j++) \
- if (i == p[j*2-1]) return p[j*2]; \
- return 0; \
- } return 0;
- // usage cat.2
- int CvBuildingInfo::getSpecialistCount(int i) const
- { // [...FAssertMsg's...]
- PACKED_ARRAY_GET_LOOKUP(m_piSpecialistCount, i);
- }
- // category 3
- #define PACKED_ARRAY_GET_CONTAINS(p, i) if (p) {\
- if (i==-1) return true; \
- for (int j=1; j<=p[0];j++) if (i==p[j]) return true;\
- return false; \
- } return false;
- // usage cat.3
- bool CvBuildingInfo::isBuildingClassNeededInCity(int i) const
- { // [...FAssertMsg's...]
- PACKED_ARRAY_GET_CONTAINS(m_piBuildingClassNeededInCity, i);
- }
- // added function for cat.3 that uses cat.1 macro
- int CvBuildingInfo::getBuildingClassNeededInCity(int i) const
- { // [...FAssertMsg's...]
- PACKED_ARRAY_GET_KEY(m_piBuildingClassNeededInCity, i);
- }
- ========================================
- Checksum Macros
- ========================================
- // category 1
- #define CheckSumPackedArrayKeys(A, B) if (B) CheckSum(A, B, B[0]+1);
- CheckSumPackedArrayKeys(iSum, m_piPrereqOrBonuses);
- // used for cat.3 also
- CheckSumPackedArrayKeys(iSum, m_piBuildingClassNeededInCity);
- // category 2
- #define CheckSumPackedArrayPair(A, B) if (B) CheckSum(A, B, B[0]*2+1);
- CheckSumPackedArrayPair(iSum, m_piSpecialistCount);
- ========================================
- XML Parse (read(*pXML)) Macros
- ========================================
- // note: iNumChildren and szTextVal have been declared in the scope of read(*pXML).
- // category 1
- #define PACKED_ARRAY_READ(p, pXML, tagName, limit) \
- if (pXML->TryMoveToXmlFirstChild(tagName)) { \
- iNumChildren = pXML->GetXmlChildrenNumber(); \
- FAssertMsg((iNumChildren<=limit),"Too many children tags.");\
- int *tmp = new int[iNumChildren], iTotal = 0; \
- if (0 < iNumChildren && pXML->GetChildXmlVal(szTextVal)) { \
- for (int i=0; i<iNumChildren; i++) { \
- int eInfo = pXML->GetInfoClass(szTextVal); \
- if (eInfo != -1) tmp[iTotal++] = eInfo; \
- if (!pXML->GetNextXmlVal(szTextVal)) break; } \
- if (iTotal != 0) { \
- p = new int[iTotal+1]; p[0] = iTotal; \
- for (int i=0; i<iTotal; i++) p[i+1] = tmp[I]; } \
- pXML->MoveToXmlParent(); } pXML->MoveToXmlParent(); }
- // usage
- PACKED_ARRAY_READ(m_piPrereqOrBonuses, pXML, L"PrereqBonuses", GC.getNUM_BUILDING_PREREQ_OR_BONUSES());
- // category 2
- // revised to eliminate 0-value pairs being stored
- #define PACKED_ARRAY_READ_PAIR_INT(p, pXML, tagName, limit) \
- if (pXML->TryMoveToXmlFirstChild(tagName)) { \
- iNumChildren = pXML->GetXmlChildrenNumber(); \
- FAssertMsg((iNumChildren<=limit),"Too many children tags.");\
- int *tmp = new int[iNumChildren*2], iTotal = 0; \
- if (0 < iNumChildren && pXML->TryMoveToXmlFirstChild()) { \
- for (int i=0; i<iNumChildren; i++) { \
- if(pXML->GetChildXmlVal(szTextVal)) { \
- int eInfo = pXML->GetInfoClass(szTextVal), val; \
- pXML->GetNextXmlVal(&val); if (eInfo!=-1 && val)\
- { tmp[iTotal*2]=eInfo; tmp[iTotal++*2+1]=val; } \
- pXML->MoveToXmlParent(); } \
- if (!pXML->TryMoveToXmlNextSibling()) break; } \
- if (iTotal != 0) { \
- p = new int[iTotal*2+1]; p[0] = iTotal; \
- for (int i=0; i<iTotal*2; i++) p[i+1] = tmp[I]; } \
- pXML->MoveToXmlParent(); } pXML->MoveToXmlParent(); }
- // usage
- PACKED_ARRAY_READ_PAIR_INT(m_piSpecialistCount, pXML, L"SpecialistCounts", GC.getNumSpecialistInfos());
- // watch how it compares to original line
- pXML->SetVariableListTagPair(&m_piSpecialistCount, L"SpecialistCounts", GC.getNumSpecialistInfos());
- // category 3
- #define PACKED_ARRAY_READ_PAIR_BOOL(p, pXML, tagName, limit) \
- if (pXML->TryMoveToXmlFirstChild(tagName)) { \
- iNumChildren = pXML->GetXmlChildrenNumber(); \
- FAssertMsg((iNumChildren<=limit),"Too many children tags.");\
- int *tmp = new int[iNumChildren], iTotal = 0; \
- if (0 < iNumChildren && pXML->TryMoveToXmlFirstChild()) { \
- for (int i=0; i<iNumChildren; i++) { \
- if(pXML->GetChildXmlVal(szTextVal)) { \
- int eInfo = pXML->GetInfoClass(szTextVal); \
- bool b; pXML->GetNextXmlVal(&b); \
- if (eInfo != -1 && b) tmp[iTotal++] = eInfo; \
- pXML->MoveToXmlParent(); } \
- if (!pXML->TryMoveToXmlNextSibling()) break; } \
- if (iTotal != 0) { \
- p = new int[iTotal+1]; p[0] = iTotal; \
- for (int i=0; i<iTotal; i++) p[i+1] = tmp[I]; } \
- pXML->MoveToXmlParent(); } pXML->MoveToXmlParent(); }
- // usage
- PACKED_ARRAY_READ_PAIR_BOOL(m_piBuildingClassNeededInCity, pXML, L"BuildingClassNeededs", GC.getNumBuildingClassInfos());
- // watch how it compares to original line
- pXML->SetVariableListTagPair(&m_pbBuildingClassNeededInCity, L"BuildingClassNeededs", GC.getNumBuildingClassInfos());
- ========================================
- CopyNonDefaults Macros
- ========================================
- // category 1
- #define PACKED_ARRAY_COPYNONDEFAULTS(theirFn, ourArr) \
- if (theirFn(-1) != -1) { int N1 = theirFn(-1); \
- int N2 = 0; if (ourArr) N2 = ourArr[0]; \
- int* tmp = new int[N1+N2+1]; tmp[0] = N1+N2; \
- for (int i=0; i<N1; i++) tmp[i+1] = theirFn(i); \
- for (int i=0; i<N2; i++) tmp[i+N1+1] = ourArr[i+1]; \
- ourArr = tmp; }
- // usage
- PACKED_ARRAY_COPYNONDEFAULTS(pClassInfo->getPrereqOrBonuses, m_piPrereqOrBonuses);
- // used for cat.3 also
- PACKED_ARRAY_COPYNONDEFAULTS(pClassInfo->getBuildingClassNeededInCity, m_piBuildingClassNeededInCity);
- // category 2
- #define PACKED_ARRAY_COPYNONDEFAULTS_PAIR(theirFn, ourArr, limit) \
- if (theirFn(-1) != 0) { int N1=theirFn(-1),N2=0;if(ourArr) N2=ourArr[0];\
- int* tmp = new int[N1*2+N2*2+1]; tmp[0] = N1+N2; \
- for (int i=0,j=0; i<limit; i++) if (theirFn(i) != 0)\
- { tmp[j++*2+1] = i; tmp[j*2] = theirFn(i); } \
- for (int i=0;i<N2*2;i++) tmp[i+N1*2+1]=ourArr[i+1];\
- ourArr = tmp; }
- // usage
- PACKED_ARRAY_COPYNONDEFAULTS_PAIR(pClassInfo->getSpecialistCount, m_piSpecialistCount, GC.getNumSpecialistInfos());
Advertisement
Add Comment
Please, Sign In to add comment