Skip to content

gofmt #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/basic/cgo/lib/print.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package lib
/*
#include <stdio.h>
#include <stdlib.h>
void myprint(char* s) {
printf("%s", s);
}
*/
import "C"
import "unsafe"
func Print(s string) {
cs := C.CString(s)
defer C.free(unsafe.Pointer(cs))
C.myprint(cs)
}
package lib

/*
#include <stdio.h>
#include <stdlib.h>

void myprint(char* s) {
printf("%s", s);
}
*/
import "C"
import "unsafe"

func Print(s string) {
cs := C.CString(s)
defer C.free(unsafe.Pointer(cs))
C.myprint(cs)
}
28 changes: 14 additions & 14 deletions src/basic/cgo/lib/rand.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package lib
/*
#include <stdlib.h>
*/
import "C"
func Random() int {
return int(C.rand())
}
func Seed(i int) {
C.srand(C.uint(i))
}
package lib

/*
#include <stdlib.h>
*/
import "C"

func Random() int {
return int(C.rand())
}

func Seed(i int) {
C.srand(C.uint(i))
}
292 changes: 146 additions & 146 deletions src/basic/map1/cmap.go
Original file line number Diff line number Diff line change
@@ -1,146 +1,146 @@
package map1
import (
"bytes"
"fmt"
"reflect"
"sync"
)
type ConcurrentMap interface {
GenericMap
}
type myConcurrentMap struct {
m map[interface{}]interface{}
keyType reflect.Type
elemType reflect.Type
rwmutex sync.RWMutex
}
func (cmap *myConcurrentMap) Get(key interface{}) interface{} {
cmap.rwmutex.RLock()
defer cmap.rwmutex.RUnlock()
return cmap.m[key]
}
func (cmap *myConcurrentMap) isAcceptablePair(k, e interface{}) bool {
if k == nil || reflect.TypeOf(k) != cmap.keyType {
return false
}
if e == nil || reflect.TypeOf(e) != cmap.elemType {
return false
}
return true
}
func (cmap *myConcurrentMap) Put(key interface{}, elem interface{}) (interface{}, bool) {
if !cmap.isAcceptablePair(key, elem) {
return nil, false
}
cmap.rwmutex.Lock()
defer cmap.rwmutex.Unlock()
oldElem := cmap.m[key]
cmap.m[key] = elem
return oldElem, true
}
func (cmap *myConcurrentMap) Remove(key interface{}) interface{} {
cmap.rwmutex.Lock()
defer cmap.rwmutex.Unlock()
oldElem := cmap.m[key]
delete(cmap.m, key)
return oldElem
}
func (cmap *myConcurrentMap) Clear() {
cmap.rwmutex.Lock()
defer cmap.rwmutex.Unlock()
cmap.m = make(map[interface{}]interface{})
}
func (cmap *myConcurrentMap) Len() int {
cmap.rwmutex.RLock()
defer cmap.rwmutex.RUnlock()
return len(cmap.m)
}
func (cmap *myConcurrentMap) Contains(key interface{}) bool {
cmap.rwmutex.RLock()
defer cmap.rwmutex.RUnlock()
_, ok := cmap.m[key]
return ok
}
func (cmap *myConcurrentMap) Keys() []interface{} {
cmap.rwmutex.RLock()
defer cmap.rwmutex.RUnlock()
initialLen := len(cmap.m)
keys := make([]interface{}, initialLen)
index := 0
for k, _ := range cmap.m {
keys[index] = k
index++
}
return keys
}
func (cmap *myConcurrentMap) Elems() []interface{} {
cmap.rwmutex.RLock()
defer cmap.rwmutex.RUnlock()
initialLen := len(cmap.m)
elems := make([]interface{}, initialLen)
index := 0
for _, v := range cmap.m {
elems[index] = v
index++
}
return elems
}
func (cmap *myConcurrentMap) ToMap() map[interface{}]interface{} {
cmap.rwmutex.RLock()
defer cmap.rwmutex.RUnlock()
replica := make(map[interface{}]interface{})
for k, v := range cmap.m {
replica[k] = v
}
return replica
}
func (cmap *myConcurrentMap) KeyType() reflect.Type {
return cmap.keyType
}
func (cmap *myConcurrentMap) ElemType() reflect.Type {
return cmap.elemType
}
func (cmap *myConcurrentMap) String() string {
var buf bytes.Buffer
buf.WriteString("ConcurrentMap<")
buf.WriteString(cmap.keyType.Kind().String())
buf.WriteString(",")
buf.WriteString(cmap.elemType.Kind().String())
buf.WriteString(">{")
first := true
for k, v := range cmap.m {
if first {
first = false
} else {
buf.WriteString(" ")
}
buf.WriteString(fmt.Sprintf("%v", k))
buf.WriteString(":")
buf.WriteString(fmt.Sprintf("%v", v))
}
buf.WriteString("}")
return buf.String()
}
func NewConcurrentMap(keyType, elemType reflect.Type) ConcurrentMap {
return &myConcurrentMap{
keyType: keyType,
elemType: elemType,
m: make(map[interface{}]interface{})}
}
package map1

import (
"bytes"
"fmt"
"reflect"
"sync"
)

type ConcurrentMap interface {
GenericMap
}

type myConcurrentMap struct {
m map[interface{}]interface{}
keyType reflect.Type
elemType reflect.Type
rwmutex sync.RWMutex
}

func (cmap *myConcurrentMap) Get(key interface{}) interface{} {
cmap.rwmutex.RLock()
defer cmap.rwmutex.RUnlock()
return cmap.m[key]
}

func (cmap *myConcurrentMap) isAcceptablePair(k, e interface{}) bool {
if k == nil || reflect.TypeOf(k) != cmap.keyType {
return false
}
if e == nil || reflect.TypeOf(e) != cmap.elemType {
return false
}
return true
}

func (cmap *myConcurrentMap) Put(key interface{}, elem interface{}) (interface{}, bool) {
if !cmap.isAcceptablePair(key, elem) {
return nil, false
}
cmap.rwmutex.Lock()
defer cmap.rwmutex.Unlock()
oldElem := cmap.m[key]
cmap.m[key] = elem
return oldElem, true
}

func (cmap *myConcurrentMap) Remove(key interface{}) interface{} {
cmap.rwmutex.Lock()
defer cmap.rwmutex.Unlock()
oldElem := cmap.m[key]
delete(cmap.m, key)
return oldElem
}

func (cmap *myConcurrentMap) Clear() {
cmap.rwmutex.Lock()
defer cmap.rwmutex.Unlock()
cmap.m = make(map[interface{}]interface{})
}

func (cmap *myConcurrentMap) Len() int {
cmap.rwmutex.RLock()
defer cmap.rwmutex.RUnlock()
return len(cmap.m)
}

func (cmap *myConcurrentMap) Contains(key interface{}) bool {
cmap.rwmutex.RLock()
defer cmap.rwmutex.RUnlock()
_, ok := cmap.m[key]
return ok
}

func (cmap *myConcurrentMap) Keys() []interface{} {
cmap.rwmutex.RLock()
defer cmap.rwmutex.RUnlock()
initialLen := len(cmap.m)
keys := make([]interface{}, initialLen)
index := 0
for k, _ := range cmap.m {
keys[index] = k
index++
}
return keys
}

func (cmap *myConcurrentMap) Elems() []interface{} {
cmap.rwmutex.RLock()
defer cmap.rwmutex.RUnlock()
initialLen := len(cmap.m)
elems := make([]interface{}, initialLen)
index := 0
for _, v := range cmap.m {
elems[index] = v
index++
}
return elems
}

func (cmap *myConcurrentMap) ToMap() map[interface{}]interface{} {
cmap.rwmutex.RLock()
defer cmap.rwmutex.RUnlock()
replica := make(map[interface{}]interface{})
for k, v := range cmap.m {
replica[k] = v
}
return replica
}

func (cmap *myConcurrentMap) KeyType() reflect.Type {
return cmap.keyType
}

func (cmap *myConcurrentMap) ElemType() reflect.Type {
return cmap.elemType
}

func (cmap *myConcurrentMap) String() string {
var buf bytes.Buffer
buf.WriteString("ConcurrentMap<")
buf.WriteString(cmap.keyType.Kind().String())
buf.WriteString(",")
buf.WriteString(cmap.elemType.Kind().String())
buf.WriteString(">{")
first := true
for k, v := range cmap.m {
if first {
first = false
} else {
buf.WriteString(" ")
}
buf.WriteString(fmt.Sprintf("%v", k))
buf.WriteString(":")
buf.WriteString(fmt.Sprintf("%v", v))
}
buf.WriteString("}")
return buf.String()
}

func NewConcurrentMap(keyType, elemType reflect.Type) ConcurrentMap {
return &myConcurrentMap{
keyType: keyType,
elemType: elemType,
m: make(map[interface{}]interface{})}
}
Loading