{"id":4735,"date":"2019-01-26T08:03:52","date_gmt":"2019-01-26T08:03:52","guid":{"rendered":"https:\/\/www.gtechbooster.com\/?p=4459"},"modified":"2019-01-26T08:03:00","modified_gmt":"2019-01-26T08:03:00","slug":"the-lost-art-of-storage-mapping-function-part-2","status":"publish","type":"post","link":"https:\/\/gtechbooster.com\/the-lost-art-of-storage-mapping-function-part-2\/","title":{"rendered":"The Lost Art of Storage Mapping Function &#8211; Part 2"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted\"><strong>continuation of part 1<\/strong><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Key Mapping and Trees<\/h2>\n\n\n\n<p>The two-dimensional array SMF is beginning to look a little more interesting but it still nothing more than an application of the idea of using a function f(key) to locate the data corresponding a key.<\/p>\n\n\n\n<div class=\"gtech-migrated-from-ad-inserter-placement-2\" style=\"text-align: center;\" id=\"gtech-1307025492\"><div style=\"margin-left: auto;margin-right: auto;text-align: center;\" id=\"gtech-2282995438\"><a data-bid=\"1\" data-no-instant=\"1\" href=\"https:\/\/gtechbooster.com\/linkout\/78735\" rel=\"noopener\" class=\"notrack\" aria-label=\"005\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/gtechbooster.com\/media\/2026\/03\/005.webp\" alt=\"\"  srcset=\"https:\/\/gtechbooster.com\/media\/2026\/03\/005.webp 1000w, https:\/\/gtechbooster.com\/media\/2026\/03\/005-768x768.webp 768w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" width=\"500\" height=\"500\"  style=\"display: inline-block;\" \/><\/a><\/div><\/div><p>In the case of the one-dimensional array the key is simply the index i and the location of a[i] is given by f(i) and in the two-dimensional case the keys are i and j and the location of a[i,j] is given by f(i,j). Higher dimensional arrays can be treated in the same way.<\/p>\n\n\n\n<p>At this point my guess is that many a high-level language programmer will be thinking how could an SMF be at all useful to me?<\/p>\n\n\n\n<p>After all, high-level languages keep programmers well away from real memory locations. The answer is that while most languages provide simple data structures such as arrays, they often don&#8217;t provide the rarer data structures such as trees. Even when they do there are times when an SMF implementation of a simple or exotic data structure can be useful.<\/p>\n\n\n\n<p>The trick is to use an array as if it was a chunk of old-fashioned linear memory and a suitable SMF to map the new data structure onto the array.<\/p>\n\n\n\n<p>Puzzled?<\/p>\n\n\n\n<p>Well let&#8217;s try a simple binary tree implementation.<\/p>\n\n\n\n<p>A binary tree is a tree structure such that each node, apart from the final or terminal nodes, has exactly two children &#8211; hence the binary in binary tree.<\/p>\n\n\n\n<p>Each node is associated with a value that takes n bytes to store then a suitable SMF is:<\/p>\n\n\n\n<p><code>location = base + n*i + n*(2^j-1)<\/code><\/p>\n\n\n\n<p>which gives the location of the value stored at ith node at the jth level. That is we are using i and j to &#8220;index&#8221; the tree by node number i at level j.<\/p>\n\n\n\n<p>To see why this works consider the following small binary tree:<\/p>\n\n\n\n\n\n<p>and so on.<\/p>\n\n\n\n<p>Notice that going from one level to the next doubles the number of nodes and you should be able to work out that the number of nodes at level j is simply 2<sup>j<\/sup>.<\/p>\n\n\n\n<p>With this insight you should now also be able to see how the SMF works. The tree is stored as lines of nodes one level at a time. That is:<\/p><div class=\"gtech-mid-cont\" style=\"text-align: center;\" id=\"gtech-2060284619\"><div style=\"margin-right: auto;margin-left: auto;text-align: center;\" id=\"gtech-2735215342\"><a data-bid=\"1\" data-no-instant=\"1\" href=\"https:\/\/gtechbooster.com\/linkout\/75343\" rel=\"noopener\" class=\"notrack\" aria-label=\"jesdphis\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/gtechbooster.com\/media\/2025\/08\/jesdphis.avif\" alt=\"\"  srcset=\"https:\/\/gtechbooster.com\/media\/2025\/08\/jesdphis.avif 1179w, https:\/\/gtechbooster.com\/media\/2025\/08\/jesdphis-768x950.avif 768w\" sizes=\"(max-width: 1179px) 100vw, 1179px\" width=\"300\" height=\"300\"  style=\"display: inline-block;\" \/><\/a><\/div><\/div>\n\n\n\n<p><code>location &nbsp;&nbsp; 0 &nbsp; 1 2 &nbsp; 3  4  5  6<br>\nnode &nbsp; &nbsp; &nbsp;&nbsp; 0 | 0  1 | 0  1  2  3<br>\nlevel &nbsp; &nbsp; &nbsp; 0 | 1 &nbsp; | 2<\/code><\/p>\n\n\n\n<p>To make use of this SMF in a high-level language all you have to do is declare an array of elements that will hold the node values and use the SMF with n=1 to determine which element node i at level j is stored in.<\/p>\n\n\n\n<p>For example, if the array is a[N] then node i at level j is stored in a[i+2^j-1].<\/p>\n\n\n\n<p>You can see the general idea. If you have a data structure which has a regular pattern then you need to find a function that maps the regular pattern to a linear sequence of storage locations.<\/p>\n\n\n\n<p>Basically you need a function which enumerates the elements of the data structure one after another. Inventing a storage mapping function can be tricky but its always possible &#8211; unless the data structure can&#8217;t be enumerated.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A Practical Application &#8211; A Database Array<\/h2>\n\n\n\n<p>If binary trees sound too unlikely to gain your interest in SMFs then perhaps it is worth pointing out their use with respect to disk files.<\/p>\n\n\n\n<p>For example, suppose you need a 2D array bigger than RAM will allow then as long as you have a fast hard disk available what could be easier than implementing a virtual array using a random access file or a database table.<\/p>\n\n\n\n<p>Simply set up a random access file such that each record can hold exactly one value and then when you want to access the value of a[i,j] seek record number i + n*j where n is the size of the first dimension.<\/p>\n\n\n\n<p>You can probably work out for yourself various ways of making the process more efficient &#8211; for example by defining a record large enough to hold a complete row of the array etc. The same principles apply to creating any virtual data structure on disk &#8211; store each element of the data structure in a record and use an SMF to find which record corresponds to each element.<\/p>\n\n\n\n<p>It is also worth pointing out that this is how databases are implemented. In this case you set up a file of a given size and consider it to consist of fixed size records. If each record uses b Bytes of storage then the location of the ith record is obviously:<\/p>\n\n\n\n<p><code>location=i*b<\/code><\/p>\n\n\n\n<p>and this is were you perform a random access seek to and read in b bytes to get the ith record.<\/p>\n\n\n\n<p>In many&nbsp;cases you don&#8217;t lookup the database using the record number but a more general key. In this case there is an extra step &#8211; lookup the record number you need in a key table. &nbsp;In other words, if you want to find the record containing the data corresponding to &#8220;key&#8221; you first look &#8220;key&#8221; up in the index which gives the record number.<\/p>\n\n\n\n<p>This works in all cases unless the record size is variable when you need a different approach.<\/p>\n\n\n\n<p>Credit: <a href=\"https:\/\/www.i-programmer.info\/babbages-bag\/476-storage-mapping-functions.html?start=1\"><strong>I-Programmer, <span class=\"small\">Harry Fairhead<\/span><\/strong><\/a><\/p>\n<div class=\"gtech-end-cont\" id=\"gtech-2518692732\"><div style=\"margin-right: auto;margin-left: auto;text-align: center;\" id=\"gtech-213534223\"><a data-bid=\"1\" data-no-instant=\"1\" href=\"https:\/\/gtechbooster.com\/linkout\/76065\" rel=\"noopener\" class=\"notrack\" aria-label=\"26002\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/gtechbooster.com\/media\/2025\/10\/26002.jpg\" alt=\"\"  srcset=\"https:\/\/gtechbooster.com\/media\/2025\/10\/26002.jpg 1200w, https:\/\/gtechbooster.com\/media\/2025\/10\/26002-768x768.jpg 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" width=\"500\" height=\"500\"  style=\"display: inline-block;\" \/><\/a><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>The two-dimensional array SMF is beginning to look a little more interesting but it still nothing more than an&#8230;<\/p>\n","protected":false},"author":7,"featured_media":4463,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[124,6,677],"class_list":["post-4735","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-features","tag-bitmap","tag-programming","tag-random-access-memory"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"_links":{"self":[{"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/posts\/4735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/comments?post=4735"}],"version-history":[{"count":0,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/posts\/4735\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/media\/4463"}],"wp:attachment":[{"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/media?parent=4735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/categories?post=4735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/tags?post=4735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}