Hi, I'm trying to teach myself programming. I already know Javascript and a little bit of Java. I got a Data Structures book and I decided to follow it and do the exercises with Nim. One of the first exercises is about implementing a dynamic vector, I know how to do it with Java but I couldn't find a way to do it with Nim. I know that already has an implementation called Seq, but I wanted to implement to learn. Hope you guys can help me.
Sorry for my english.
I got a Data Structures book
Which book do you have? Do you like it?
I know how to do it with Java
Can you show us your code? I would like to learn how to do it in Java.
Basically, what you need to build something like Nim's seq is allocating chunks of memory from the OS. You may inspect Nim's low level modules to find it out. But of course there exists much more interesting and useful data structures to build. Maybe a robin hood or hopscotch ordered hash table (I was playing recently with that) or some of the structures boost lib has?
The book is Algorithms from Sedgewick.
In Java I just create an array with a certain size and when it's full I create another one with double the size and copy the elements to it. I'm doing it wrong? I'm just following the book.
In Nim I couldn't do it. I looked at the source code on github and all I found was:
seq*{.magic: "Seq".}[T]
newSeq also has a signature like that and no implementation
proc newSeq*[T](s: var seq[T], len: Natural) {.magic: "NewSeq", noSideEffect.}
In Java I just create an array with a certain size and when it's full I create another one with double the size and copy the elements to it.
Of course you can do exactly the same with Nim: You can create a seq of some initial size, fill in elements, create a larger one and copy elements over. So you learn creating seqs and copying elements. Not very interesting and useful. Have you read tutorial part 1 and 2? Then you already know much of Nim, and you can decide yourself which exercises are really interesting. (For Nim's seq implementation -- I assume that one is very low level, optimized for speed and interacting with the OS for allocating memory, so may be not easy understandable for beginners. And it may be hidden in the compiler code, because seq's are internal data structures.)
And for the Sedgewick book, I think chapter 2 about sorting is more interesting. Comparing performance of quicksort vs. mergesort is some fun, and comparing recursive vs. non recursive code.
I'm still on chapter 1. It's pretty big. I was trying to write all examples he gave and in his implementation of Stack he does that. So I'll follow your advice and just use seq.
Thanks all of you!
You can define the object to represent the continuous block of memory using alloc0 and the size field to indicate its size.
To extract the value at given offset, you can use pointer arithmetic in this post
To resize vector, you can use resize.
To free the memory you can use free or resize the block to 0.
All proc above are not shared version, so make sure the instance not out of scope when using it.