Sorting an Array

Now that we know how to structure our data, we can revisit the signature of qsort:

 def qsort(data:Ptr[Byte],
  comparator:CFuncPtr2[Ptr[Byte], Ptr[Byte], Int])

We’ll pass in an array of NGramData cast to Ptr[Byte], we can get the value of num from the used field of the WrappedArray, and we can get the size of each item just by sizeof[NGgramData]. But for qsort to work, we have to supply the comparator, too; and comparator has a type we haven’t seen before, a CFuncPtr, or function pointer.

Implementing a Comparator

In a typical systems programming course, abstracting from functions to function pointers is one of the hardest conceptual leaps, and C’s syntax certainly doesn’t make it any easier. In Scala ...

Get Modern Systems Programming with Scala Native now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.