gotypes

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2020 License: GPL-2.0 Imports: 0 Imported by: 0

README

go types

Introduction

The implementation of data type, such as simple array list, linked list, queue, stack, etc. Uses the idea of lambda in the implementation.

  1. BufferedChannel : Buffered IO channel
  2. HashMap : A HashMap based on Go's map
  3. ArrayList : A safe ArrayList
  4. LinkedList : A safe LinkedList
  5. Queue : A safe FIFO Queue
  6. Stack : A safe LIFO Queue
  7. HashSet : A safe HashSet based on Go's map
  8. FileSplitter : File Splitter by line
  9. ...

Install

  1. Git
mkdir -p $GOPATH/src/gitee.com/Billcoding/gotypes

cd $GOPATH/src/gitee.com/Billcoding

git clone https://gitee.com/Billcoding/gotypes.git gotypes
  1. IDE(Goland)
import "gitee.com/Billcoding/gotypes/set"

Usage

  • BufferedChannel
package main

import (
	"fmt"
)

func main() {
	//create new buffered file channel
	channel := NewBufferedChannel("text.txt", 1024)
	var buffer *[]byte
	//read by rune 3
	buffer = channel.ReadRune('3', true)
	if buffer != nil {
		fmt.Println(string(*buffer))
	}

	//set offset 0
	channel.SetOffset(0)
	for {
		//read line
		buffer = channel.ReadLine()
		if buffer == nil {
			break
		}
		fmt.Println(string(*buffer))
	}

	channel.SetOffset(0)
	//read all bytes
	buffer = channel.ReadEnd()
	if buffer != nil {
		fmt.Println(string(*buffer))
	}
}

  • HashMap
package main

import (
	"fmt"
)

func main() {
	//Get new HashMap
	hashMap := NewHashMap()

	//Put an element
	hashMap.Put("abc", "a")
	hashMap.Put("111", "1")
	hashMap.Put("222", "2")

	//Check key exists
	fmt.Println(hashMap.Key("abc"))
	//Check Value exists
	fmt.Println(hashMap.Value("abc"))

	//Foreach HashMap
	hashMap.ForEach(func(k Key, v Value) {
		fmt.Println(k, " = ", v)
	})

}

  • ArrayList
package main

import (
	"fmt"
)

func main() {
	//Get new ArrayList
	arrayList := NewArrayList()

	//Add element
	arrayList.Add(10)
	arrayList.Add(100)
	arrayList.Add(1000)

	//Get ArrayList size
	fmt.Println(arrayList.Size())

	//Get ArrayList [0] element
	fmt.Println(arrayList.Get(0))

	//Foreach function
	arrayList.ForEach(func(e *gotypes.Element) {
		fmt.Println(*e)
	})

	//Reduce function
	fmt.Println("Reduce : ", arrayList.Reduce(0, func(val gotypes.Element, e *gotypes.Element) Element {
		val = (*e).(int) + val.(int)
		return val
	}))

	//Clear elements
	arrayList.Clear()

}

  • LinkedList
package main

import (
	"fmt"
)

func main() {
	//Get new LinkedList
	linkedList := NewLinkedList()

	//Add element
	linkedList.Add(10)
	linkedList.Add(100)
	linkedList.Add(1000)
	linkedList.Add(10000)

	//Get head element and remove it
	fmt.Println(*linkedList.Take().Value)

	//Get tail element and remove it
	fmt.Println(*linkedList.Peek().Value)

	//Get LinkedList size
	fmt.Println(linkedList.Size())

	//Foreach function
	linkedList.ForEach(func(e *LinkedElement) {
		fmt.Println(*e.Value)
	})

}

  • Queue
package main

import (
	"fmt"
)

func main() {
	//Get new Queue
	queue := NewQueue()

	//Push element into Queue
	queue.Push(777)
	queue.Push(111)
	queue.Push(222)

	//Get element from Queue and remove it
	fmt.Println(*queue.Pull())

	//Foreach function
	queue.ForEach(func(e *gotypes.Element) {
		fmt.Println(*e)
	})
}

  • Stack
package stack

import (
	"fmt"
)

func main() {
	//Get new Stack
	stack := NewStack()

	//Push element into Stack bottom
	stack.Push(123)
	stack.Push("222")
	stack.Push("3333")
	stack.Push("4444")
	stack.Push("5555")

	//Pop element from Stack
	fmt.Println(*stack.Pop())
	fmt.Println(*stack.Pop())
	fmt.Println(*stack.Pop())

	//Get Stack's size
	fmt.Println(stack.Size())

	//Foreach function
	stack.ForEach(func(e *gotypes.Element) {
		fmt.Println(*e)
	})
}

  • HashSet
package main

import (
	"fmt"
)

func main() {
	//Get new HashSet
	hashSet := NewHashSet()

	//Add element into HashSet
	hashSet.Add("a")
	hashSet.Add("b")
	hashSet.Add("c")

	//Check element exists
	fmt.Println(hashSet.Contains("123"))

	//Reduce function
	fmt.Println("Reduce : ", hashSet.Reduce("", func(val gotypes.Element, e Element) Element {
		val = (val).(string) + (e).(string)
		return val
	}))

	//Foreach function
	hashSet.ForEach(func(e Element) {
		fmt.Println(e)
	})

}

  • FileSplitter
package main

func main() {
	//Get new FileSplitter
	splitter := NewFileSplitter("text.txt", "", 30)
	//Start split
	splitter.Split()
}

Docs

Wiki

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element interface{}

Define Element type

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL