set

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2022 License: MIT Imports: 2 Imported by: 0

README

set

set is a data structure package written by Go. It provides some basic set functionalities of the user. It uses map data structure under the hood. It does not have any dependency.

Installation

go get github.com/gozeloglu/set

Example

package main

import (
    "fmt"
    "github.com/gozeloglu/set"
)

func main() {
	s := set.New()
	s.Add(123)

	exist := s.Contains(123)
	if !exist {
		fmt.Println("123 not exist")
	}

	s.Append(1, 2, 3, 4, "abc")    // Add multiple values
	values := []interface{}{"github", 100, 640, 0.43, false}
	s.Append(values...) // Append the array of elements 

	s.Remove(4)
	size := s.Size()
	fmt.Println(size)   // Prints 5

	s.Pop()    // Returns random value from the set
	s.Clear()
	fmt.Println(s.Size())   // Prints 0
	
	if s.Empty() {
            fmt.Println("set is empty")
        }   
}

Supported methods:

  • Add(val interface{})
  • Append(val ...interface{})
  • Remove(val interface{})
  • Contains(val interface{})
  • Size()
  • Pop()
  • Clear()
  • Empty()
  • Slice()

Tests

You can run the tests with the following command.

go test ./...

You can check the code coverage with the following commands:

go test -coverprofile=coverage.out
go tool cover -html=coverage.out -o coverage.html

LICENSE

MIT

Documentation

Overview

Package set is a set package provides set data structure for Go. It is written without any dependency.

package main

import (
	"fmt"
	"github.com/gozeloglu/set"
)

func main() {
	s := set.New()
	s.Add(123)

	exist := s.Contains(123)
	if !exist {
		fmt.Println("123 not exist")
	}

	s.Append(1, 2, 3, 4, "abc")    // Add multiple values
	values := []interface{}{"github", 100, 640, 0.43, false}
	s.Append(values...) // Append the array of elements

	s.Remove(4)
	size := s.Size()
	fmt.Println(size)   // Prints 5

	s.Pop()    // Returns random value from the set
	s.Clear()
	fmt.Println(s.Size())   // Prints 0
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type S

type S interface {
	Add(val interface{})
	Append(val ...interface{})
	Remove(val interface{})
	Contains(val interface{}) bool
	Size() uint
	Pop() interface{}
	Clear()
	Empty() bool
	Slice() []interface{}
}

S is set interface.

type Set

type Set struct {
	// contains filtered or unexported fields
}

Set is the data structure which provides some functionalities.

func New

func New() *Set

New creates a set data structure.

func (*Set) Add

func (s *Set) Add(val interface{})

Add adds a new values to set if there is enough capacity.

func (*Set) Append

func (s *Set) Append(values ...interface{})

Append adds multiple values into set.

func (*Set) Clear

func (s *Set) Clear()

Clear removes everything from the set.

func (Set) Contains

func (s Set) Contains(val interface{}) bool

Contains checks the value whether exists in the set.

func (Set) Empty

func (s Set) Empty() bool

Empty checks whether the set is empty.

func (Set) Pop

func (s Set) Pop() interface{}

Pop returns a random value from the set. If there is no element in set, it returns nil.

func (*Set) Remove

func (s *Set) Remove(val interface{})

Remove deletes the given value.

func (Set) Size

func (s Set) Size() uint

Size returns the length of the set which means that number of value of the set.

func (Set) Slice added in v0.2.0

func (s Set) Slice() []interface{}

Slice returns the elements of the set as a slice. The slice type is interface{}. The elements can be in any order.

Jump to

Keyboard shortcuts

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