-
Notifications
You must be signed in to change notification settings - Fork 25
Vector module (#258) #306
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
base: master
Are you sure you want to change the base?
Vector module (#258) #306
Conversation
wojpok
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall this PR is a nice addition to standard library. Some cosmetic changes are required. As far as I am concerned the logic is correct, so there shouldn't be much work to do
test/stdlib/Vector.fram
Outdated
|
|
||
| let vec = makeVector {T=Int} 4 | ||
| let _ = | ||
| assert (vec.size == 4); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, use designated testing framework for testing. Use String tests as an example
lib/Vector.fram
Outdated
| parameter T | ||
|
|
||
| # Helper methods for vector implementation. | ||
| method getContentAt (Vector{ content } : Vector E T) (n : Int) = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting is off here. We usually write Vector {content} or Vector { content }. This extra space before opening bracket is somewhat mandatory. This applies to entire file
lib/Vector.fram
Outdated
| let n = n ||| (n>>64) in | ||
| n+1 | ||
|
|
||
| pub let makeVector {T} (size : Int) = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation is missing. Please, add docstrings to all public functions and methods
lib/Vector.fram
Outdated
| #} | ||
| pub method resize (self : Vector E T) (n : Int) = | ||
| self.setSize n; | ||
| let new_capacity = getSmallestUpperPow2 n in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use camelCase for variables
lib/Vector.fram
Outdated
|
|
||
| # Get the nth element of a vector. | ||
| pub method get (self : Vector E T) (n : Int) = | ||
| assert {msg="Vector is empty"} (self.empty.neg); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assert is excessive. Second one is sufficient on its own
lib/Vector.fram
Outdated
| self.setSize 0; | ||
| self.setCapacity 0; | ||
| self.setContent (self.mut.makeArray 0 (None : Option T)) | ||
|
No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This interface is pretty minimal. It would be nice to have al least iter and fold methods, as well as toList/fromList conversion functions.
|
This commit includes almost all the fixes you mentioned. At this point, I haven't implemented iter and fold methods yet. |
wojpok
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code is much cleaner now. Upon more careful study I have found more issues with the code
lib/Vector.fram
Outdated
| parameter E | ||
| parameter T | ||
|
|
||
| {## Helper methods for vector implementation ##} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is comment about internal implementation, it shouldn't be a part of a documentation. Single # would be enough. Also, please add ## ## Vector library at the top of the file. Title is missing
lib/Vector.fram
Outdated
| import open /Base/Bool | ||
| import open /List | ||
|
|
||
| data Vector E T = Vector of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make this type abstract. It is visible outside the module, so users should be able to use this type to annotate stuff
lib/Vector.fram
Outdated
| n+1 | ||
|
|
||
| {## Creates vector of given size (filled with Nones). ##} | ||
| pub let makeVector {T} (size : Int) = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have hardcoded ioMut handler, which disallows using vectors with other mutability handlers. Desired handler should be passed as an argument to this function.
lib/Vector.fram
Outdated
| pub method empty (Vector {size} : Vector E T) = size.get == 0 | ||
|
|
||
| {## | ||
| Resizes the vector to contain n elements. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Content of multiline docstrings should be indented with 2 spaces
test/stdlib/Vector.fram
Outdated
| import open /Vector | ||
| import open Testing | ||
|
|
||
| let vec = makeVector {T=Int} 4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests needs rework. All actions should be performed inside testCase body. Since IO is hardcoded into vector, it is impossible to fix right now. Once you enable other handlers, rework those tests. You will need to make a second wrapper, that will provide mut handler. Tests will look like this:
testCase "test" (fn _ =>
hMut (fn mut =>
# test body
))
lib/Vector.fram
Outdated
| size := n | ||
|
|
||
| {## Returns maximum size of vector. ##} | ||
| pub let maxVectorSize = maxArrayLength |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong. Vectors are always aligned to the nearest power of 2. Arrays can have any arbitrary size, so there might a case where maxVectorSize is not possible to allocate. For example if maxArrayLength is 17, then the vector of size 17 will require internal array of size 32, which is impossible to allocate. I suggest deleting this. I don't think there is any demand for contant such as this one
test/stdlib/Vector.fram
Outdated
| assertEqS cap 4); | ||
|
|
||
| testCase "checks max vector size constant" (fn _ => | ||
| assertEqS maxVectorSize maxArrayLength); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be tested at all
|
Tests are not working yet because the testing framework is still pending merge. |
Xaro8
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall looks fine to me. Agree that fold left/right may be useful, also maybe resize functionality should be tested (outside of push_back).
|
From now on the vector no longer stores Option T. |
|
I think, you were meant to use |
Vector module implementation with tests