Golang sparse array of data structures

grasp knowledge:

  • Initialization and array-valued
  • Initialization and assignment structure
  • Converting between strings and integer operations, and some other
  • Type assertion
  • Read the file
  • Write to file
  • To compress sparse array
package main

import (
    "bufio"
    "fmt"
    "io"
    "os"
    "strconv"
    "strings"
    // "strconv"
)

func originArr() [11][11]int {
    //创建原始数组
    var chessMap [11][11]int

    chessMap[1][2] = 1
    chessMap[2][3] = 2
    return chessMap 
} 

FUNC printArr (chessMap [ . 11 ] [ . 11 ] int ) {
     // print array 
    for _, V1: = Range chessMap {
         for _, V2: = Range {V1 
            fmt.Printf ( " % D \ T " , V2 ) 
        } 
        fmt.Println () 
    } 
} 

// memory rows, columns, the value of 
type valNode struct { 
    row    int 
    COL    int 
    // this is the interface type, the value may be any type, and includes not only character integer 
    value interface { }
}

var sparseArr [] valNode 

FUNC doParseArr (chessMap [ . 11 ] [ . 11 ] int ) [] valNode {
     // sparse array
     // iterate, if a value is not zero, it is placed in the corresponding structure 
    val: = valNode {
         // original array and the row and column values 
        row:    . 11 , 
        COL:    . 11 , 
        value: 0 , 
    } 
    // initializes the memory sparse array 
    sparseArr = the append (sparseArr, Val) 

    for I, V1: = Range chessMap {
         for J, V2: = Range V1 {
             IF v2 != 0 {
                var val valNode
                val.row = i
                val.col = j
                val.value = v2
                sparseArr = append(sparseArr, val)
            }
        }
    }

    for _, j := range sparseArr {
        fmt.Printf("第%d行,第%d列的值是%d\n", j.row, j.col, j.value.(int))

    }
    return sparseArr
}

func writeParseArr(sparseArr []valNode, filepath string) {
     // sparse array storage 
    File, ERR: = os.OpenFile (filepath, os.O_CREATE | os.O_APPEND | os.O_WRONLY, 0666 )
     IF ERR =! Nil {
         return 
    } 
    the defer File.close () 
    for _, J: = Range sparseArr {
         // because the read integer into a string and then needs to be written
         // interface needs to be assigned to a variable type assertion 
        STR: = strconv.Itoa (j.row) + "  " + strconv.Itoa (j.col) + "  " + strconv.Itoa (. (j.value ( int ))) + " \ n- " 
        wriiter: = bufio.NewWriter (File)
        wriiter.WriteString (STR) 
        wriiter.Flush () 
        // fmt.Printf ( "% d row, the value of the first column is the% d% d \ n", j.row, j.col, j.value. (int )) 

    } 
} 

FUNC readParseArr (filepath String ) (newChessMap [ . 11 ] [ . 11 ] int ) {
     // initialize an array
     // reading the stored files, and each row is converted into 
    file, err: = os.OpenFile (filepath , os.O_RDONLY, 0666 )
     IF ERR =! nil {
         return 
    } 
    the defer File.close () 
    Reader: = bufio.NewReader (File)
     for { 
        STR, ERR: = reader.ReadString ( '\n')
        if err != nil {
            return
        }
        tmp := strings.Split(strings.TrimRight(str, "\n"), " ")
        // fmt.Println(strings.Split(strings.TrimRight(str, "\n"), " "))
        r, _ := strconv.Atoi(tmp[0])
        c, _ := strconv.Atoi(tmp[1])
        v, _ := strconv.Atoi(tmp[2])
        if r == 11 {
            continue
        }
        newChessMap[r][c] = v
        if err == io.EOF {
            break
        }
    }
    return newChessMap
}

func main() {
    chessMap := originArr()
    printArr(chessMap)
    sparseArr := doParseArr(chessMap)
    filepath := "data.txt"
    writeParseArr(sparseArr, filepath)
    newChessMap := readParseArr(filepath)
    printArr(newChessMap)
}

 

 data.txt

 

 

Guess you like

Origin www.cnblogs.com/xiximayou/p/12002686.html