Skip to content

feat: add DoubleBuffer implementation and initial tests #5885

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

Closed
wants to merge 1 commit into from

Conversation

abda-gaye
Copy link

  • I have read and followed the guidelines outlined in CONTRIBUTING.md.
    Yes, I have read and followed the guidelines provided in the CONTRIBUTING.md file.

  • This pull request contains only my own work; it has not been plagiarized.
    Yes, this pull request contains only my original work and has not been plagiarized.

  • All filenames follow the PascalCase convention.
    Yes, all filenames follow the PascalCase convention.

  • All function and variable names adhere to Java naming conventions.
    Yes, all function and variable names adhere to Java naming conventions.

  • [] All new algorithms include a URL in their comments linking to a Wikipedia page or a similar source for reference.

  • All code is formatted using clang-format -i --style=file /datastructures/buffers/DoubleBufferTest.java

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 89.65517% with 3 lines in your changes missing coverage. Please review.

Project coverage is 66.83%. Comparing base (33dee07) to head (b445e84).

Files with missing lines Patch % Lines
...lgorithms/datastructures/buffers/DoubleBuffer.java 89.65% 0 Missing and 3 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #5885      +/-   ##
============================================
+ Coverage     66.81%   66.83%   +0.02%     
- Complexity     4501     4515      +14     
============================================
  Files           611      612       +1     
  Lines         16958    16987      +29     
  Branches       3275     3281       +6     
============================================
+ Hits          11330    11354      +24     
- Misses         5181     5182       +1     
- Partials        447      451       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

*
* @param <Item> The type of elements stored in the double buffer.
*/
public class DoubleBuffer<Item> {
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about using T instead of Item. Because this is common practice


if (isPrimaryActive.get()) {
if (primaryIndex.get() < capacity) {
primaryBuffer[primaryIndex.getAndIncrement()] = item;
Copy link
Contributor

Choose a reason for hiding this comment

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

As you want to make this code thread-safe, will be better to use ReentrantLock for this.

private final ReentrantLock lock = new ReentrantLock();

public void switchBuffer() {
    lock.lock();
    try {
        isPrimaryActive.set(!isPrimaryActive.get());
    } finally {
        lock.unlock();
    }
}

public boolean put(Item item) {
    if (item == null) {
        throw new IllegalArgumentException("Null items are not allowed");
    }
    lock.lock();
    try {
        if (isPrimaryActive.get()) {
            if (primaryIndex.get() < capacity) {
                primaryBuffer[primaryIndex.getAndIncrement()] = item;
                return true;
            }
        } else {
            if (secondaryIndex.get() < capacity) {
                secondaryBuffer[secondaryIndex.getAndIncrement()] = item;
                return true;
            }
        }
        return false; // Buffer is full
    } finally {
        lock.unlock();
    }
}

public Item get() {
    lock.lock();
    try {
        if (isPrimaryActive.get()) {
            if (primaryIndex.get() == 0) {
                return null;
            }
            return primaryBuffer[primaryIndex.decrementAndGet()];
        } else {
            if (secondaryIndex.get() == 0) {
                return null;
            }
            return secondaryBuffer[secondaryIndex.decrementAndGet()];
        }
    } finally {
        lock.unlock();
    }
}

Copy link

github-actions bot commented Dec 5, 2024

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution!

@github-actions github-actions bot added the stale label Dec 5, 2024
Copy link

Please reopen this pull request once you have made the required changes. If you need help, feel free to ask in our Discord server or ping one of the maintainers here. Thank you for your contribution!

@github-actions github-actions bot closed this Dec 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants