åé¡ã®å£°æ
ãã¡ãããããããå®å šæ°žç¶æ§ãå®è£ ããŸããããã¯ãäžéããŒãžã§ã³ãèªã¿åãå°çšã¢ãŒãã ãã§ãªãããã€ã§ãèŠçŽ ãè¿œå ããã³æœåºã§ããããšãæå³ããŸãã ããã«ããã¡ãããéæ°žç¶çãªãªãã·ã§ã³ãã€ãŸãOïŒnïŒã®å Žåãšåãåäœæéãšè¿œå ã®ã¡ã¢ãªã®æŒžè¿çãªåäœãå¿ èŠã§ããããã§ãnã¯ãã¥ãŒã§å®è¡ãããæäœã®ç·æ°ã§ãã ãšããã§ãèŠä»¶ãOïŒn log nïŒã«ç·©åãããšããã¥ãŒã¯æé»çãªããŒãæã€æ°žç¶çãªãã«ã«ãããªãŒã䜿çšããŠç°¡åã«ãšãã¥ã¬ãŒããããŸãã
次ã®ç°¡åãªã€ã³ã¿ãŒãã§ãŒã¹ã䜿çšããŠãããŒã¿æ§é ãæäœããŸãããã¥ãŒã®çµæã®ããŒãžã§ã³ã«ã¯ãè² ã§ãªãæŽæ°ã§çªå·ãä»ããããŸããæåã¯ãçªå·0ã®ç©ºã®ãã¥ãŒã®ã¿ã§ãããã¥ãŒã®NããŒãžã§ã³ïŒå ã®ç©ºãå«ãïŒã«ã0ããã®çªå·ãä»ããããŠããŸãN-1ã«å€æŽãããšã次ã®4ã€ã®ã¯ãšãªãå®äºããããšãã§ããŸãã
- emptyïŒquery_idïŒ-æå®ãããçªå·ã®ãã¥ãŒã空ãã©ããã確èªããŸãã
- frontïŒquery_idïŒ-ãã¥ãŒã®æåã®èŠçŽ ãè¿ããŸããããã¥ãŒèªäœã¯ãŸã£ããå€æŽããããæ°ãããã¥ãŒã¯äœæãããŸããã ãã¥ãŒã空ã§ãªãå Žåãæäœã¯æå¹ã§ãã
- pushïŒquery_idãnew_elementïŒ-å ã®æ°ããèŠçŽ ã®æ«å°Ÿã«è¿œå ããããšã§ååŸãããçªå·Nã®äžã«æ°ãããã¥ãŒãäœæããŸãã å€ãããŒãžã§ã³ã¯query_idã§åŒãç¶ã䜿çšã§ããŸãã
- popïŒquery_idïŒ-å ããæåã®èŠçŽ ãæœåºããããšã§ååŸãããçªå·Nã§æ°ãããã¥ãŒãäœæããŸãã å ã®ãã¥ãŒã¯å€ãçªå·ã§åŒãç¶ãå©çšã§ããŸãã å ã®ãã¥ãŒã空ã ã£ãå Žåãæ°ãããã¥ãŒã空ã«ãªããŸãã
ã¹ã¿ãã¯ã䜿çšãããã¥ãŒã®ã·ãã¥ã¬ãŒã·ã§ã³
ãã¹ãŠã®åé¡ã«å¯ŸããŠãç°¡åã§ãéããééã£ã解決çããããŸãã
-ãªã³ã©ã€ã³è£å€å®ã®ç¬¬äžæ³å
ãåç¥ã®ããã«ãæ°žç¶ã¹ã¿ãã¯ã¯éåžžã«åçŽãªããŒã¿æ§é ã§ããããã®æŒžè¿çãªåäœãå¿ èŠã§ãã ããã«ã2ã€ã®ã¹ã¿ãã¯ã䜿çšããŠãã¥ãŒãã·ãã¥ã¬ãŒãã§ããããšãããã£ãŠããŸãã æãããªã¢ã€ãã¢ããããŸãããããã®2ã€ã®ã¹ã¿ãã¯ãæ°žç¶åããŠãåé¡ã解決ããŸãã æ®å¿µãªããããã®ãããªåçŽãªã¢ãããŒãã¯æ©èœããŸããã åé¡ã¯ããã®ã·ãã¥ã¬ãŒã·ã§ã³ã§ã¯ããã¹ãŠã®æäœã«æŒžè¿ç·OïŒ1ïŒãããããã§ã¯ãªããšããããšã§ãïŒpop'eäžã«èŠçŽ ãååŸããã¹ã¿ãã¯ã空ã«ãªã£ãå Žåãå¥ã®ã¹ã¿ãã¯ãããã¹ãŠã®èŠçŽ ã転éããŸãã éåžžã®ãã¥ãŒã®å ŽåãåèŠçŽ ã¯1åã ãã·ãããããããã挞è¿çãªæ¯ãèãã®åèšã¯OïŒnïŒã®ãŸãŸã§ãããæ°žç¶çãªå ŽåãåèŠçŽ ã¯å€ãã®ãã¥ãŒã«å±ããããã«å¿ããŠæ°åã·ãããããŸãã ããšãã°ãqããã®ã¹ã¿ãã¯ã空ã®ãã¥ãŒã®çªå·ãšããåä¿¡ãããã¥ãŒã®pushïŒqã1ïŒããã³pushïŒqã2ïŒã®åŸããã®ã¹ã¿ãã¯ã¯ç©ºã®ãŸãŸã«ãªãããã¥ãŒã®åèŠçŽ ãã·ãããããŸãq 2åã é 眮ãããèŠçŽ ã®å ±å䜿çšãæŽçããããšã¯ãããããã·ããããããšã«ãã£ãŠåŸãããã¹ã¿ãã¯ã®æäžäœã®èŠçŽ ãç°ãªãïŒãããã1ããã³2ïŒãããåèŠçŽ ããã®äžã®èŠçŽ ãžã®ãã€ã³ã¿ãæ ŒçŽããããã«é 眮ããããããäžå¯èœã§ãããããã£ãŠããããã®å Žåãã察å¿ããæåŸã瀺ãæåŸãã2çªç®ã®èŠçŽ ã®ã³ããŒã2ã€å¿ èŠã«ãªããŸãããããã£ãŠããã§ãŒã³å ã®ç®çã®æåŸãã2çªç®ã瀺ãããã«æåŸãã2çªç®ã®ã³ããŒãå¿ èŠã«ãªããŸãã ã
ããã§ãããã®ãããªèãã«åºã¥ããã¢ã«ãŽãªãºã ããã ïŒ2ã€ã®ã¹ã¿ãã¯ã䜿çšããŠãã¥ãŒãã·ãã¥ã¬ãŒãïŒãpop'eããååŸããã¹ã¿ãã¯ã空ã«ãªããªãããšãä¿èšŒãã6ã€ã®ã¹ã¿ãã¯ã䜿çšããŠãããä¿èšŒããŸãã ç§ã®ã¢ã«ãŽãªãºã ã¯ãã®ã¢ã€ãã¢ãæ瀺çã«äœ¿çšããŠããŸããããäžè¬çãªç¹ã«ã€ããŠã¯ãã¡ããããªãã¯ãããèŠã€ããããšãã§ããŸãã
ã¢ã«ãŽãªãºã ã®èª¬æ
æ°žç¶ã¹ã¿ãã¯ãå®è£ ãããšããšåãæ§é ã䜿çšããŠã¿ãŸãããïŒè¿œå ãããèŠçŽ ãããªãŒïŒããæ£ç¢ºã«ã¯ãããªãŒ-ãã©ã¬ã¹ãã®ã»ããïŒã®åœ¢ã§ä¿åããŸããåé ç¹ã«ã¯ãè¿œå ãããèŠçŽ ãšãã¥ãŒã®åã®èŠçŽ ïŒããæ£ç¢ºã«ã¯ã察å¿ããèŠçŽ ïŒãžã®ãã€ã³ã¿ãŒããããŸãåã®èŠçŽ ã¯ããªãŒã®æäžéšã§ãïŒã ãã¥ãŒèŠçŽ ã®å°æ¥ã«ã€ããŠèšãã°ãèŠçŽ ã«å¯Ÿå¿ããé ç¹ãæå³ããããšããããããŸãã
ãã®å Žåã®ç©ºã§ãªããã¥ãŒã¯ããã¥ãŒã®æåãšæåŸã®èŠçŽ ãžã®ãã€ã³ã¿ãŒã®ãã¢ã§è¡šãããšãã§ããæåã®èŠçŽ ã«å¯Ÿå¿ããé ç¹ã¯ãå¿ ãæåŸã«å¯Ÿå¿ããé ç¹ã®ç¥å ã«ãªããŸãïŒ1ã€ã®èŠçŽ ã®ãã¥ãŒã®å Žåã¯ãäžèŽããŸãïŒã ãã®ãããªæ§é ãšãã®äžã®ãã¥ãŒã®è¡šç€ºã®äŸïŒ
ããã·ã¥æäœã¯åã«æ°ããé ç¹ãäœæããå ã®ãã¥ãŒã®æåŸã®èŠçŽ ã®èŠªãšããŠèšå®ããŸãïŒãŸãã¯ç©ºã®ã«ãŒãã«è¿œå ãããå Žåã¯æ°ããã«ãŒãã«ãªããŸãïŒããããæäœã¯ãã©ã¬ã¹ãã«ãŸã£ãã觊ãããåã«æåã®èŠçŽ ã«ãã€ã³ã¿ãŒã移åããŸãã ãããã£ãŠãäœæåŸãé ç¹ã¯äœæ¥ãçµäºãããŸã§å€æŽãããªããããæ°ããæäœãæ¢åã®ãã¥ãŒãå°ç¡ãã«ããããšãå¿é ããå¿ èŠã¯ãããŸããã
ãã®ã¢ãããŒãã®å¯äžã®åé¡ã¯ããããæäœã®å®è£ ã§ãïŒä»ã®3ã€ã¯ç°¡åã§ãïŒãæåã®èŠçŽ ã«ãã€ã³ã¿ãŒã移åããé ç¹ã決å®ããæ¹æ³ã¯æ確ã§ã¯ãããŸãããè€æ°ã®ããŒã¹ãååšããå¯èœæ§ãããããããã®ãã¹ãŠãæ ŒçŽãããŠããå Žåã§ãããããã®ã©ãããã¥ãŒã®æ¬¡ã®èŠçŽ ã«å¯Ÿå¿ãããã¯å®å šã«äžæã§ãã ãããã£ãŠã3ã€ä»¥äžã®èŠçŽ ãååšããåãã¥ãŒã«å¯ŸããŠãåçŽã«æ¥ç¶ããããªã¹ãã®èŠçŽ ãžã®ãã€ã³ã¿ãŒãè¿œå ã§ä¿åããŸãããã€ã³ã¿ãŒã«ãããªã¹ãèŠçŽ ã«ã¯ããã¥ãŒã®2çªç®ã®èŠçŽ ãžã®ãã€ã³ã¿ãŒãšããªã¹ãå ã§ãã®äžã«ããèŠçŽ ã3çªç®ãžã®ãã€ã³ã¿ãŒãå«ãŸããŸãã 4çªç®ãªã©ã åæãã¥ãŒãžã®ãã®ãããªãã€ã³ã¿ãŒããããšãããããå®è¡ãããšãã«ããã¥ãŒã®æ°ããããããç°¡åã«ç¹å®ã§ããããã«ãªããŸããçµæã®ãã¥ãŒã®ãªã¹ãé ç®ãžã®ãã€ã³ã¿ãŒã¯ããã®ãªã¹ãã®æ¬¡ã®èŠçŽ ãžã®ãã€ã³ã¿ãŒã«ãããŸããã
ãã ãã2ã€ã®ã¹ã¿ãã¯ã«ãããã¥ãŒã®ãšãã¥ã¬ãŒã·ã§ã³ãæ©èœããªãçç±ãšåæ§ã®çç±ã§ããªã¹ãã®äžã®åãã¥ãŒã®äžéé ç¹ãå®å šã«ãã¹ãŠã§ããããšãä¿èšŒããããã«ãæåããŸããã次ã«ããããã®æåããããŠãã®ãªã¹ããå°ãããªãå§ããããæ°ãããªã¹ããäœæããã¹ãããããšã«ç§»åããŸãïŒã€ãŸããããã·ã¥ãŸãã¯ãããæäœããšã«æ§ç¯äžã®ãªã¹ãã1èŠçŽ ãã€å¢ãããŸãïŒãç§ãã¡ã®æšã®ããŒã¯ã®èŠªã« ããããå€ããªã¹ããå®äºããæç¹ã«ç¶æããããšãæã ã¯ãã§ã«æ°ããã¢ã©ãŒããæã£ãŠããŸããã
ããæ£åŒã«ã¯ããã¥ãŒããšã«ãéå§ãšçµäºãžã®ãã€ã³ã¿ãšå®æãããªã¹ãã®èŠçŽ ãžã®ãã€ã³ã¿ã«å ããŠãæ§ç¯äžã®ãªã¹ãã®èŠçŽ ãžã®ãã€ã³ã¿ãä¿åãïŒäœãäœæããªãå Žåã¯0ã«ãªããŸãïŒãæ°ããå€ãèšç®ããŸã次ã®åçŽãªã¢ã«ãŽãªãºã ã䜿çšãããã¥ãŒïŒå ã®push'aãŸãã¯pop'aãžã®é©çšåŸã«åä¿¡ïŒïŒ
- æåã®ãã¥ãŒã®ãã®ãã€ã³ã¿ãŒã0ã«çããå Žåãæ°ãããªã¹ãã®äœæãéå§ããæéã§ãããã©ããã確èªããŸãïŒãã®ãããªç¢ºèªã®åºæºã¯ä»¥äžã§èª¬æããŸãïŒ
- æéãçµéãããããªã¹ãå ã®æ¬¡ã®é ç®ãžã®ãã«ãã€ã³ã¿ãŒã䜿çšããŠãªã¹ãã¢ã€ãã ãäœæãïŒããã«ãããäœæãããã¢ã€ãã ãäœææžã¿ãªã¹ãã®æåŸã«ãªããŸãïŒããã€ã³ã¿ãŒããã¥ãŒã®æåŸãã2çªç®ã®èŠçŽ ïŒæåŸã®ã¢ã€ãã ã®èŠªïŒã®å é ã«é 眮ãã
- ãŸã æéããªãå Žåã¯ããã€ã³ã¿ãŒããŒãã®ãŸãŸã«ããŸãã
- ãŒã以å€ã®ãœãŒã¹ãã€ã³ã¿ãŒã®å Žåããªã¹ãã1èŠçŽ äžã«å±éããŸãããªã¹ãå ã®æ¬¡ã®é ç¹ãšããŠãœãŒã¹ãæãããã®ãœãŒã¹èŠçŽ ã«å¯Ÿå¿ãã芪ãæãæ°ããèŠçŽ ãäœæããŸãã
åæã«ãæåã®ãã¥ãŒã«ä¿åãããŠãããœãŒã¹ãªã¹ãèŠçŽ èªäœãŸãã¯ãããžã®ãã€ã³ã¿ãŒãå€æŽãããäžè¬ã«ããã¹ãŠã®ããŒã¿ïŒããªãŒãããããªã¹ãã¢ã€ãã ããã¥ãŒã«ä¿åãããŠããããŒã¿ïŒã¯äžå€ã§ãïŒã€ãŸããäœæåŸïŒæå³ãå€ããããšã¯ãããŸããïŒã ãã¹ãŠãã©ã®ããã«æ©èœããpush'eãŸãã¯pop'eã§äœãèµ·ãããã瀺ãå³ãå³ã«è¡šç€ºãããŠããŸãïŒç解ããããããããã«ãã©ããªã«é 匵ã£ãŠãæ©èœããŸããã§ããïŒã
ãããã£ãŠããªã¹ãã®å®äºæã«æåã®äžéèŠçŽ ã«å°éãããšããã®ãã¥ãŒã®æ§ç¯ãå®äºããŸãïŒä»ã®ããã€ãã®ãã¥ãŒã¯ãã®ãªã¹ããå®äºãç¶ããå¯èœæ§ããããŸãããå€ãèŠçŽ ã¯æ±ºããŠå€ãããªããããããã¯æ±ºããŠå®³ãäžããŸããïŒã ãã®å Žåãå€ããªã¹ãã®èŠçŽ ãžã®ãã€ã³ã¿ã®ä»£ããã«ãæ°ãããªã¹ãã®èŠçŽ ãžã®ãã€ã³ã¿ãèšè¿°ããæ§ç¯ããããªã¹ããžã®ãã€ã³ã¿ããŒãã«å²ãåœãŠãŸãã
ã¢ã«ãŽãªãºã ã®èª¬æãããåãªã¯ãšã¹ãäžã«ããªãŒã®æ°ããé ç¹ã1ã€ãšæ°ãããªã¹ãã¢ã€ãã ã1ã€ã ãäœæããOïŒ1ïŒæéãã€ãŸãæ±ãã挞è¿çãªåäœãè²»ãããæ£ããããšãè°è«ããã ãã§ããããšãããããŸãåºæºã
建èšéå§æã®ãã§ãã¯åºæº
ãªã¹ãã®ãµã€ãºãäžéé ç¹ïŒã€ãŸããæåãšæåŸã®ãªãé ç¹ïŒã®æ°ã®1/2以äžã§ããéã¯ãäœãããããªã¹ããå°ãããªããšããã«æ°ãããªã¹ãã®äœæãéå§ããŸãã äžéã®é ç¹ããªãå ŽåãäžçåŒ0â¥0ããããäœãããªãããšã«æ³šæããŠãã ãããã€ãŸããç¹å¥ãªå Žåãããã¯å¿ èŠãããŸããã ãã®ã¢ãããŒãã§ã¯ãå€ããªã¹ããçµäºããæ°ãããªã¹ãããŸã æºåãããŠããªãç¶æ³ã§èªåèªèº«ãèŠã€ããããšãã§ããªãããšã蚌æããŸãã
ãŸãã次ã®äžå€åŒã蚌æããŸããæ°ãããªã¹ããäœæããå Žåãåã¹ãããã§2k + l = sãkã¯å€ããªã¹ãã®èŠçŽ æ°ãlã¯äœæããããªã¹ãïŒãã®ã¹ãããã§äœæãå®äºããåŸïŒã«ãããsã¯äžéãªã¹ãã®ç·æ°ã§ãèŠçŽ ã æ°åŠçåž°çŽæ³ã§èšŒæããŸãã æ§ç¯ãéå§ããã°ããã®æåã®ã¹ããããèããŸãïŒl = 1ïŒã ãã ããæ°ãããã¥ãŒ2k-s <0ïŒåºæºïŒã§ã¯ãå ã®2k old -s oldâ¥0ã§ããããšã«æ³šæããŠãã ããããããã©ã®ããã«çºçããããèŠãŠã¿ãŸãããã + 1ããããã®å Žåãk = k old -1ããã³s = s old -1.ã芧ã®ãšãããã©ã¡ãã®å Žåã§ã2k-sã¯2k old -sã ã1 å€ãã§ããäž¡æ¹ã®éãã¯æŽæ°ã§ãã ã2çªç®ã®èŠçŽ ã¯0ã1çªç®ã®èŠçŽ ã¯-1ã§ãããããã£ãŠã2k + 1 = sã§ãããlã¯1ã§ãã èªå°ããŒã¹ã蚌æãããŠããŸãã 移è¡ã蚌æããŸãããïŒããã€ãã®æ§ç¯ã¹ãããã§2k old + l old = s oldã«ããŸãã 次ã«ãl = l old + 1ïŒ1èŠçŽ ã§ãªã¹ããå®æïŒãpush'aã®å ŽåïŒk = k old ãs = s old + 1ãpop'aã®å ŽåïŒk = k old -1ãs = s old -1.ã©ã¡ãã®å Žåããå¹³çãæç«ããŸããã€ãŸããã¹ããŒãã¡ã³ãã蚌æãããŸãã
ãããæäœãå®è¡ããŠããŠãå¿ èŠãªãªã¹ãããœãŒã¹ãã¥ãŒã§ç©ºã§ãããšä»®å®ããŸãïŒããã·ã¥ããå Žåããã®ãªã¹ãã¯äžå䜿çšããŸããïŒã 次ã«ã次ã®2ã€ã®ããããããããŸãã
- ãŸãã¯ãæåã®ãã¥ãŒã«ã€ããŠã¯ãæ°ãããªã¹ãã®äœæãéå§ããªãã£ãããããã®ãã¥ãŒã«ã¯2kâ¥sãããã
k = 0 => s =0ãããã¯ããµã€ãº0ã1ããŸãã¯2ã®ãã¥ãŒã§ãããpop'aãå®äºããããã®æåŸã®èŠçŽ ãååã§ããããšãæå³ããŸãã - äžå€2k + l = sãk = 0 => l = sâ 0 =>ã®ãããã§ãéå§ãããŠããlâ 0ã«ãªããæ§ç¯ããããªã¹ãã¯ç©ºã§ã¯ãªãããã¹ãŠã®äžéèŠçŽ ãå«ãã§ããŸããã空ã§ãªããªã¹ããååŸããŸãã
æ§ç¯ãå®äºãããšãã«ãæ§ç¯ã®æ¬ åŠã®æ¡ä»¶ãæºããããŠããããšãã€ãŸããçµæã®ãªã¹ãã®é·ããäžéé ç¹ã®ç·æ°ã®1/2以äžã§ããããšãã€ãŸããå®äºæã«2lâ¥sããŸãã¯åçã«lâ¥s -lã ããããs-lãšã¯äœã§ããïŒ ããã¯ããªã¹ãã«ãªãäžéé ç¹ã®æ°ã§ãã ããã¯ãæ§ç¯ããã»ã¹äžã«çºçããããã·ã¥æäœã®æ°ã«çããããšãç解ããã®ã¯ç°¡åã§ãã ãã ãããã®ãããªåæäœã§ã¯ããªã¹ãå ã®èŠçŽ ã®æ°ã1ãã€å¢å ããŸãïŒåã®æé ã§ãäœæããããªã¹ãã®èŠçŽ ããã¥ãŒã®3çªç®ã®èŠçŽ ã«å¯Ÿå¿ããŠãããããæ°ãããã¥ãŒã®å Žåããªã¹ãèŠçŽ ã¯æåã®äžéèŠçŽ ã«å¯Ÿå¿ããããã«ãªããŸãã ããã ããããã¯æ§ç¯ã®æåŸã®ã¹ãããã§ã®ã¿å¯èœã§ããããæäœã§ã®ã¿å¯èœã§ãã ãããã£ãŠãlâ¥ãã®ãããªããã·ã¥ã®æ°ïŒå³å¯ã«ãã以äžã§ããããšãç解ããããšã¯é£ãããããŸããïŒãããã蚌æããå¿ èŠããããŸããã
æ確ã«ããããã«ã次ã®2ã€ã®æ¥µç«¯ãªã±ãŒã¹ã§äœãèµ·ãããã瀺ããŸããåžžã«ããããããšããšãåžžã«ããã·ã¥ãããšãã§ãã
C ++å®è£
æåŸã«ãäžèšã®ããŒã¿æ§é ãC ++ã§å®è£ ããŸãïŒå®è£ ã§ã¯C ++ 11ã®é©æ°ã䜿çšããŸãïŒã ãã®ã³ãŒãããããã¿ã€ããšããŠäœ¿çšãã䟡å€ã¯ãããŸããã§ããã ãã·ã³ãã«ã§çããä»ã®ãã¹ãŠãç ç²ã«ããŠãããæºè¶³ãããããã«ããããšããŸããã
persistent_queue.h
#ifndef PERSISTENT_QUEUE_H #define PERSISTENT_QUEUE_H #include <cstdlib> #include <vector> using Element_type = int; class Persistent_queue { public: Persistent_queue(); ~Persistent_queue(); Persistent_queue(const Persistent_queue&) = delete; Persistent_queue& operator =(const Persistent_queue&) = delete; using Queue_id = size_t; static const Queue_id start_queue_id = 0; Queue_id push(Queue_id queue_id, const Element_type& value); Queue_id pop(Queue_id queue_id); const Element_type& front(Queue_id queue_id); size_t size(Queue_id queue_id); bool empty(Queue_id queue_id); private: struct TreeNode; struct QueueIntermediateTreeNodeList; struct Queue { const TreeNode* const first; const TreeNode* const last; const size_t size; const QueueIntermediateTreeNodeList* const known_intermediate_list; const QueueIntermediateTreeNodeList* const constructing_intermediate_list; const bool own_last; const bool own_known_intermediate_list; Queue(const TreeNode* const first, const TreeNode* const last, const size_t size, const QueueIntermediateTreeNodeList* const known_intermediate_list, const QueueIntermediateTreeNodeList* const constructing_intermediate_list, const bool own_last, const bool own_known_intermediate_list); ~Queue() = default; Queue(Queue&& source) = default; // needed for vector reallocation Queue(const Queue&) = delete; Queue& operator =(const Queue&) = delete; }; std::vector<Queue> queues_; Queue_id register_new_queue(const TreeNode* const first, const TreeNode* const last, const size_t size, const QueueIntermediateTreeNodeList* const known_intermediate_list, const QueueIntermediateTreeNodeList* const constructing_intermediate_list, const bool own_last, const bool own_known_intermediate_list); void manage_intermediate_lists(const QueueIntermediateTreeNodeList*& known_intermediate_list, const QueueIntermediateTreeNodeList*& constructing_intermediate_list, bool& own_known_intermediate_list, const TreeNode* const first, const TreeNode* const last, const size_t size); }; #endif // PERSISTENT_QUEUE_H
persistent_queue.cpp
#include "persistent_queue.h" #include <cassert> struct Persistent_queue::TreeNode { const TreeNode* const parent; const Element_type element; TreeNode(const TreeNode* const parent, const Element_type& value) : parent(parent), element(value) {} ~TreeNode() = default; TreeNode(const TreeNode&) = delete; TreeNode& operator =(const TreeNode&) = delete; }; struct Persistent_queue::QueueIntermediateTreeNodeList { const Persistent_queue::TreeNode* const front; const QueueIntermediateTreeNodeList* const next; const size_t size; QueueIntermediateTreeNodeList(const Persistent_queue::TreeNode* const front, const QueueIntermediateTreeNodeList* const tail_list) : front(front), next(tail_list), size{tail_list ? tail_list->size + 1 : 1} { assert(front); } ~QueueIntermediateTreeNodeList() = default; QueueIntermediateTreeNodeList(const QueueIntermediateTreeNodeList&) = delete; QueueIntermediateTreeNodeList& operator =(const QueueIntermediateTreeNodeList&) = delete; }; Persistent_queue::Queue::Queue( const Persistent_queue::TreeNode* const first, const Persistent_queue::TreeNode* const last, const size_t size, const Persistent_queue::QueueIntermediateTreeNodeList* const known_intermediate_list, const Persistent_queue::QueueIntermediateTreeNodeList* const constructing_intermediate_list, const bool own_last, const bool own_known_intermediate_list ) : first(first), last(last), size(size), known_intermediate_list(known_intermediate_list) , constructing_intermediate_list(constructing_intermediate_list) , own_last(own_last), own_known_intermediate_list(own_known_intermediate_list) { // Some asserts if (size == 0) { assert(first == nullptr); assert(last == nullptr); } else { assert(first); assert(last); if (size > 1) assert(last->parent); } if (size <= 2) { assert(known_intermediate_list == nullptr); assert(constructing_intermediate_list == nullptr); if (size == 1) assert(first == last); if (size == 2) assert(first == last->parent); } else { assert(known_intermediate_list); assert(first == known_intermediate_list->front->parent); } } size_t Persistent_queue::size(const Persistent_queue::Queue_id queue_id) { return queues_.at(queue_id).size; } bool Persistent_queue::empty(const Queue_id queue_id) { return size(queue_id) == 0; } const Element_type& Persistent_queue::front(const Persistent_queue::Queue_id queue_id) { assert(!empty(queue_id)); return queues_.at(queue_id).first->element; } Persistent_queue::Queue_id Persistent_queue::register_new_queue(const TreeNode* const first, const TreeNode* const last, const size_t size, const QueueIntermediateTreeNodeList* const known_intermediate_list, const QueueIntermediateTreeNodeList* const constructing_intermediate_list, const bool own_last, const bool own_known_intermediate_list) { queues_.emplace_back(first, last, size, known_intermediate_list, constructing_intermediate_list, own_last, own_known_intermediate_list); return queues_.size() - 1; } Persistent_queue::Persistent_queue() { register_new_queue(nullptr, nullptr, 0, nullptr, nullptr, false, false); } Persistent_queue::~Persistent_queue() { for (const auto& q : queues_) { if (q.own_last) delete q.last; if (q.own_known_intermediate_list) delete q.known_intermediate_list; delete q.constructing_intermediate_list; } } Persistent_queue::Queue_id Persistent_queue::push(const Persistent_queue::Queue_id queue_id, const Element_type& value) { const auto& queue_for_push = queues_.at(queue_id); const size_t size = queue_for_push.size + 1; const bool own_last = true; const TreeNode* first; const TreeNode* last; if (queue_for_push.size == 0) { first = last = new TreeNode(nullptr, value); } else { first = queue_for_push.first; last = new TreeNode(queue_for_push.last, value); } bool own_known_intermediate_list; const QueueIntermediateTreeNodeList* known_intermediate_list = queue_for_push.known_intermediate_list; const QueueIntermediateTreeNodeList* constructing_intermediate_list = queue_for_push.constructing_intermediate_list; manage_intermediate_lists(known_intermediate_list, constructing_intermediate_list, own_known_intermediate_list, first, last, size); return register_new_queue(first, last, size, known_intermediate_list, constructing_intermediate_list, own_last, own_known_intermediate_list); } Persistent_queue::Queue_id Persistent_queue::pop(const Persistent_queue::Queue_id queue_id) { const auto& queue_for_pop = queues_.at(queue_id); const bool own_last = false; const TreeNode* first; const TreeNode* last; size_t size; const QueueIntermediateTreeNodeList* known_intermediate_list; if (queue_for_pop.size <= 1) { first = last = nullptr; size = 0; known_intermediate_list = nullptr; } else { last = queue_for_pop.last; size = queue_for_pop.size - 1; if (queue_for_pop.size == 2) { first = queue_for_pop.last; known_intermediate_list = nullptr; } else { assert(queue_for_pop.known_intermediate_list != nullptr); first = queue_for_pop.known_intermediate_list->front; known_intermediate_list = queue_for_pop.known_intermediate_list->next; } } bool own_known_intermediate_list; const QueueIntermediateTreeNodeList* constructing_intermediate_list = queue_for_pop.constructing_intermediate_list; manage_intermediate_lists(known_intermediate_list, constructing_intermediate_list, own_known_intermediate_list, first, last, size); return register_new_queue(first, last, size, known_intermediate_list, constructing_intermediate_list, own_last, own_known_intermediate_list); } void Persistent_queue::manage_intermediate_lists( const Persistent_queue::QueueIntermediateTreeNodeList*& known_intermediate_list, const Persistent_queue::QueueIntermediateTreeNodeList*& constructing_intermediate_list, bool& own_known_intermediate_list, const Persistent_queue::TreeNode* const first, const Persistent_queue::TreeNode* const last, const size_t size) { own_known_intermediate_list = false; const size_t intermediate_nodes_count = (size > 2 ? size - 2 : 0); size_t known_intermediate_list_size = (known_intermediate_list ? known_intermediate_list->size : 0); if (2*known_intermediate_list_size < intermediate_nodes_count) { auto try_to_replace_known_to_constructing = [&](){ if (constructing_intermediate_list && constructing_intermediate_list->front->parent == first) { known_intermediate_list = constructing_intermediate_list; known_intermediate_list_size = constructing_intermediate_list->size; constructing_intermediate_list = nullptr; return true; } return false; }; if (!try_to_replace_known_to_constructing()) { const auto adding_node = (constructing_intermediate_list ? constructing_intermediate_list->front->parent : last->parent); constructing_intermediate_list = new QueueIntermediateTreeNodeList( adding_node, constructing_intermediate_list); if (try_to_replace_known_to_constructing()) own_known_intermediate_list = true; } } // Check invariants if (2*known_intermediate_list_size >= intermediate_nodes_count) assert(constructing_intermediate_list == nullptr); const size_t constructing_intermediate_list_size = (constructing_intermediate_list ? constructing_intermediate_list->size : 0); const auto invariant_sum = 2*known_intermediate_list_size + constructing_intermediate_list_size; assert(invariant_sum >= intermediate_nodes_count); if (constructing_intermediate_list) assert(invariant_sum == intermediate_nodes_count); }
TreeNodeãQueueãããã³QueueIntermediateTreeNodeListã¿ã€ãã¯ãã¢ã«ãŽãªãºã ã®ããªãŒã®æäžéšããã¥ãŒãããã³åçŽã«æ¥ç¶ããããªã¹ãã®èŠçŽ ã«å¯Ÿå¿ããŸãã Queueã®ããŒã«å€æ°ã¯ãã¯ã©ã¹ã®ãã¹ãã©ã¯ã¿ã§ã¡ã¢ãªãæ£ãã解æŸããããã«äœ¿çšãããŸãïŒããªãŒã®é ç¹ãšãªã¹ãã¢ã€ãã ã¯ãããããäœæãããã¥ãŒã«ãã£ãŠåé€ãããŸãïŒäžèšã®ããã«ãåãªã¯ãšã¹ãã§é ç¹ã¯1ã€ã ãããªã¹ãã¢ã€ãã ã¯1ã€ã ãäœæãããŸãïŒã ããªãŒã®æäžéšã¯ãããã·ã¥æäœäžã«ã®ã¿äœæããããããžã®ãã€ã³ã¿ã¯ããããæåŸã«æžã蟌ãŸããŸããown_lastã¯ãæ°ããé ç¹ãšå€ãé ç¹ã®ã©ã¡ããæåŸã«æžã蟌ãŸãããã瀺ããŸãã äœæäžã«äœæããããªã¹ãã¢ã€ãã ã¯ãã»ãšãã©ã®å Žåãconstructing_intermediate_listã«æžã蟌ãŸããŸãããã ããäœæãããåãã¹ãããã§ã®äœæãå®äºããå Žåã¯äŸå€ã§ãããã®ãããªå Žåã
2çªç®ã®æçœã§ãªãå¯èœæ§ã®ãããã€ã³ãã¯ããã©ã€ããŒãã¡ã³ããŒé¢æ°manage_intermediate_listsã§ã次ã®ããã«ãªããŸãã
void Persistent_queue::manage_intermediate_lists( const Persistent_queue::QueueIntermediateTreeNodeList*& known_intermediate_list, const Persistent_queue::QueueIntermediateTreeNodeList*& constructing_intermediate_list, bool& own_known_intermediate_list, const Persistent_queue::TreeNode* const first, const Persistent_queue::TreeNode* const last, const size_t size) { own_known_intermediate_list = false; const size_t intermediate_nodes_count = (size > 2 ? size - 2 : 0); size_t known_intermediate_list_size = (known_intermediate_list ? known_intermediate_list->size : 0); if (2*known_intermediate_list_size < intermediate_nodes_count) { auto try_to_replace_known_to_constructing = [&](){ if (constructing_intermediate_list && constructing_intermediate_list->front->parent == first) { known_intermediate_list = constructing_intermediate_list; known_intermediate_list_size = constructing_intermediate_list->size; constructing_intermediate_list = nullptr; return true; } return false; }; if (!try_to_replace_known_to_constructing()) { const auto adding_node = (constructing_intermediate_list ? constructing_intermediate_list->front->parent : last->parent); constructing_intermediate_list = new QueueIntermediateTreeNodeList( adding_node, constructing_intermediate_list); if (try_to_replace_known_to_constructing()) own_known_intermediate_list = true; } } // Check invariants }
ãã®é¢æ°ã¯ãæ§ç¯äžã®ãªã¹ããžã®ãã€ã³ã¿ãŒãæäœããããã®äžèšã®ã¢ã«ãŽãªãºã ãå®è£ ããŸãïŒæåã®3ã€ã®ãã©ã¡ãŒã¿ãŒã¯éå®æ°ãªã³ã¯ãä»ããŠæž¡ãããŸããæåã®2ã€ã®ãã©ã¡ãŒã¿ãŒã«ã€ããŠã¯ãåŒã³åºãã®åã«å¿ èŠãªå€ã«ãã£ãŠå€æ°ãåæåããããšæ³å®ãããŠããŸãïŒã å€ãèŠçŽ ãæåã®äžéäœã«ãªã£ãŠããå Žåããªã¹ããå®æããåã«ç¢ºèªããããšãéèŠã§ãããã§ã«è¿°ã¹ãããã«ããããã®å Žåããã®ãªãã·ã§ã³ã¯å¯èœã§ãã 誰ãã建èšã®éå§åºæºãåžžã«ãã§ãã¯ããŠããããšã«æ°ä»ããã®ãããããŸããã
if (2*known_intermediate_list_size < intermediate_nodes_count) {
建èšããã§ã«é²è¡äžã®å Žåã¯ãåå¥ã«åŒ·èª¿ããã«ã äºå®ã¯ãèšèšããã»ã¹å
šäœãéããŠåºæºãçã®ãŸãŸã§ãããšããããšã§ããããã¯æããã«ãèšèšäžå€åŒïŒ2k + l = sãl> 0 => 2k <sããåŸãããŸãã
å¯èœãªæé©å
ãŸãã å³ã§ãããããã«ãæ§ç¯ãé²è¡äžã®åããã¥ãŒã§è€æ°ã®ç°ãªãæäœãå®è¡ããããšïŒå³ã§ã¯ãããã¯ãããïŒQïŒããã³ããã·ã¥ïŒQã11ïŒæäœã§ãïŒãããã€ãã®èŠçŽ ã¯ãŸã£ããåãã§ããªã¹ãïŒããªãŒã®åã次ã®èŠçŽ ãšåãé ç¹ãæããŸã-ãã€ã¢ã°ã©ã ã«ã¯ãçªå·7ã®2ã€ã®éãåããããŸãïŒã ããšãã°ãå®äºæã«ã誰ãããã§ã«èªåã®åã§ãããè¡ã£ãŠãããã©ããã瀺ãæ§é ã䜿çšããŠããã®ãããªèŠçŽ ã1ã€ã«ãæ¥çãããããšããããšãã§ããŸãïŒãã®ãããªæ§é ãšããŠãéåžžã®ãã¯ãã«ãŸãã¯äºéã«ãªã³ã¯ããããªã¹ãã䜿çšã§ããŸãïŒã æããã«ããã®ãããªæé©åã¯ãé«åºŠã«åå²ããããªãŒã®å Žåã«ããŸãæ©èœããŸãïŒå€ãã®ã¡ã¢ãªãç¯çŽããŸãïŒã
第äºã«ãå¥ã®éè€ããããŸãïŒå®æãããªã¹ããšäœæäžã®ãªã¹ãã®èŠçŽ ãããªãŒã®åãé ç¹ãæããŠããå ŽåïŒ ãã®åçã§ã¯ç·ãšãªã¬ã³ãžã®èŠçŽ ã«ã€ããŠè©±ããŠããïŒã ãã¡ããããã®ãããªéè€ãå®å šã«åé¿ããããšã¯ã§ããŸããããªã¹ãã®éå§ãšç¶ç¶ãåãã§ããå¯èœæ§ãé«ãããã§ãããäœæäžã®ãªã¹ãã®æåãå®æãããªã¹ãã®æåŸïŒãã¥ãŒã®å é ã§ã¯ãªãïŒã«éãããšãåã«ãæ¥çãããããšãã§ããŸãããã«ãããå°ãã®ã¡ã¢ãªãç¯çŽãããŸãã æ®ãã®ãªã¹ãïŒãã¡ããããã®æºåã«å¯Ÿå¿ïŒã«ã€ããŠã¯ãè€è£œããå¿ èŠããããŸãã ãããã£ãŠããã®æé©åã¯ãæšã«é·ãçŽç·ã®æãããããããå Žåã«æãããçŸããŸãã
æåŸã«ã第äžã«ãå®æ°ã§éãã§ã¿ãããšãã§ããŸãã ããšãã°ã1ã€ã®èŠçŽ ã§ã¯ãªã1ã€ã®ã¹ãããã§å®äºããããšãã§ããŸããã2ãã€ã§ã¯ãk <1/3ç§ã§æ§ç¯ãéå§ããã ãã§ååã§ãã ããããäœããã®åœ¢ã§åœ¹ç«ã€ã§ãããã
ãããèªãã ãã¹ãŠã®äººã«æè¬ããŸãã