<i>Some way to modify complex data structures efficiently</i> status still Open with 3 other changes
by
.
D 2015-05-09T08:59:18.736
J icomment The\sreason\swhy\swe\smodify\sthe\scopy,\sis\sthat\sthere\sare\sstill\slive\sreferences\sto\sthe\soriginal\sinstance\s(refcount>1).\sIt\sturns\sout\sthat\sthere\sis\sno\sother\ssound\sway\sto\sdo\sit,\sgiven\sthe\sstring\ssemantics\sof\sthe\slanguage\s(look\sfor\s"Mutability\spatch"\sin\scomp.lang.tcl\sarchives\sto\switness\sa\sfailed\sattempt\sto\swander\soff).\r\n\r\n\sMost\sTcl\slist-manipulation\sprimitives\sare\soptimized\sto\swork\sin-place\swhen\srefcount\sis\s1.\sSo,\swhen\sit\smatters,\sarrange\sfor\srefcount\sto\sbe\s1.\sNote\sthis\sis\snot\strivial.\sTwo\sgotchas:\r\n\r\n\s\s-\sdon't\spass\sthe\svalue-to-be-modified-in-place\sas\sproc\sargs,\sbecause\sit\swill\sadd\sreferences\sfrom\sthe\s(live)\scall\sstack,\swhich\syou\scannot\snuke.\r\n\r\n\s\s-\suse\sthe\s"K-free-K\strick"\s(like\sthe\sK\scombinator,\swithout\sdefining\sit):\sassuming\sthe\slast\sreference\sis\sin\sthe\s(local)\svar\s"x"\sholding\sit,\spass\sthe\svalue\s\r\n\r\n\s\s\s\s\s$x[unset\sx]\r\n\r\nto\sthe\slist-manipulation\sprimitive.\sE.g,\sfor\sin-place\struncation:\r\n\r\n\s\s\s\s\sset\sx\s[lrange\s$x[unset\sx]\s0\s1000]\r\n\r\nThe\scase\sof\spass-by-varname\sprimitives\sis\sa\sbit\sdifferent.\sLset\sis\sone\ssuch\sbeast.\sThe\scopy-on-write\sprinciple\sapplies\sall\sthe\ssame,\sexcept\sthat\sthere\sis\sno\sneed\sto\sunset\sthe\svar\s(nor\sis\sit\san\soption,\ssince\swe're\spassing\sa\svarname\s!),\sbut\syou\sstill\sneed\sto\smake\ssure\sthe\svar\sis\sthe\sonly\sremaining\sreference.\sWhen\sthis\sis\sso,\sa\s[representation\s$x]\syields\sa\srefcount\sof\s2\s(one\sfor\sthe\svar,\sand\sone\sfor\sthe\sstack\sreference,\si.e.\sthe\sargument\spassed\sto\s[representation]).\r\n\r\nIn\syour\scase,\s$subnode\shas\sa\srefcount\sof\s3\sbecause\sone\sreference\sremains\sin\sthe\scontaining\sdict.\sThis\sprevents\slset\sfrom\sworking\sin-place.\sAgain,\swhen\sit\smatters,\sexplicit\sref\ssurgery\sis\sdue.\sIn\syour\scase,\stemporarily\ssevere\sthe\ssubnode\sfrom\sits\scontainer:\r\n\r\n\s\s\s\s\sset\ssubnode\s[dict\sget\s$complex_datastructure\sc]\r\n\s\s\s\s\sdict\sunset\scomplex_datastructure\sc\r\n\s\s\s\s\slset\ssubnode\s0\s10\r\n\s\s\s\s\sdict\sset\scomplex_datastructure\sc\s$subnode\r\n\r\nNote\sthat,\scontrarily\sto\sthe\sK-free-K,\sthis\sidiom\scan\sbe\shidden\sin\sa\sutility\sfunction,\ssince\sthe\simportant\svalue\sis\sdeep\sinside\sa\scontainer\srather\sthan\spassed\sas\san\sargument.\r\n\r\nSummary:\ssuch\stricks\r\n\r\n\s\s\s\s\s\s-\sare\sa\snecessary\sevil\sdue\sto\sthe\svalue\ssemantics\sin\sTcl;\r\n\s\s\s\s\s\s-\shave\sa\scost\sin\sterms\sof\scode\scomplexity,\sand\sshould\sbe\spulled\sonly\swhen\sit\smatters,\sie\sfor\sbig\svalues;\r\n\s\s\s\s\s\s-\sdon't\shave\sa\sfixed\sor\sobvious\sindexing\salgorithm\s(what\sif\sthe\svalue\sis\sburied\sin\sa\sdict-of-lists-of-dict\s?)\r\n\r\n\r\nFor\sthese\sreasons,\sad\shoc\scode\slike\sthe\sabove\swill\salways\sbe\sthe\sbest\sapproach,\son\sa\scase-by-case\sbasis,\sand\sno\smagical\sprimitive\sin\sthe\score\swill\sease\sthat.\sHence,\sclosing,\ssorry.
J login ferrieux
J mimetype text/plain
K 9ec7bd5f329df1ff790266b3c318a837ac32895e
U ferrieux
Z 05d695771b77521852ba088731a0d13f