-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
root
committed
Aug 23, 2023
1 parent
4083948
commit d849fa9
Showing
4 changed files
with
359 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"bytes" | ||
) | ||
|
||
type IntSet struct { | ||
words []uint64 | ||
} | ||
|
||
// Has reports whether the set contains the non-negative value x. | ||
func (s *IntSet) Has(x int) bool { | ||
word, bit := x/64, uint(x%64) | ||
return word < len(s.words) && s.words[word]&(1<<bit) != 0 | ||
} | ||
|
||
// Add adds the non-negative value x to the set. | ||
func (s *IntSet) Add(x int) { | ||
word, bit := x/64, uint(x%64) | ||
for word >= len(s.words) { | ||
s.words = append(s.words, 0) | ||
} | ||
s.words[word] |= 1 << bit | ||
} | ||
|
||
// return the number of elements | ||
func (s *IntSet) Len() int { | ||
counter := 0 | ||
for _, word := range s.words { | ||
if word == 0 { | ||
continue | ||
} | ||
|
||
for j := 0; j < 64; j++ { | ||
if word & (1 << uint(j)) != 0 { | ||
counter++ | ||
} | ||
} | ||
} | ||
|
||
return counter | ||
} | ||
|
||
// remove x from the set | ||
func (s *IntSet) Remove(x int) { | ||
word, bit := x/64, uint(x%64) | ||
if word < len(s.words) { | ||
s.words[word] &= ^(1<<bit) | ||
} | ||
} | ||
|
||
// remove all elements from the set | ||
func (s *IntSet) Clear() { | ||
for i, _ := range s.words { | ||
s.words[i] = 0 | ||
} | ||
|
||
// for i := 0; i < len(s.words); i++ { | ||
// *(&s.words[i]) = 0 | ||
// } | ||
} | ||
|
||
// return a copy of the set | ||
func (s *IntSet) Copy() *IntSet { | ||
d := &IntSet{words: make([]uint64, len(s.words))} | ||
copy(d.words, s.words) | ||
return d | ||
} | ||
|
||
// String returns the set as a string of the form "{1 2 3}". | ||
func (s *IntSet) String() string { | ||
var buf bytes.Buffer | ||
buf.WriteByte('{') | ||
for i, word := range s.words { | ||
if word == 0 { | ||
continue | ||
} | ||
|
||
for j := 0; j < 64; j++ { | ||
if word&(1<<uint(j)) != 0 { | ||
if buf.Len() > len("{") { | ||
buf.WriteByte(' ') | ||
} | ||
fmt.Fprintf(&buf, "%d", 64*i+j) | ||
} | ||
} | ||
} | ||
|
||
buf.WriteByte('}') | ||
return buf.String() | ||
} | ||
|
||
func main() { | ||
var d uint8 = 2 | ||
fmt.Printf("%08b\n", d) // 00000010 | ||
fmt.Printf("%08b\n", ^d) // 11111101 | ||
|
||
var x, y IntSet | ||
x.Add(1) | ||
x.Add(144) | ||
x.Add(9) | ||
fmt.Println(x.Len()) | ||
fmt.Println(x.String()) | ||
|
||
y.Add(9) | ||
y.Add(42) | ||
fmt.Println(y.Len()) | ||
|
||
x.Remove(9) | ||
fmt.Println(x.String()) | ||
fmt.Println(x.Copy()) | ||
|
||
x.Clear() | ||
fmt.Println(x.String()) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
"math" | ||
) | ||
|
||
type Point struct { | ||
X, Y float64 | ||
} | ||
|
||
func (p Point) Distance(q Point) float64{ | ||
fmt.Println("value method: Distance") | ||
return math.Hypot(q.X-p.X, q.Y-p.Y) | ||
} | ||
|
||
func (p *Point) ScaleBy(factor float64) { | ||
fmt.Println("pointer method: ScaleBy") | ||
p.X *= factor | ||
p.Y *= factor | ||
} | ||
|
||
type Rocket struct { /* ... */ } | ||
func (r *Rocket) Launch() { | ||
fmt.Println("Launch") | ||
} | ||
|
||
|
||
|
||
func main () { | ||
p := Point{1, 2} | ||
q := Point{4, 6} | ||
|
||
distanceFromP := p.Distance | ||
fmt.Println(distanceFromP(q)) | ||
var origin Point // {0, 0} | ||
fmt.Println(distanceFromP(origin)) // "2.23606797749979", sqrt(5) | ||
|
||
scaleP := p.ScaleBy | ||
scaleP(2) | ||
scaleP(3) | ||
scaleP(10) | ||
fmt.Println(p) | ||
fmt.Println("******************************") | ||
|
||
r := new(Rocket) | ||
time.AfterFunc(2 * time.Second, func() {r.Launch() }) | ||
time.AfterFunc(2 * time.Second, r.Launch) | ||
// time.Sleep(4 * time.Second) | ||
fmt.Println("******************************") | ||
|
||
distance := Point.Distance | ||
fmt.Println(distance(p, q)) | ||
fmt.Printf("%T\n", distance) | ||
|
||
scale := (*Point).ScaleBy | ||
scale(&p, 2) | ||
fmt.Println(p) // "{2 4}" | ||
fmt.Printf("%T\n", scale) // "func(*Point, float64)" | ||
} | ||
|
||
func (p Point) Add(q Point) Point { return Point{p.X + q.X, p.Y + q.Y} } | ||
func (p Point) Sub(q Point) Point { return Point{p.X - q.X, p.Y - q.Y} } | ||
type Path []Point | ||
func (path Path) TranslateBy(offset Point, add bool) { | ||
var op func(p, q Point) Point | ||
if add { | ||
op = Point.Add | ||
} else { | ||
op = Point.Sub | ||
} | ||
|
||
for i := range path { | ||
// Call either path[i].Add(offset) or path[i].Sub(offset). | ||
path[i] = op(path[i], offset) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"bytes" | ||
) | ||
|
||
type IntSet struct { | ||
words []uint64 | ||
} | ||
|
||
// Has reports whether the set contains the non-negative value x. | ||
func (s *IntSet) Has(x int) bool { | ||
word, bit := x/64, uint(x%64) | ||
return word < len(s.words) && s.words[word]&(1<<bit) != 0 | ||
} | ||
|
||
// Add adds the non-negative value x to the set. | ||
func (s *IntSet) Add(x int) { | ||
word, bit := x/64, uint(x%64) | ||
for word >= len(s.words) { | ||
s.words = append(s.words, 0) | ||
} | ||
s.words[word] |= 1 << bit | ||
} | ||
|
||
// UnionWith sets s to the union of s and t. | ||
func (s *IntSet) UnionWith(t *IntSet) { | ||
for i, tword := range t.words { | ||
if i < len(s.words) { | ||
s.words[i] |= tword | ||
} else { | ||
s.words = append(s.words, tword) | ||
} | ||
} | ||
} | ||
|
||
func (s *IntSet) UnionWithMyEdition(t *IntSet) { | ||
var count int | ||
if len(s.words) < len(t.words) { | ||
count = len(s.words) | ||
} else { | ||
count = len(t.words) | ||
} | ||
|
||
for i := 0; i < count; i++ { | ||
s.words[i] |= t.words[i] | ||
} | ||
|
||
if len(s.words) < len(t.words) { | ||
s.words = append(s.words, t.words[count:]...) | ||
} | ||
} | ||
|
||
func (s *IntSet) StringMyEdition() string { | ||
var buf bytes.Buffer | ||
buf.WriteByte('{') | ||
for i, word := range s.words { | ||
if word == 0 { | ||
continue | ||
} | ||
|
||
if i < 1 { | ||
fmt.Fprintf(&buf, "%d", word) | ||
// fmt.Printf("%d", word) | ||
}else { | ||
fmt.Fprintf(&buf, " %d", word) | ||
// fmt.Printf(" %d", word) | ||
} | ||
} | ||
|
||
buf.WriteByte('}') | ||
return buf.String() | ||
} | ||
|
||
// String returns the set as a string of the form "{1 2 3}". | ||
func (s *IntSet) String() string { | ||
var buf bytes.Buffer | ||
buf.WriteByte('{') | ||
for i, word := range s.words { | ||
if word == 0 { | ||
continue | ||
} | ||
|
||
for j := 0; j < 64; j++ { | ||
if word&(1<<uint(j)) != 0 { | ||
if buf.Len() > len("{") { | ||
buf.WriteByte(' ') | ||
} | ||
fmt.Fprintf(&buf, "%d", 64*i+j) | ||
} | ||
} | ||
} | ||
|
||
buf.WriteByte('}') | ||
return buf.String() | ||
} | ||
|
||
type Student struct { | ||
name string | ||
Age int | ||
} | ||
|
||
func (s *Student) Print() { | ||
fmt.Println(s.name) | ||
fmt.Println(s.Age) | ||
} | ||
|
||
func main () { | ||
fmt.Println("IntSet") | ||
|
||
set := &IntSet{[]uint64{7, 4, 5}} | ||
// set.String() | ||
fmt.Println(set.StringMyEdition()) | ||
fmt.Println(set.String()) | ||
fmt.Println(^uint(0)) | ||
|
||
var x, y IntSet | ||
x.Add(1) | ||
x.Add(144) | ||
x.Add(9) | ||
fmt.Println(x.String()) | ||
|
||
y.Add(9) | ||
y.Add(42) | ||
fmt.Println(y.String()) | ||
|
||
// x.UnionWithMyEdition(&y) | ||
x.UnionWith(&y) | ||
fmt.Println(x.String()) // "{1 9 42 144}" | ||
fmt.Println(x.Has(9), x.Has(123)) // "true false" | ||
|
||
fmt.Println(&x) // "{1 9 42 144}" | ||
fmt.Println(x.String()) // "{1 9 42 144}" | ||
fmt.Println(x) //{[4398046511618 0 65536]} | ||
|
||
stu := Student{} | ||
stu.name = "peter" | ||
stu.Age = 18 | ||
fmt.Println(stu) | ||
stu.Print() | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type ByteCounter int | ||
|
||
func (c *ByteCounter) Write(p []byte) (n int, err error) { | ||
*c += ByteCounter(len(p)) // convert int to ByteCounter | ||
return len(p), nil | ||
} | ||
|
||
func main() { | ||
var c ByteCounter | ||
c.Write([]byte("hello")) | ||
fmt.Println(c) | ||
c = 0 | ||
|
||
var name = "Dolly" | ||
fmt.Fprintf(&c, "hello, %s", name) | ||
fmt.Println(c) | ||
} |