Skip to content
Merged
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
29 changes: 22 additions & 7 deletions src/deque.cr
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class Deque(T)
return unshift(value) if index == 0
return push(value) if index == @size

increase_capacity if @size >= @capacity
resize_if_cant_insert
rindex = @start + index
rindex -= @capacity if rindex >= @capacity

Expand Down Expand Up @@ -474,7 +474,7 @@ class Deque(T)
# a.push 3 # => Deque{1, 2, 3}
# ```
def push(value : T)
increase_capacity if @size >= @capacity
resize_if_cant_insert
index = @start + @size
index -= @capacity if index >= @capacity
@buffer[index] = value
Expand Down Expand Up @@ -556,7 +556,7 @@ class Deque(T)
# a.unshift 0 # => Deque{0, 1, 2}
# ```
def unshift(value : T) : self
increase_capacity if @size >= @capacity
resize_if_cant_insert
@start -= 1
@start += @capacity if @start < 0
@buffer[@start] = value
Expand All @@ -581,15 +581,30 @@ class Deque(T)
end
end

private def increase_capacity
private INITIAL_CAPACITY = 4

# behaves like `calculate_new_capacity(@capacity + 1)`
private def calculate_new_capacity
return INITIAL_CAPACITY if @capacity == 0

@capacity * 2
end

# behaves like `resize_if_cant_insert(1)`
private def resize_if_cant_insert
if @size >= @capacity
resize_to_capacity(calculate_new_capacity)
end
end

private def resize_to_capacity(capacity)
old_capacity, @capacity = @capacity, capacity

unless @buffer
@capacity = 4
@buffer = Pointer(T).malloc(@capacity)
return
end

old_capacity = @capacity
@capacity *= 2
@buffer = @buffer.realloc(@capacity)

finish = @start + @size
Expand Down