Skip to content

Avoid O-notation for String operations #46650

Closed
@jonhoo

Description

@jonhoo

A couple of places in the String docs states that an operation is O(n), usually when discussing a method that needs to shift elements over. However, this is not helpful to users who do not know O-notation, and n is generally either misleading or undefined.

String::insert_str: The operation is really O(n-idx+k), where n is the length of self, and k is the length of string. In fact, that's not even true if the String needs to be resized. If we wanted to cover that, we'd need to include the "amortized" qualifier in there. O(n) is insufficient in part because if k is large and n is small, k is much more important. We probably want to ditch the O-notation here entirely, and just explain that it needs to shift everything after idx over, and copy in the characters from string, and that this operation may be slow because of it.

String::insert and String::remove: These are less egregious since O(n) is correct. However, the smaller bound of O(n-idx) can be given, and the difference might be significant. Here too, it'd be better to just get rid of the O-notation altogether and explain what happens instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions