{"id":486,"date":"2014-07-24T07:38:34","date_gmt":"2014-07-24T07:38:34","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=486"},"modified":"2018-05-21T12:43:11","modified_gmt":"2018-05-21T12:43:11","slug":"aligning-diff-output-2","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2014\/07\/aligning-diff-output-2\/","title":{"rendered":"Aligning Diff Output"},"content":{"rendered":"<p><a title=\"The Blog is Back!\" href=\"http:\/\/www.dyalog.com\/blog\/2014\/07\/blog-is-back\/\">&#8216;Bots are off limits this week<\/a> so here is a story from <a title=\"Iverson College\" href=\"https:\/\/sites.google.com\/site\/iversoncollege\/\" target=\"_blank\" rel=\"noopener\">this year\u2019s Iverson College<\/a> \u2013 a fantastic week spent in the company of a wonderful mixture of array and functional language gurus and newbies, all learning from each other. One evening, <a title=\"Dhru Patel on Google+\" href=\"https:\/\/plus.google.com\/107468290228981686556\/about\" target=\"_blank\" rel=\"noopener\">Dhru Patel<\/a> presented a problem that he was working on which involved displaying the results of a \u201cdiff\u201d side by side with the matched rows aligned. For example, the input might be:<\/p>\n<pre><code class=\"language-apl\">      OLD NEW\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u2502This    \u2502This    \u2502\r\n\u2502is the  \u2502original\u2502\r\n\u2502original\u2502is not  \u2502\r\n\u2502text    \u2502        \u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<p>Edited by <a title=\"Roger on Wikipedia\" href=\"http:\/\/en.wikipedia.org\/wiki\/Roger_Hui\" target=\"_blank\" rel=\"noopener\">Yoda<\/a>, the text was. The selection of rows to be aligned requires finding the longest sequence of rows from the original data that matches rows in the edited data without ever skipping backwards through either sets of data. In this case, it is the original first and third row, matching the first two edited rows. We will return to how we might identify these rows in a future blog entry (meanwhile, try to <a title=\"Grok - Wikipedia\" href=\"http:\/\/en.wikipedia.org\/wiki\/Grok\" target=\"_blank\" rel=\"noopener\">grok<\/a> John Scholes&#8217; YouTube video on <a title=\"Depth First Search in APL (YouTube)\" href=\"https:\/\/www.youtube.com\/watch?v=DsZdfnlh_d0\" target=\"_blank\" rel=\"noopener\">Depth First Searching<\/a> and think about it). For now, we will provide this information as a left argument, a vector of Boolean vectors that marks the location of the matched rows:<\/p>\n<pre><code class=\"language-apl\">\u00a0\u00a0\u00a0\u00a0\u00a0 (1 0 1 0)(1 1 0) AlignMatched OLD NEW\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u2502This    \u2502This    \u2502\r\n\u2502is the  \u2502        \u2502\r\n\u2502original\u2502original\u2502\r\n\u2502text    \u2502        \u2502\r\n\u2502        \u2502is not  \u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<p>At Iverson College there was general agreement that \u201cthere should be a non-looping solution\u201d, and <a title=\"Devon McCormick at Iverson College\" href=\"https:\/\/sites.google.com\/site\/iversoncollege\/participants\/devon-mccormick\" target=\"_blank\" rel=\"noopener\">Devon McCormick<\/a> immediately stated that he would bet that it involved grade (<code>\u234b<\/code>). Let us explore:<\/p>\n<pre><code class=\" language-apl\">      masks\u2190(<\/code><code class=\"&quot;language-apl\">1 0 1 0)(1 1 0)<\/code><code class=\" language-apl\">\r\n      \u2395\u2190matched\u2190\u220amasks \u235d The two match masks catenated together\r\n 1 0 1 0 1 1 0 \r\n      \u2395\u2190origin\u2190(\u2262\u00a8OLD NEW)\/0 1 \u235d 0 for items from the old array, 1 for new\r\n 0 0 0 0 1 1 1 \r\n      \u2395\u2190block\u2190\u220a+\\\u00a8masks \u235d running count of matched rows\r\n 1 1 2 2 1 2 2 \u00a0 \r\n      \u2395\u2190data\u2190block,(~matched),origin,OLD\u236aNEW\r\n1 0 0 This    \r\n1 1 0 is the  \r\n2 0 0 original\r\n2 1 0 text    \r\n1 0 1 This    \r\n2 0 1 original\r\n2 1 1 is not  <\/code><\/pre>\n<p>Our goal is to create an expansion mask for each argument; this is going to insert blank lines at the points where a non-matched row from the other argument is included. The next step is to reorder everything by ascending block number, and within each block move the matched rows to the front (ascending by ~matched), as follows:<\/p>\n<pre><code class=\"language-apl\">\u00a0\u00a0\u00a0\u00a0  \u2395\u2190data\u2190data[\u234bdata[;1 2];]\r\n1 0 0 This    \r\n1 0 1 This    \r\n1 1 0 is the  \r\n2 0 0 original\r\n2 0 1 original\r\n2 1 0 text    \r\n2 1 1 is not \r\n<\/code><\/pre>\n<p>This contains all the items from both texts in the order that they would need to appear in the final results. We can extract the reordered flag vectors:<\/p>\n<pre><code class=\"language-apl\">       matched\u2190~data[;2] \u22c4 origin\u2190data[;3]<\/code><\/pre>\n<p>Now, (origin=0) is an expansion mask that would expand OLD to match the above, and (origin=1) would do the same for NEW:<\/p>\n<pre><code class=\"language-apl\">      0 1 {(origin=\u237a)\u2340\u2375}\u00a8OLD NEW\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u2502This    \u2502        \u2502\r\n\u2502        \u2502This    \u2502\r\n\u2502is the  \u2502        \u2502\r\n\u2502original\u2502        \u2502\r\n\u2502        \u2502original\u2502\r\n\u2502text    \u2502        \u2502\r\n\u2502        \u2502is not  \u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<p>To align the matched rows, we need to eliminate inserted blanks that correspond to matched rows from the other side:<\/p>\n<pre><code class=\" language-apl\">      0 1{((~matched\u2227origin\u2260\u237a)\/origin=\u237a)\u2340\u2375}\u00a8OLD NEW\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u2502This    \u2502This    \u2502\r\n\u2502is the  \u2502        \u2502\r\n\u2502original\u2502original\u2502\r\n\u2502text    \u2502        \u2502\r\n\u2502        \u2502is not  \u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<p>In the final function, which collects the relevant lines of code from our experiments, we do not create the temporary &#8220;data&#8221; matrix, but reorder the origin and matched vectors individually \u2013 and instead of doing grade up on a two-column matrix, we compute an integer vector that will sort by descending matched within ascending block (because we know that grade up on a simple small-range integer vector will run like greased lightning):<\/p>\n<pre><code class=\"language-apl\">\u00a0AlignMatched\u2190{\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u235d align matched rows of 1\u2283\u2375 and 2\u2283\u2375\r\n\u00a0\u00a0\u00a0\u00a0 matched\u2190\u220a\u237a\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u235d\u00a0matches are marked by 1\u2283\u237a and 2\u2283\u237a\r\n\u00a0\u00a0\u00a0\u00a0 origin\u2190(\u2262\u00a8\u2375)\/0 1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u235d\u00a0identify origin of items in matched (0=old. 1=new)\r\n\u00a0\u00a0\u00a0\u00a0 block\u2190\u220a+\\\u00a8\u237a\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u235d running count of matched rows\r\n\u00a0\u00a0\u00a0\u00a0 order\u2190\u234b(2\u00d7block)-matched\u00a0 \u235d Order so matching rows are adjacent and order of\r\n\u00a0\u00a0\u00a0\u00a0 (origin matched)\u2190(\u2282\u2282order)\u2337\u00a8origin matched \u235d items following matched row is preserved\r\n\u00a0\u00a0\u00a0\u00a0 0 1{((~matched\u2227origin\u2260\u237a)\/origin=\u237a)\u2340\u2375}\u00a8\u2375 \u235d Expand exluding matched from \"other\" list\r\n\u00a0}\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>Reviving Lost Arts<\/h2>\n<p>The algorithm above makes use of techniques that were well-known in APL circles in the 1980s, but atrophied after nested arrays arrived on the scene and applications tended to keep parts of the data in separate leaves of an array rather than using simple data structures.<\/p>\n<p>If you would like to read up on some of the old techniques, you might enjoy browsing the\u00a0<a title=\"FinnAPL Idiom Library (PDF)\" href=\"http:\/\/www.finnapl.fi\/texts\/IdiLib.pdf\" target=\"_blank\" rel=\"noopener\">FinnAPL Idiom Library<\/a>, and Bob Smith&#8217;s immortal <a title=\"Boolean Functions and Techniques (PDF)\" href=\"http:\/\/www.sudleyplace.com\/APL\/boolean.pdf\" target=\"_blank\" rel=\"noopener\">Boolean Functions and Techniques<\/a>. Although nested arrays might have made them a little less relevant in the 90s and 00s, the search for high-performance parallel solutions could bring them back, as explained in this session from Dyalog &#8217;12, on <a title=\"Segmented Scans and Nested Data Parallelism\" href=\"https:\/\/dyalog.tv\/Dyalog12\/?v=sLZOQ4XWqbshttp:\/\/\" target=\"_blank\" rel=\"noopener\">Segmented Scans and Nested Data Parallelism<\/a> by Andrzej Filinski.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8216;Bots are off limits this week so here is a story from this year\u2019s Iverson College \u2013 a fantastic week spent in the company of a wonderful mixture of array and functional language gurus and newbies, all learning from each &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2014\/07\/aligning-diff-output-2\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,8,12],"tags":[],"class_list":["post-486","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-apl-language","category-cto"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=486"}],"version-history":[{"count":46,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/486\/revisions"}],"predecessor-version":[{"id":5169,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/486\/revisions\/5169"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}