Git Product home page Git Product logo

godazzle's Introduction

Go-Dazzle

a Golang datastructure package

Features

  • Multiple data structure supporting
  • Some structures are Concurrency Safe
  • Embedded JSON Encoder/Decoder

Getting GoDazzle

Run:

go get -u github.com/454270186/GoDazzle

Example

package main

import (
	"fmt"

	"github.com/454270186/GoDazzle/cmp"
	"github.com/454270186/GoDazzle/list/linklist"
)

func main() {
	list := linklist.New() // create a linklist

	// Add value
	list.Add(5, 4, 3, 2, 1)
	fmt.Println(list.Values()...) // [5, 4, 3, 2, 1]

	// Get value by index
	fmt.Println(list.Get(1)) // 4

	// Remove value by index
	list.Remove(3)
	fmt.Println(list.Values()...) // [5, 4, 3, 1]

	// Sort list by built-in comparator
	list.Sort(cmp.IntComparator)
	fmt.Println(list.Values()...) // [1, 3, 4, 5]

	// Sort by custom comparator
	list.Sort(func(a, b interface{}) int {
		num1 := a.(int)
		num2 := b.(int)

		if num1 > num2 {
			return -1
		}

		return 0
	})
	fmt.Println(list.Values()...) // [5, 4, 3, 1]
}

Usage

Container

All data structures have implement container interface

type Container interface {
	Empty() bool
	Size() int
	Clear()
	Values() []any
	String() string
}

List

  • Arraylist
  • Linklist
  • Doubly linked list

list interface impl Container, JsonCoder, JsonDecoder

type List interface {
	Get(index int) (interface{}, bool)
	Remove(index int)
	Add(values ...interface{})
	Contains(value interface{}) bool
	Sort(cmpFunc cmp.Comparator)
	
	container.Container
}

JSON

Example

	data := `[1, 2, 3, 4, 5]`
	d := linklist.New()
	d.FromJson([]byte(data)) // equal to json.Unmarshal([]byte(data), d)
	fmt.Println(d.Values()...)

Output:

1 2 3 4 5

Map

  • GoMap -- native map
  • SyncMap -- concurrency safe map

map interface impl Container, JsonCoder, JsonDecoder

type Map interface {
	Put(key, value interface{})
	Get(key interface{}) (interface{}, bool)
	Remove(key interface{})
	Keys() []interface{}

	container.Container
}

Queue

  • LinkQueue
  • LoopQueue
  • PriorityQueue
  • SyncQueue

queue interface

type Queue interface {
	Push(val interface{})
	Pop() bool
	Front() (interface{}, bool)
	Sort(cmpFunc cmp.Comparator)

	container.Container
}

Benchmark

Run:

go test -run=NO_TEST -bench . -benchmem  -benchtime 1s ./...

godazzle's People

Contributors

454270186 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.