Skip to content

Commit

Permalink
ch02 reorganize packages
Browse files Browse the repository at this point in the history
  • Loading branch information
spamegg1 committed Apr 7, 2024
1 parent 0af268a commit 0a2b900
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
9 changes: 4 additions & 5 deletions src/main/scala/ch02/aggregateAndCount.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package ch02.aggregateAndCount
package ch02
package agg

import scalanative.unsigned.UnsignedRichInt // .toUSize
import scalanative.unsafe.{Ptr, CSize, CQuote, CString, sizeof, stackalloc}
import scalanative.libc.{stdio, stdlib, string}, stdio.FILE
import ch02.common
import common.{qsort, NGramData, byCount, WrappedArray}

val lineBuffer = stdlib.malloc(1024) // 0.5
val tempWordBuffer = stdlib.malloc(1024)
val blockSize = 1048576 // ~ 1MB - too big? (same)

@main
def aggregateAndCount(args: String*): Unit =
val array = common.makeWrappedArray(blockSize)
val array = makeWrappedArray(blockSize)
val readStart = System.currentTimeMillis()
val linesRead = readAllLines(stdio.stdin, array) // NEW, different
val readElapsed = System.currentTimeMillis() - readStart
Expand Down Expand Up @@ -42,7 +41,7 @@ def readAllLines(fd: Ptr[FILE], array: WrappedArray[NGramData]): Long =
var linesRead = 0L

while stdio.fgets(lineBuffer, 1024, fd) != null do
if array.used >= array.capacity - 1 then common.growWrappedArray(array, blockSize)
if array.used >= array.capacity - 1 then growWrappedArray(array, blockSize)
parseAndCompare(lineBuffer, array) // this is the main difference!
linesRead += 1

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/ch02/common.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch02.common
package ch02

import scalanative.unsigned.UnsignedRichInt // .toUSize
import scalanative.unsafe.{Ptr, sizeof, CString, CStruct4, CFuncPtr2, CQuote, extern}
Expand Down
9 changes: 4 additions & 5 deletions src/main/scala/ch02/sortByCount.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package ch02.sortByCount
package ch02
package sort

import scalanative.unsigned.UnsignedRichInt // .toUSize
import scalanative.unsafe.{Ptr, CQuote, CSize, sizeof}
import scalanative.libc.{stdio, stdlib, string}
import ch02.common
import common.{qsort, NGramData, byCount}

// this temporary space is used to read the string in each line of the file.
val tempWord: Ptr[Byte] = stdlib.malloc(1024) // 0.5
Expand Down Expand Up @@ -62,12 +61,12 @@ def parseLine(lineBuffer: Ptr[Byte], data: Ptr[NGramData]): Unit =
def sortByCount(args: String*): Unit =
val blockSize = 1048576 // 2^20 NGramData items = 2^20 * 20 bytes = 20 MB
val lineBuffer = stdlib.malloc(1024) // 0.5
var array = common.makeWrappedArray(blockSize)
var array = makeWrappedArray(blockSize)
val readStart = System.currentTimeMillis()

// pipe file into stdin, then read line by line (fgets respects newlines)
while stdio.fgets(lineBuffer, 1024, stdio.stdin) != null do // reads <= 1024 - 1 chars
if array.used >= array.capacity then common.growWrappedArray(array, blockSize)
if array.used >= array.capacity then growWrappedArray(array, blockSize)
parseLine(lineBuffer, array.data + array.used) // parse CURRENT line
array.used += 1

Expand Down

0 comments on commit 0a2b900

Please sign in to comment.