Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/main/java/algorithms/sprint2/Deque.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
public class Deque {

// -------------------- RING BUFFER DEQUE --------------------
private static final int MAX_CAPACITY = 100_000;

static final class RingDeque {
private final int[] a;
private final int cap;
Expand All @@ -63,8 +65,8 @@
private int size = 0;

RingDeque(int cap) {
this.cap = cap;
this.a = new int[cap];
this.cap = safeCapacity(cap);
this.a = new int[this.cap];
}

private int next(int i) {
Expand Down Expand Up @@ -110,6 +112,13 @@
}
}

private static int safeCapacity(int cap) {

Check warning on line 115 in src/main/java/algorithms/sprint2/Deque.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move this method into "RingDeque".

See more on https://sonarcloud.io/project/issues?id=krotname_JavaAlgorithmsShowcase&issues=AZ-4pNzUqFO0EDsIEPT1&open=AZ-4pNzUqFO0EDsIEPT1&pullRequest=87
if (cap < 0) {
return 0;
}
return Math.min(cap, MAX_CAPACITY);
Comment thread
krotname marked this conversation as resolved.
}

private static void process(FastIn in, FastOut out) throws Exception {
int n = in.nextInt();
int m = in.nextInt();
Expand Down Expand Up @@ -233,6 +242,27 @@
)
);

// Некорректная емкость из ввода не должна приводить к аварийному завершению
assertEq(
"error\nerror\n",
solveIO(
"2\n" +
"-1\n" +
"push_back 1\n" +
"pop_front\n"

Check warning on line 252 in src/main/java/algorithms/sprint2/Deque.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this String concatenation with Text block.

See more on https://sonarcloud.io/project/issues?id=krotname_JavaAlgorithmsShowcase&issues=AZ-4pNzUqFO0EDsIEPTz&open=AZ-4pNzUqFO0EDsIEPTz&pullRequest=87
)
);

// Слишком большая емкость ограничивается безопасным максимумом до выделения массива
assertEq(
"error\n",
solveIO(
"1\n" +
"1000000000\n" +
"pop_front\n"

Check warning on line 262 in src/main/java/algorithms/sprint2/Deque.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this String concatenation with Text block.

See more on https://sonarcloud.io/project/issues?id=krotname_JavaAlgorithmsShowcase&issues=AZ-4pNzUqFO0EDsIEPT0&open=AZ-4pNzUqFO0EDsIEPT0&pullRequest=87
)
);

// Wrap-around: head/tail должны корректно "перепрыгивать" границу массива
assertEq(
"1\n4\n2\n3\n",
Expand Down