Skip to content

Commit

Permalink
upload source code.
Browse files Browse the repository at this point in the history
  • Loading branch information
hyper0x committed Sep 23, 2014
1 parent a0b97d2 commit 04813d7
Show file tree
Hide file tree
Showing 84 changed files with 8,989 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bin**/*
pkg**/*
logs**/*
pprof**/*
_obj
*.exe
*.out
*.di
*.callgrind
.DS_Store
19 changes: 19 additions & 0 deletions src/basic/cgo/cgo_demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
cgolib "basic/cgo/lib"
"fmt"
)

func main() {
input := float32(2.33)
output, err := cgolib.Sqrt(input)
if err != nil {
fmt.Errorf("Error: %s\n", err)
}
fmt.Printf("The square root of %f is %f.\n", input, output)

cgolib.Print("ABC\n")

cgolib.CallCFunc()
}
18 changes: 18 additions & 0 deletions src/basic/cgo/lib/go_export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package lib

/*
#include <stdio.h>
extern void CFunction1();
*/
import "C"

import "fmt"

//export GoFunction1
func GoFunction1() {
fmt.Println("GoFunction1() is called.")
}

func CallCFunc() {
C.CFunction1()
}
10 changes: 10 additions & 0 deletions src/basic/cgo/lib/go_export_def.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package lib

/*
#include <stdio.h>
void CFunction1() {
printf("CFunction1() is called.\n");
GoFunction1();
}
*/
import "C"
12 changes: 12 additions & 0 deletions src/basic/cgo/lib/math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package lib

/*
#cgo LDFLAGS: -lm
#include <math.h>
*/
import "C"

func Sqrt(p float32) (float32, error) {
result, err := C.sqrt(C.double(p))
return float32(result), err
}
18 changes: 18 additions & 0 deletions src/basic/cgo/lib/print.go
Original file line number Diff line number Diff line change
@@ -0,0 +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)
}
14 changes: 14 additions & 0 deletions src/basic/cgo/lib/rand.go
Original file line number Diff line number Diff line change
@@ -0,0 +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))
}
146 changes: 146 additions & 0 deletions src/basic/map1/cmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +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{})}
}
Loading

0 comments on commit 04813d7

Please sign in to comment.