BVB Source Codes

CRYENGINE Show XMLCPB_AttrSetTableWriter.h Source code

Return Download CRYENGINE: download XMLCPB_AttrSetTableWriter.h Source code - Download CRYENGINE Source code - Type:.h
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. /*************************************************************************
  4. *************************************************************************/
  5.  
  6. #pragma once
  7. #ifndef XMLCPB_ATTRSET_TABLE_WRITER_H
  8.         #define XMLCPB_ATTRSET_TABLE_WRITER_H
  9.  
  10.         #include "../XMLCPB_Common.h"
  11.         #include "XMLCPB_BufferWriter.h"
  12.         #include <CryCore/Platform/IPlatformOS.h>
  13.         #include <CryCore/StlUtils.h>
  14.  
  15. // an "AttrSet" defines the datatype + tagId of all the attributes of a node.  each datatype+tagId entry is called a "header".
  16. // those sets are stored in a common table because many nodes share similar lists of attrs.
  17.  
  18. namespace XMLCPB {
  19.  
  20. class CWriter;
  21. class CAttrSet;
  22.  
  23. class CAttrSet
  24. {
  25. public:
  26.  
  27.         bool operator<(const CAttrSet& other) const
  28.         {
  29.                 if (m_numAttrs < other.m_numAttrs)
  30.                         return true;
  31.                 if (m_numAttrs > other.m_numAttrs)
  32.                         return false;
  33.  
  34.                 for (uint32 i = 0; i < m_numAttrs; ++i)
  35.                 {
  36.                         if (m_pHeaders[i] == other.m_pHeaders[i])
  37.                                 continue;
  38.                         if (m_pHeaders[i] < other.m_pHeaders[i])
  39.                                 return true;
  40.                         else
  41.                                 return false;
  42.                 }
  43.                 return false;
  44.         }
  45.  
  46.         bool operator==(const CAttrSet& other) const
  47.         {
  48.                 if (m_numAttrs != other.m_numAttrs)
  49.                         return false;
  50.  
  51.                 for (uint32 i = 0; i < m_numAttrs; ++i)
  52.                 {
  53.                         if (m_pHeaders[i] != other.m_pHeaders[i])
  54.                                 return false;
  55.                 }
  56.                 return true;
  57.         }
  58.  
  59.         uint16* m_pHeaders;
  60.         uint32  m_numAttrs;
  61. };
  62.  
  63. //////////////////////////////////////////////////////////////////////////
  64.  
  65. class CAttrSetTableWriter
  66. {
  67. public:
  68.  
  69.         CAttrSetTableWriter(uint32 maxNumSets, uint32 bufferSize);
  70.         void      Init(CWriter* pWriter);
  71.  
  72.         AttrSetID GetSetID(const CAttrSet& set);
  73.         int       GetNumSets() const { return m_setAddrs.size(); }
  74.         void      WriteToFile();
  75.         void      WriteToMemory(uint8*& rpData, uint32& outWriteLoc);
  76.         uint32    GetDataSize() const;
  77.         uint32    GetSetsDataSize() const;
  78.  
  79. private:
  80.  
  81.         CAttrSetTableWriter&       operator=(const CAttrSetTableWriter& other); // just to prevent assignments and copy constructors
  82.         CAttrSetTableWriter(const CAttrSetTableWriter&);                        //
  83.         const CAttrSetTableWriter& GetThis() const { return *this; }
  84.  
  85.         static const AttrSetID SEARCHING_ID;
  86.  
  87.         struct SetHasher
  88.         {
  89.                 const CAttrSetTableWriter& m_owner;
  90.  
  91.                 SetHasher(const CAttrSetTableWriter& owner) : m_owner(owner) {}
  92.  
  93.                 ILINE static size_t HashSet(const CAttrSet& set)
  94.                 {
  95.                         size_t hash = 5381;
  96.                         for (uint32 i = 0; i < set.m_numAttrs; ++i)
  97.                         {
  98.                                 hash = ((hash << 5) + hash) + set.m_pHeaders[i];   /* hash * 33 + c */
  99.                         }
  100.                         return hash;
  101.                 }
  102.  
  103.                 size_t operator()(const AttrSetID& ID) const
  104.                 {
  105.                         if (ID == CAttrSetTableWriter::SEARCHING_ID)
  106.                                 return m_owner.m_checkHash;
  107.  
  108.                         CAttrSet set;
  109.                         assert(ID < m_owner.m_numAttrsArray.size());
  110.                         set.m_numAttrs = m_owner.m_numAttrsArray[ID];
  111.                         set.m_pHeaders = (uint16*)(m_owner.m_buffer.GetPointerFromAddr(m_owner.m_setAddrs[ID]));
  112.  
  113.                         return HashSet(set);
  114.                 }
  115.  
  116.                 bool operator()(const AttrSetID& s0Id, const AttrSetID& s1Id) const
  117.                 {
  118.                         CAttrSet set0;
  119.                         CAttrSet set1;
  120.                         m_owner.BuildSetForLessThanCheck(set0, s0Id);
  121.                         m_owner.BuildSetForLessThanCheck(set1, s1Id);
  122.                         return set0 == set1;
  123.                 }
  124.         };
  125.  
  126. private:
  127.         ILINE void BuildSetForLessThanCheck(CAttrSet& set, const AttrSetID ID) const
  128.         {
  129.                 assert(ID != XMLCPB_INVALID_ID);
  130.  
  131.                 if (ID == SEARCHING_ID)
  132.                 {
  133.                         set = *m_pCheckSet;
  134.                 }
  135.                 else
  136.                 {
  137.                         assert(ID < m_numAttrsArray.size());
  138.                         set.m_numAttrs = m_numAttrsArray[ID];
  139.                         set.m_pHeaders = (uint16*)(m_buffer.GetPointerFromAddr(m_setAddrs[ID]));
  140.                 }
  141.         }
  142.  
  143.         AttrSetID AddSet(const CAttrSet& set);
  144.  
  145.         typedef std::vector<FlatAddr16> FlatAddrVec;
  146.         void CalculateFlatAddrs(FlatAddrVec& outFlatAddrs);
  147.  
  148. private:
  149.  
  150.         std::vector<uint8>                m_numAttrsArray; // stores the number of attrs that each sets has. indexed by id.
  151.         CBufferWriter                     m_buffer;        // all headers are sequentially stored here
  152.         std::vector<CBufferWriter::SAddr> m_setAddrs;      // pointers into m_buffer
  153.         typedef std::unordered_set<AttrSetID, SetHasher, SetHasher, AttrStringAllocator<AttrSetID>> HashTableType;
  154.         HashTableType                     m_sortedIndexes;
  155.         int                               m_maxNumSets;   // used for checks. we need to control it because in the final data we use a limited amount of bits to store the ID.
  156.         CWriter*                          m_pWriter;
  157.         const CAttrSet*                   m_pCheckSet;    // only used when comparing sets inside the GetSetID function. it should be NULL otherwise
  158.         size_t                            m_checkHash;    // ditto
  159. };
  160.  
  161. }  // end namespace
  162.  
  163. #endif
  164.  
downloadXMLCPB_AttrSetTableWriter.h Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top