Skip to content

add vector slice#1

Open
elcritch wants to merge 9 commits into
paranim:masterfrom
elcritch:add-vec-slice
Open

add vector slice#1
elcritch wants to merge 9 commits into
paranim:masterfrom
elcritch:add-vec-slice

Conversation

@elcritch
Copy link
Copy Markdown

No description provided.

@oakes
Copy link
Copy Markdown
Member

oakes commented Dec 25, 2022

This is really great. But you don't need to define in -- it already works, because nim defines it in terms of contains.

@elcritch
Copy link
Copy Markdown
Author

elcritch commented Dec 25, 2022

Oh nice! I can never remember that ‘in’ is already defined. Ok removed the in.

@oakes
Copy link
Copy Markdown
Member

oakes commented Dec 25, 2022

Currently the behavior of add on slices is buggy because it still thinks it's the original vector:

var s2 = v1[1..1] # ["two"]
s2 = s2.add(0, "yo") # expected ["yo"] but I get ["two"]
s2 = s2.add("yo") # expected ["two", "yo"] but I get ["yo"]

@elcritch
Copy link
Copy Markdown
Author

Currently the behavior of add on slices is buggy because it still thinks it's the original vector:

Good catch! Ok, I pushed fixes for add on a slice. I also found a bug in the regular vector add too where the Leaf branch didn't increase the vec size. There should probably be large vec test for these too.

Merry Christmas and thanks for a fun x-mas day puzzle! ;)

Comment thread src/parazoa.nim
of Leaf:
newChild.value = value
node.nodes[index] = newChild
res.size.inc()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think this is correct. At this point we are reaching a leaf that already exists and updating its value, so the size should not change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

However, you inspired me to look into the size stuff and I realized I am actually incrementing too often. Specifically, if you use setLen to increase the size of a vec, and then set a value in one of the new slots, it is incrementing the size when it should not:

7fc8d3a

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

setLen isn't used when adding a new item, at least for Vec[T]. In my test case when you added an item to the seq, the length wasn't changing like it should.

Specifically, if you use setLen to increase the size of a vec, and then set a value in one of the new slots, it is incrementing the size when it should not:

To handle both a setLen and adding new items, you'll might want a capacity and a length. That's how seq works I think.

Though, perhaps you could update add to only increment if index == size? Then I think my change would be correct for cases when you're adding at the end, and fix the issue you pointed out.

Or you could re-work add to always use setLen when index >= size.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

setLen isn't used when adding a new item

I know; I didn't mean to imply that it was. It was a separate bug that I happened to notice yesterday.

Though, perhaps you could update add to only increment if index == size? Then I think my change would be correct for cases when you're adding at the end, and fix the issue you pointed out.

That is exactly what I did. I linked to the commit in my last comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants