{"id":10,"date":"2013-02-22T20:11:20","date_gmt":"2013-02-23T04:11:20","guid":{"rendered":"http:\/\/192.168.1.2\/wordpress\/?p=10"},"modified":"2013-03-06T14:22:32","modified_gmt":"2013-03-06T22:22:32","slug":"dyanmic-array-template","status":"publish","type":"post","link":"http:\/\/cywang.no-ip.org\/wordpress\/?p=10","title":{"rendered":"Dynamic Array Template"},"content":{"rendered":"<p>\n\tReference: Data Structures and Algorithms with Object-Oriented Design Patterns in C++\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<pre class=\"brush:cpp;\">\r\n#include &lt;iostream&gt;\r\n#include &lt;stdexcept&gt; \/\/ std::out_of_range\r\nusing namespace std;\r\n\r\ntemplate &lt;class T&gt;\r\n<\/pre>\n<p>\n\t<!--more-->\n<\/p>\n<pre class=\"brush:cpp;\">\r\nclass Array\r\n{\r\nprotected:\r\n&nbsp;&nbsp;&nbsp; T* data;\r\n&nbsp;&nbsp;&nbsp; unsigned int base;\r\n&nbsp;&nbsp;&nbsp; unsigned int length;\r\npublic:\r\n&nbsp;&nbsp;&nbsp; Array();\r\n&nbsp;&nbsp;&nbsp; Array(unsigned int, unsigned int = 0);\r\n&nbsp;&nbsp;&nbsp; ~Array();\r\n\r\n&nbsp;&nbsp;&nbsp; Array(Array const&amp;);\r\n&nbsp;&nbsp;&nbsp; Array&amp; operator = (Array const&amp;);\r\n\r\n&nbsp;&nbsp;&nbsp; T const&amp; operator [] (unsigned int) const;\r\n&nbsp;&nbsp;&nbsp; T&amp; operator [] (unsigned int);\r\n\r\n&nbsp;&nbsp;&nbsp; T const* Data() const;\r\n&nbsp;&nbsp;&nbsp; unsigned int Base() const;\r\n&nbsp;&nbsp;&nbsp; unsigned int Length() const;\r\n\r\n&nbsp;&nbsp;&nbsp; void SetBase(unsigned int);\r\n&nbsp;&nbsp;&nbsp; void SetLength(unsigned int);\r\n};\r\n\r\ntemplate &lt;class T&gt;\r\nArray&lt;T&gt;::Array()\r\n{\r\n&nbsp;&nbsp;&nbsp; data = new T[0];\r\n&nbsp;&nbsp;&nbsp; base = 0;\r\n&nbsp;&nbsp;&nbsp; length = 0;\r\n}\r\n\r\ntemplate &lt;class T&gt;\r\nArray&lt;T&gt;::Array(unsigned int n, unsigned int m)\r\n{\r\n&nbsp;&nbsp;&nbsp; data = new T[n];\r\n&nbsp;&nbsp;&nbsp; base = m;\r\n&nbsp;&nbsp;&nbsp; length = n;\r\n}\r\n\r\ntemplate &lt;class T&gt;\r\nArray&lt;T&gt;::Array(Array&lt;T&gt; const&amp; array)\r\n{\r\n&nbsp;&nbsp;&nbsp; data = new T[array.length];\r\n&nbsp;&nbsp;&nbsp; base = array.base;\r\n&nbsp;&nbsp;&nbsp; length = array.length;\r\n&nbsp;&nbsp;&nbsp; for(unsigned int i = 0; i&lt;length; i++)\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data[i] = array.data[i];\r\n}\r\n\r\ntemplate &lt;class T&gt;\r\nArray&lt;T&gt;::~Array()\r\n{\r\n&nbsp;&nbsp;&nbsp; delete [] data;\r\n}\r\n\r\ntemplate &lt;class T&gt;\r\nT const* Array&lt;T&gt;::Data() const\r\n{\r\n&nbsp;&nbsp;&nbsp; return data;\r\n}\r\n\r\ntemplate &lt;class T&gt;\r\nunsigned int Array&lt;T&gt;::Base() const\r\n{\r\n&nbsp;&nbsp;&nbsp; return base;\r\n}\r\n\r\ntemplate &lt;class T&gt;\r\nunsigned int Array&lt;T&gt;::Length() const\r\n{\r\n&nbsp;&nbsp;&nbsp; return length;\r\n}\r\n\r\ntemplate &lt;class T&gt;\r\nT const&amp; Array&lt;T&gt;::operator [] (unsigned int position) const\r\n{\r\n&nbsp;&nbsp;&nbsp; unsigned int const offset = position - base;\r\n&nbsp;&nbsp;&nbsp; if(offset &gt;= length)\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw out_of_range(&quot;invalid position&quot;);\r\n&nbsp;&nbsp;&nbsp; return data[offset];\r\n}\r\n\r\ntemplate &lt;class T&gt;\r\nT&amp; Array&lt;T&gt;::operator [] (unsigned int position)\r\n{\r\n&nbsp;&nbsp;&nbsp; unsigned int const offset = position - base;\r\n&nbsp;&nbsp;&nbsp; if(offset &gt;= length)\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw out_of_range(&quot;invalid position&quot;);\r\n&nbsp;&nbsp;&nbsp; return data[offset];\r\n}\r\n\r\ntemplate &lt;class T&gt;\r\nvoid Array&lt;T&gt;::SetBase(unsigned int newBase)\r\n{\r\n&nbsp;&nbsp;&nbsp; base = newBase;\r\n}\r\n\r\ntemplate &lt;class T&gt;\r\nvoid Array&lt;T&gt;::SetLength(unsigned int newLength)\r\n{\r\n&nbsp;&nbsp;&nbsp; T* const newData = new T[newLength];\r\n&nbsp;&nbsp;&nbsp; unsigned int const min = length &lt; newLength ? length : newLength;\r\n&nbsp;&nbsp;&nbsp; for(unsigned int i = 0; i&lt;min; i++)\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newData[i] = data[i];\r\n&nbsp;&nbsp;&nbsp; delete [] data;\r\n&nbsp;&nbsp;&nbsp; data = newData;\r\n&nbsp;&nbsp;&nbsp; length = newLength;\r\n}\r\n\r\nint main()\r\n{\r\n&nbsp;&nbsp;&nbsp; Array&lt;int&gt; array(5,0);\r\n&nbsp;&nbsp;&nbsp; array[0] = 10;\r\n&nbsp;&nbsp;&nbsp; array[1] = 20;\r\n&nbsp;&nbsp;&nbsp; return 0;\r\n}<\/pre>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reference: Data Structures and Algorithms with Object-Oriented Design Patterns in C++ &nbsp; #include &lt;iostream&gt; #include &lt;stdexcept&gt; \/\/ std::out_of_range using namespace std; template &lt;class T&gt;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[7],"tags":[4,5],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-cc","tag-dynamic-array","tag-template"],"_links":{"self":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10"}],"version-history":[{"count":19,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":110,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions\/110"}],"wp:attachment":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}