Skip to content

Commit

Permalink
Add dup to BitArray
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre Medeiros committed Jun 25, 2020
1 parent 476486e commit d315847
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions spec/std/bit_array_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,13 @@ describe "BitArray" do
iter.next.should be_true
iter.next.should be_a(Iterator::Stop)
end

it "provides dup" do
a = BitArray.new(2)
b = a.dup

b[0] = true
a[0].should be_false
b[0].should be_true
end
end
7 changes: 7 additions & 0 deletions src/bit_array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,13 @@ struct BitArray
hasher
end

# Returns a new `BitArray` with all of the same elements.
def dup
bit_array = BitArray.new(@size)
@bits.copy_to(bit_array.@bits, malloc_size)
bit_array
end

private def bit_index_and_sub_index(index)
bit_index_and_sub_index(index) { raise IndexError.new }
end
Expand Down

0 comments on commit d315847

Please sign in to comment.