Created
April 8, 2017 00:36
-
-
Save scottcagno/def7bab4c7e166b648c02742a871fa7a to your computer and use it in GitHub Desktop.
Docs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "strings" | |
| ) | |
| type Doc struct { | |
| Off int | |
| Len int | |
| Nil bool | |
| } | |
| func (doc *Doc) String() string { | |
| isnil := "F" | |
| if doc.Nil { | |
| isnil = "T" | |
| } | |
| return fmt.Sprintf("Doc{%d, %d, %s}", doc.Off, doc.Len, isnil) | |
| } | |
| type Docs []*Doc | |
| func NewDocs(n int) Docs { | |
| docs := make(Docs, n) | |
| for i := 0; i < n; i++ { | |
| docs[i] = new(Doc) | |
| } | |
| return docs | |
| } | |
| func (docs *Docs) String() string { | |
| if len(*docs) == 0 { | |
| return "Docs [ <nil> ]\n" | |
| } | |
| if len(*docs) == 1 { | |
| return "Docs [ " + (*docs)[0].String() + " ]\n" | |
| } | |
| var ss []string | |
| for i := 0; i < len(*docs); i++ { | |
| s := "%s" | |
| switch i { | |
| case 0: | |
| s = "Docs [ %s" | |
| case len(*docs) - 1: | |
| s = "%s ]\n" | |
| } | |
| ss = append(ss, fmt.Sprintf(s, (*docs)[i])) | |
| } | |
| return strings.Join(ss, ", ") | |
| } | |
| func (docs *Docs) InsertAt(doc *Doc, n int) { | |
| if n > len(*docs) || n < 0 { | |
| fmt.Printf("err: InsertAt(%d): %d is out of bounds!\n", n, n) | |
| return // out of bounds | |
| } | |
| if n <= len(*docs)-1 && (*docs)[n].Nil { | |
| fmt.Printf("Overwrote *Doc at index: %d\n", n) | |
| (*docs)[n] = doc | |
| return // overwrite if nil | |
| } | |
| *docs = append(*docs, new(Doc)) | |
| copy((*docs)[n+1:], (*docs)[n:]) | |
| (*docs)[n] = doc | |
| fmt.Printf("Inserted *Doc at index: %d\n", n) | |
| } | |
| func (docs *Docs) DeleteAt(n int) { | |
| if n > len(*docs)-1 || n < 0 { | |
| fmt.Printf("err: DeleteAt(%d): %d is out of bounds!\n", n, n) | |
| return // out of bounds | |
| } | |
| copy((*docs)[n:], (*docs)[n+1:]) | |
| (*docs)[len(*docs)-1] = new(Doc) | |
| *docs = (*docs)[:len(*docs)-1] | |
| fmt.Printf("Deleted *Doc at index: %d\n", n) | |
| } | |
| func (docs *Docs) ClearAt(n int) { | |
| if n > len(*docs)-1 || n < 0 { | |
| fmt.Printf("err: ClearAt(%d): %d is out of bounds!\n", n, n) | |
| return // out of bounds | |
| } | |
| if (*docs)[n].Nil { | |
| fmt.Printf("err: ClearAt(%d): *Doc is already nil!\n", n) | |
| return // doc is already nil | |
| } | |
| (*docs)[n] = &Doc{Nil: true} | |
| fmt.Printf("Cleared *Doc at index: %d\n", n) | |
| } | |
| func (docs *Docs) Println() { | |
| fmt.Printf("len(docs) = %d\n%s\n", len(*docs), docs.String()) | |
| } | |
| const COUNT = 8 | |
| func main() { | |
| docs := NewDocs(0) | |
| docs.Println() | |
| docs.InsertAt(&Doc{-1, -1, true}, 0) | |
| docs.Println() | |
| for i := 0; i < COUNT; i++ { | |
| docs.InsertAt(&Doc{i, i * 10, false}, i) | |
| docs.Println() | |
| } | |
| for i := COUNT - 1; i > 2; i-- { | |
| docs.DeleteAt(i) | |
| docs.Println() | |
| } | |
| docs.ClearAt(1) | |
| docs.Println() | |
| docs.ClearAt(2) | |
| docs.Println() | |
| docs.ClearAt(0) | |
| docs.Println() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment