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

📄 exarray_h.htm

📁 illustrate using std and stl libriry
💻 HTM
字号:
<html>



<head>

<meta http-equiv="Content-Type" content="text/html">

<meta name="GENERATOR" content="Microsoft FrontPage 3.0">

<title>exarray.h</title>

</head>



<body>



<pre>/*	exarray.h.



	A template of dynamic arrays with automatic check of an index.

	(minimal edition).



	(C) R.N.Shakirov, IMach of RAS (UB), 1998 - 2000

	All Rights Reserved.

*/

template &lt;class T&gt; class exarray

{

  T*		e;	// Base array

  unsigned	len;	// Num of elements



//	Memory allocation methods (see below).



  void realloc (unsigned size); 

  void grow    (unsigned i); 



//	Private copy constructor &amp; copy assignment:

//	assignnent of dynamic arrays is locked.



  exarray                (const exarray&lt;T&gt;&amp;);

  exarray&lt;T&gt;&amp; operator = (const exarray&lt;T&gt;&amp;);



public:



//	Constructor of dynamic array.



  exarray ()	{ e = 0; len = 0;	}



//	Destructor of dynamic array.



  ~exarray()	{ realloc (0);		}



//	Access to array with check of bounds.

//	If index is out of range, the array grows,

//	extra memory is filled with zeros and

//	constructors of extra elemetns is called.



  T&amp;    operator [] (unsigned i)

   { if (len&lt;=i) grow(i); return e[i];  }



  T&amp;    operator [] (int i)

   { return (operator [] ((unsigned)i));}



  T&amp;    operator *  ()

   { if (len&lt;=0) grow(0); return e[0];  }



//	Access to array without check of bounds.



  T&amp;	item (unsigned i) { return e[i];}

  T*	base ()		  { return e;   }



//	Auto conversion to const T*.



  operator const T* ()	  { return e;   }

};



//	Declarations of memory allocation

//	functions from exarray.cpp.



void	exmrealloc (void **p, unsigned size, unsigned oldsize);

unsigned excalcsize (unsigned size);



//	Function exmuladd computes n*s+k

//	within int range 0..~0u&gt;&gt;1.

//	In the case of overflow returns ~0u.



inline unsigned exmuladd (unsigned s, unsigned n, unsigned k)

{ return ((n&lt;=((~0u&gt;&gt;1)-k)/s)? n*s+k: ~0u);}



//	Stub class for call of constructors and

//	destructors without memory allocation.



template &lt;class T&gt; struct __EXRELOC

{

  T value;				

  void* operator new (size_t, void* p)

                        { return p; }

  void  operator delete (void*) { ; }

};



//	Private method realloc sets

//	the dynamic array size (in bytes)

//	and call default constructors of

//	destructors of elements.



template &lt;class T&gt; void exarray&lt;T&gt;::realloc (unsigned size)

{

  unsigned n = size/sizeof (T);



  while (len &gt; n)	// Destructors

  { len--; delete (__EXRELOC&lt;T&gt; *)(e + len);}



  exmrealloc		// Memory allocation

	((void **)&amp;e, size, sizeof(T)*len);



  while (len &lt; n)	// Default constructors

  { new (e + len)__EXRELOC&lt;T&gt;; len++; }

}



//	Private method grow allocates memory for

//	elements with indexes up to i (at least).

//	For efficiency extra allocation is provided.



template &lt;class T&gt; void exarray&lt;T&gt;::grow (unsigned i)

{

  realloc (excalcsize (

    exmuladd (sizeof(T), i, sizeof(T))));

}

</pre>

</body>

</html>

⌨️ 快捷键说明

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