-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialize_to_bin.R
executable file
·44 lines (35 loc) · 1.06 KB
/
serialize_to_bin.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/RScript
saveMatrixList <- function(baseName, mtxList) {
idxName <- paste(baseName, ".idx", sep="")
idxCon <- file(idxName, 'wb')
on.exit(close(idxCon))
dataName <- paste(baseName, ".bin", sep="")
con <- file(dataName, 'wb')
on.exit(close(con))
writeBin(0L, idxCon)
for (m in mtxList) {
writeBin(dim(m), con)
writeBin(typeof(m), con)
writeBin(c(m), con)
flush(con)
offset <- as.integer(seek(con))
# cat('offset', offset)
writeBin(offset, idxCon)
}
flush(idxCon)
}
loadMatrix <- function(baseName = "data", index) {
idxName <- paste(baseName, ".idx", sep="")
idxCon <- file(idxName, 'rb')
on.exit(close(idxCon))
dataName <- paste(baseName, ".bin", sep="")
con <- file(dataName, 'rb')
on.exit(close(con))
seek(idxCon, (index-1)*4)
offset <- readBin(idxCon, 'integer')
seek(con, offset)
d <- readBin(con, 'integer', 2)
type <- readBin(con, 'character', 1)
close(idxCon)
return(structure(readBin(con, type, prod(d)), dim=d))
}