⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mapptrtoptr.h

📁 遵循C和基本C++标准的Map类
💻 H
字号:
// MapPtrToPtr.h: interface for the CMapPtrToPtr class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_MAPPTRTOPTR_H__01FF8B22_91BF_4461_ADEE_D359EBD9AE32__INCLUDED_)
#define AFX_MAPPTRTOPTR_H__01FF8B22_91BF_4461_ADEE_D359EBD9AE32__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define ASSERT			0
#define ASSERT_VALID	0

#include <windows.h>

//////////////////////////////////////////////////////////////////////////
// CMapPtrToPtr

#define CPlex wincppCPlex
struct CPlex     // warning variable length structure
{
	CPlex* pNext;

	// BYTE data[maxNum*elementSize];

	void* data() { return this+1; }

//	static CPlex*  Create(CPlex*& head, UINT nMax, UINT cbElement);
//			// like 'calloc' but no zero fill
//			// may throw memory exceptions
//
//	void FreeDataChain();       // free this one and links

static	CPlex* CPlex::Create(CPlex*& pHead, UINT nMax, UINT cbElement)
	{
		if( nMax < 0 || cbElement < 0 )
			return NULL;
		CPlex* p = (CPlex*) new BYTE[sizeof(CPlex) + nMax * cbElement];
		// may throw exception
		p->pNext = pHead;
		pHead = p;  // change head (adds in reverse order for simplicity)
		return p;
	}
	
	void CPlex::FreeDataChain()     // free this one and links
	{
		CPlex* p = this;
		while (p != NULL)
		{
			BYTE* bytes = (BYTE*) p;
			CPlex* pNext = p->pNext;
			delete[] bytes;
			p = pNext;
		}
	}

};

struct __POSITION {};
typedef __POSITION* POSITION;

#define BEFORE_START_POSITION ((POSITION)-1L)
class CMapPtrToPtr /*: public CObject*/
{

// 	DECLARE_DYNAMIC(CMapPtrToPtr)
protected:
	// Association
	struct CAssoc
	{
		CAssoc* pNext;

		void* key;
		void* value;
	};

public:

// Construction
	CMapPtrToPtr(int nBlockSize = 10);

// Attributes
	// number of elements
	int CMapPtrToPtr::GetCount() const
	{ return m_nCount; }
	BOOL CMapPtrToPtr::IsEmpty() const
	{ return m_nCount == 0; }

	// Lookup
	BOOL Lookup(void* key, void*& rValue) const;

// Operations
	// Lookup and add if not there
	void*& operator[](void* key);

	// add a new (key, value) pair
	void CMapPtrToPtr::SetAt(void* key, void* newValue)
	{ (*this)[key] = newValue; }

	// removing existing (key, ?) pair
	BOOL RemoveKey(void* key);
	void RemoveAll();

	// iterating all (key, value) pairs
	POSITION GetStartPosition() const	{ return (m_nCount == 0) ? NULL : BEFORE_START_POSITION; }
	//_AFXCOLL_INLINE POSITION CMapPtrToPtr::GetStartPosition() const
	

	void GetNextAssoc(POSITION& rNextPosition, void*& rKey, void*& rValue) const;

	// advanced features for derived classes
	UINT CMapPtrToPtr::GetHashTableSize() const  { return m_nHashTableSize; }
	void InitHashTable(UINT hashSize, BOOL bAllocNow = TRUE);

// Overridables: special non-virtual (see map implementation for details)
	// Routine used to user-provided hash keys
	UINT HashKey(void* key) const;

// Implementation
protected:
	CAssoc** m_pHashTable;
	UINT m_nHashTableSize;
	int m_nCount;
	CAssoc* m_pFreeList;
	struct CPlex* m_pBlocks;
	int m_nBlockSize;

	CAssoc* NewAssoc();
	void FreeAssoc(CAssoc*);
	CAssoc* GetAssocAt(void*, UINT&) const;

public:
	~CMapPtrToPtr();

	void* GetValueAt(void* key) const;
};


#endif // !defined(AFX_MAPPTRTOPTR_H__01FF8B22_91BF_4461_ADEE_D359EBD9AE32__INCLUDED_)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -