aboutsummaryrefslogtreecommitdiff
path: root/sd.go
diff options
context:
space:
mode:
Diffstat (limited to 'sd.go')
-rw-r--r--sd.go28
1 files changed, 24 insertions, 4 deletions
diff --git a/sd.go b/sd.go
index 743aaa6..0bf621b 100644
--- a/sd.go
+++ b/sd.go
@@ -1,19 +1,22 @@
package main
import (
+ "database/sql"
"flag"
"fmt"
"os"
"os/exec"
+
+ _ "modernc.org/sqlite"
)
var file = flag.String("f", "cards", "cards file")
var seg = []int{}
-func build(a *[]int, l int, r int, n int) {
+func build(a *sql.Rows, l int, r int, n int) {
if l == r {
- seg[n] = (*a)[l]
+ a.Scan(&seg[n])
return
}
m := (l + r) >> 1
@@ -51,6 +54,23 @@ func query(v int, l int, r int, n int) (int, int) {
func main() {
flag.Parse()
+ db, err := sql.Open("sqlite", *file)
+ if err != nil {
+ panic(err)
+ }
+
+ // Get number of cards
+ var N int
+ _ = db.QueryRow("SELECT COUNT(*) FROM cards").Scan(&N)
+ seg = make([]int, 4*N)
+
+ // Build segment tree
+ rows, err := db.Query("SELECT * FROM cards")
+ if err != nil {
+ panic(err)
+ }
+ build(rows, 0, N-1, 1)
+
// https://stackoverflow.com/questions/14094190/function-similar-to-getchar
// disable input buffering
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
@@ -64,9 +84,9 @@ func main() {
var b []byte = make([]byte, 1)
os.Stdin.Read(b)
- if string(b) == 'y' {
+ if b[0] == byte('y') {
- } else if string(b) == 'n' {
+ } else if b[0] == byte('n') {
} else {