Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update array_deque.cpp函数index() #1368

Closed
wants to merge 1 commit into from
Closed

Conversation

KLYkl
Copy link

@KLYkl KLYkl commented May 17, 2024

将43行的return (i + capacity()) % capacity();改为return (i % capacity() + capacity()) % capacity(); 原因:原来的表达式“(i + capacity()) % capacity();”如果i是负数的话,需要保证capacity() >= -i。而更改后的表达式“ (i % capacity() + capacity()) % capacity();” 中的先对i取模相加,最后再取模操作会确保结果是一个非负整数,并且不会超过 capacity() 的值。这是因为取模操作会返回一个在 0 到 capacity() - 1 范围内的数,即使原始的和可能是负数。保证函数index()的传入形参i<-capacity()不会崩溃,且能正确返回 0 到 capacity() - 1 范围内的的索引值。

If this pull request (PR) pertains to Chinese-to-English translation, please confirm that you have read the contribution guidelines and complete the checklist below:

  • This PR represents the translation of a single, complete document, or contains only bug fixes.
  • The translation accurately conveys the original meaning and intent of the Chinese version. If deviations exist, I have provided explanatory comments to clarify the reasons.

If this pull request (PR) is associated with coding or code transpilation, please attach the relevant console outputs to the PR and complete the following checklist:

  • I have thoroughly reviewed the code, focusing on its formatting, comments, indentation, and file headers.
  • I have confirmed that the code execution outputs are consistent with those produced by the reference code (Python or Java).
  • The code is designed to be compatible on standard operating systems, including Windows, macOS, and Ubuntu.

将43行的return (i + capacity()) % capacity();改为return (i % capacity() + capacity()) % capacity();
原因:原来的表达式“(i + capacity()) % capacity();”如果i是负数的话,需要保证capacity() >= -i。而更改后的表达式“ (i % capacity() + capacity()) % capacity();” 中的先对i取模相加,最后再取模操作会确保结果是一个非负整数,并且不会超过 capacity() 的值。这是因为取模操作会返回一个在 0 到 capacity() - 1 范围内的数,即使原始的和可能是负数。保证函数index()的传入形参i<-capacity()不会崩溃,且能正确返回 0 到 capacity() - 1 范围内的的索引值。
@krahets
Copy link
Owner

krahets commented May 18, 2024

Hi,i 定义为索引,因此输入应该在区间 [0, capacity()) 内,不应出现负数的情况。

严谨的做法是在 index() 方法中添加安全判断,保证输入参数 i 满足索引范围。但为了简洁性,书中代码会省略部分安全判断等代码。

@krahets krahets closed this May 18, 2024
@KLYkl
Copy link
Author

KLYkl commented May 18, 2024

Hi, 定义为索引,因此输入应该在区间 内,不应出现负数的情况。i``[0, capacity())

严谨的做法是在 方法中添加安全判断,保证输入参数 满足索引范围。但为了简洁性,书中代码会省略部分安全判断等代码。index()``i

这么说那i%capacity()就可以了,不必写(i + capacity()) % capacity()了吧?

@KLYkl KLYkl deleted the patch-1 branch May 18, 2024 10:54
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.

None yet

2 participants