動態數組模板類

來源:互聯網
上載者:User

標籤:

本文需要說明的是一種動態數組模板類(Array),可用於自訂的需要連續空間的容器,能有效得利用分配的空間,提供較高效的數組對象操作,和使用引用計數減少記憶體複製拷貝。
 
  Array與std::vector或std::array不同。Array儲存連續的對象,並且在對象記憶體前開闢一份數組
  描述塊對數組進行描述。Array儲存的資料數量是動態,可以通過setLength調整,同時可以使用+、
  +=運算子向數組中追加資料。多個數組執行個體允許使用同一份資料內容,並且不會重複建立內容,這是該數
  組類最重要的特性以及主要涉及目的,即當從一個Array物件建構一個新的Array對象或者將他賦值給其
  他Array時不會產生新的記憶體申請和拷貝。多個Array的資料指向相同的記憶體區,並且通過引用計數管理
  該共同的記憶體區。當所有指向該記憶體區的Array對象都被銷毀後,該記憶體會被釋放。對一個空的Array追
  加內容時,他會自動開闢新的記憶體區用於儲存新資料。對已經包含內容的Array追加或改寫(通過[]下標
  運算)時,如果數組包含的內區還被其他數組引用,則會自動解除對原有記憶體區的引用並建立新的記憶體區
  儲存新的資料。
 
  使用注意:頻繁的對資料進行追加操作是十分低效的,在已經確定要追加數量的情況下先調用setLength或者
  setCapacity設定最終長度或容量再進行追加將提高效能。
  使用注意:Array設計只用於提供資料存取,因此數組中存放的內容不會被調用建構函式和解構函式。

 具體的代碼實現如下:

template <typename T>class Array{public:typedef T TYPE;struct ArrayDesc{volatile int refer;volatile unsigned int capacity;volatile unsigned int length;};protected:T* m_ptr;public:Array(){m_ptr = NULL;}Array(const Array<T> &another){m_ptr = NULL;operator = (another);}Array(const T* ptr, size_t length){m_ptr = NULL;setLength(length);memcpy(m_ptr, ptr, length * sizeof(*ptr));}Array(size_t length){m_ptr = NULL;setLength(length);}~Array(){setLength(0);}inline Array<T>& operator = (const Array<T> &another){if (m_ptr != another.m_ptr)//檢查不是同一個對象{setLength(0);//釋放本對象的記憶體if (another.m_ptr){ArrayDesc *pDesc = ((ArrayDesc*)another.m_ptr) - 1;//擷取另一對象的數組描述lock_inc(pDesc->refer);//原子操作增加數組描述的引用計數m_ptr = (T*)(pDesc + 1);//把另一對象的數組指標複製到本對象的數組指標}}return *this;}inline Array<T> operator + (const Array<T> &another){Array<T> result;if (m_ptr || another.m_ptr){size_t myCount = length();size_t anotherCount = another.length();result.setLength(myCount + anotherCount);//預留目標的記憶體空間T* destPtr = result.m_ptr;if (myCount > 0){memcpy(destPtr, m_ptr, myCount * sizeof(*m_ptr));//拷貝本對象的資料記憶體destPtr += myCount;}if (anotherCount > 0){memcpy(destPtr, another.m_ptr, anotherCount * sizeof(*m_ptr));//拷貝另一個對象的資料記憶體destPtr += myCount;}}return result;//拷貝複製結果對象}inline Array<T> operator + (const T &another)//追加一個資料對象,返回的是新的資料對象的拷貝{size_t myCount = length();Array<T> result(myCount + 1);//預留結果對象的記憶體空間if (myCount > 0)memcpy(result.m_ptr, m_ptr, myCount * sizeof(*m_ptr));//拷貝本對象的資料到目標記憶體result.m_ptr[myCount] = another;//拷貝追加的資料對象return result;}inline Array<T>& operator += (const Array<T> &another)//追加一個動態數組對象{size_t len = another.length();if (len > 0 )cat(another.m_ptr, another.length());return *this;}inline Array<T>& operator += (const T &another)//追加一個資料對象,返回的是本對象{size_t myCount = length();setLength(myCount + 1);m_ptr[myCount] = another;return *this;}inline T& operator [] (size_t index)//擷取動態數組的資料索引上的資料對象的引用{if (!m_ptr)return *(T*)NULL;ArrayDesc *pDesc = ((ArrayDesc*)m_ptr) - 1;assert(index < pDesc->length);//檢查索引不超出動態數組對象的長度duplicate();return m_ptr[index];}inline const T& operator [] (size_t index) const //擷取動態數組的資料索引上的資料對象的引用(cosnt類型){if (!m_ptr)return *(T*)NULL;ArrayDesc * const pDesc = ((ArrayDesc* const)m_ptr) - 1;assert(index < pDesc->length);//檢查索引不超出動態數組對象的長度return m_ptr[index];}inline bool operator == (const Array<T> &another) const//判斷資料對象是否是同一個對象{return m_ptr == another.m_ptr;}inline bool operator != (const Array<T> &another) const//判斷資料對象是否不是同一個對象{return m_ptr != another.m_ptr;}inline const T* ptr() const//擷取唯讀內容資料指標{return m_ptr;}inline T* own_ptr()//擷取可寫的內容資料指標{duplicate();//複製本對象的資料內容到一個新的對象上,本對象就成了一個新的對象,舊的對象的資料記憶體需要在別處有管理return m_ptr;}inline size_t length() const//擷取本對象的資料數組長度{if (!m_ptr)return 0;ArrayDesc * const pDesc = ((ArrayDesc* const)m_ptr) - 1;return pDesc->length;}inline Array<T>& cat(const T* ptr, size_t count)//追加一個或連續的多個資料對象到本數組對象的末尾{return insert(length(), ptr, count);}inline Array<T>& insert(size_t index, const T* ptr, size_t count)//在指定索引位置插入一個或者連續的多個資料對象{if (count > 0){ArrayDesc *pMyDesc = m_ptr ? ((ArrayDesc*)m_ptr) - 1 : NULL;size_t myCount = pMyDesc ? pMyDesc->length : 0;//本數組的資料對象數assert(index <= myCount);//判斷目標是否是來自自己記憶體地區的資料,如果是來自自己記憶體地區的,那麼可能//在setLength後ptr指標失效,此時需要重新確定ptr的位置。bool isMySelf = false;size_t inMyPos = 0;if (pMyDesc && (ptr >= m_ptr) && (ptr <= (m_ptr + pMyDesc->capacity))){isMySelf = true;//目標對象的起始地址在本對象數組的記憶體地區內inMyPos = ptr - m_ptr;//在記憶體地區的索引位置if (inMyPos >= index)inMyPos += count;//重新置放需要拷貝的對象的起始位置ptr(跟setLength的實現有關)}setLength(myCount + count);//預留本對象的資料記憶體大小if (index < myCount)memmove(&m_ptr[index + count], &m_ptr[index], sizeof(*ptr) * (myCount - index));//拷貝會被覆蓋的資料記憶體到末尾。當記憶體發生局部重疊的時候,memmove保證拷貝的結果是正確的,memcpy不保證拷貝的結果的正確if (isMySelf)memcpy(&m_ptr[index], &m_ptr[inMyPos], count * sizeof(*m_ptr));//拷貝需要拷貝的資料對象到指定的位置else memcpy(&m_ptr[index], ptr, count * sizeof(*m_ptr));}return *this;}inline Array<T>& remove(size_t index, size_t count){if (count > 0){duplicate();//拷貝本數組對象到新的記憶體ArrayDesc *pMyDesc = m_ptr ? ((ArrayDesc*)m_ptr) - 1 : NULL;size_t myCount = pMyDesc ? pMyDesc->length : 0;assert(index < myCount);if (count > myCount - index) count = myCount - index;size_t copyCount = myCount - index - count;if (copyCount > 0)memcpy(&m_ptr[index], &m_ptr[index + count], sizeof(*m_ptr) * copyCount);setLength(myCount - count);}return *this;}inline Array<T>& setLength(const size_t length){assert(length >= 0);ArrayDesc *pDesc = NULL;if (m_ptr) pDesc = ((ArrayDesc*)m_ptr) - 1;if (length == 0)//回收本數組對象{if (pDesc){if (lock_dec(pDesc->refer) <= 0)//引用計數為0,釋放本數組對象的記憶體free(pDesc);m_ptr = NULL;}}else if (pDesc){if (length != pDesc->length){if (pDesc->refer > 1)//引用計數大於1時返回的是記憶體拷貝的數組對象{lock_dec(pDesc->refer);//減少本對象的引用計數ArrayDesc *pNewDesc = (ArrayDesc*)calloc(1, sizeof(*pDesc) + sizeof(*m_ptr) * (length + 1));//分配新的記憶體對象,calloc分配連續記憶體,初始化已指派的記憶體為0,返回數組pNewDesc->capacity = (unsigned int)length;pNewDesc->length = (unsigned int)length;pNewDesc->refer = 1;m_ptr = (T*)(pNewDesc + 1);//把本數組對象重新置放到新的數組對象記憶體size_t lengthMin = pDesc->length < length ? pDesc->length : length;memcpy(m_ptr, pDesc + 1, lengthMin * sizeof(*m_ptr));//拷貝本數組對象記憶體到新的對象的記憶體}else //引用計數為1是返回的就是本對象,有效長度和記憶體大小根據需求調整{if (length >= pDesc->capacity)//需要的記憶體大小大於本數組對象的包含的記憶體大小{pDesc = (ArrayDesc*)realloc(pDesc, sizeof(*pDesc) + sizeof(*m_ptr) * (length + 1));m_ptr = (T*)(pDesc + 1);//重新置放本數組對象的資料指標到新的記憶體位址memset(&m_ptr[pDesc->length], 0, sizeof(*m_ptr) * (length - pDesc->capacity + 1));//把數組對象末尾的無效對象初始化pDesc->capacity = (unsigned int)length;//記憶體大小pDesc->length = (unsigned int)length;//數組大小}else pDesc->length = (unsigned int)length;//截短本數組對象的有效長度}memset(&m_ptr[length], 0, sizeof(*m_ptr));//數組有效長度後面的那個對象初始化為0(在數組對象為字串時有作用)}}else //數組資料對象指標沒有記憶體,則分配新的記憶體並把本數組對象資料指標重新置放到新的記憶體上{ArrayDesc *pNewDesc = (ArrayDesc*)calloc(1, sizeof(*pDesc) + sizeof(*m_ptr) * (length + 1));pNewDesc->capacity = (unsigned int)length;pNewDesc->length = (unsigned int)length;pNewDesc->refer = 1;m_ptr = (T*)(pNewDesc + 1);}return *this;}inline size_t capacity() const{if (m_ptr){ArrayDesc *pDesc = ((ArrayDesc*)m_ptr) - 1;return pDesc->capacity;}return 0;}inline Array<T>& setCapacity(size_t capacity){size_t myCount = length();//容量必須大於數量。如果要進行清空操作,則請調用setLength(0)if (capacity > myCount){setLength(capacity);ArrayDesc *pDesc = ((ArrayDesc*)m_ptr) - 1;pDesc->length = myCount;}return *this;}protected:inline void duplicate()//拷貝複製本數組對象到新的記憶體{if (m_ptr){ArrayDesc *pDesc = ((ArrayDesc*)m_ptr) - 1;//記錄本數組對象的描述塊指標if (pDesc->refer > 1){ArrayDesc *pNewDesc = (ArrayDesc*)malloc(sizeof(*pDesc) + sizeof(*m_ptr) * (pDesc->capacity + 1));//多預留了一個資料對象的大小pNewDesc->capacity = pDesc->capacity;pNewDesc->length = pDesc->length;pNewDesc->refer = 1;m_ptr = (T*)(pNewDesc + 1);//把本數組對象重新置放到新的記憶體memcpy( m_ptr,pDesc + 1,, (pDesc->length + 1) * sizeof(*m_ptr));//拷貝本數組對象的原來的資料記憶體到新的數組對象記憶體lock_dec(pDesc->refer);}}}};




 

動態數組模板類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.