Skip to content

Conversation

t0hsumi
Copy link
Owner

@t0hsumi t0hsumi commented Jan 27, 2025

Comment on lines +30 to +31
self.dummy_head = ListNode()
self.dummy_tail = ListNode()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この2つ同じノードにして輪っかでもいいですね。

Comment on lines +91 to +95
self.access_history.append(key)
self.cache[key] = value
else:
self.access_history.append(key)
self.cache[key] = value
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここまとめられそうですね。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L105-L127辺りにも書いたんですが、こういうのって基本はまとめるもんなんでしょうか?
まとめてしまうと、視線が上下に飛ぶ感覚があるんですが、コードの読み方が悪いんでしょうか?

if Condition1:
    Statement1
elif Condition2:
    Statement2
Statement3

みたいな時って、自分は「if, elifだからStatement3が共通処理だな」「Statement1見た後にStatement3みよう」「その次にStatement2」みたいな感じなんですが、先にStatement3理解してから前処理見ようっていう感覚なんでしょうか?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

私の読み方は、

  1. 前処理として、すでに加えてあったらそれを消す
  2. 前処理として、溢れるようだったら一番古いのを消す
  3. 追加
    なので、あまり違和感はないです。

経験的に同じものを繰り返し書くと事故を起こしやすいので、そちらをより避けているかもしれません。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あと、「消す」メソッドがあってもいいかもしれません。
(データ構造を整合性を保って触る最小単位が何になっているかを考えると、そういう関数があってもいいでしょう。そういうものだけを使って動かしていると、何らかの意味で各関数が整合性を保って全体が動いていることが分かりやすくなります。)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。
すでに加えてある場合と、溢れる場合とで、似たようなことをしているのに書き方が異なることも、自分の中のわかりにくさの原因な気がしてきました。

node.value = value
elif len(self.key_to_node) == self.capacity:
oldest_node = self._popleft()
del self.key_to_node[oldest_node.key]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この行、_popleft にいれてもいいかもしれませんね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants