Skip to content

Commit

Permalink
first, most simple version
Browse files Browse the repository at this point in the history
  • Loading branch information
koron committed Aug 8, 2024
0 parents commit 4162cd2
Show file tree
Hide file tree
Showing 14 changed files with 327 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* -text
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "github-actions" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
29 changes: 29 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Go

on: [push]

env:
GO_VERSION: '>=1.21.0'

jobs:

build:
name: Build
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
steps:

- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}

- run: go test

- run: go build

# based on: github.com/koron-go/_skeleton/.github/workflows/go.yml
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*~
default.pgo
tags
tmp/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 MURAOKA Taro <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
42 changes: 42 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
TEST_PACKAGE ?= ./...

.PHONY: build
build:
go build -gcflags '-e' ./...

.PHONY: test
test:
go test $(TEST_PACKAGE)

.PHONY: bench
bench:
go test -bench $(TEST_PACKAGE)

.PHONY: tags
tags:
gotags -f tags -R .

.PHONY: cover
cover:
mkdir -p tmp
go test -coverprofile tmp/_cover.out $(TEST_PACKAGE)
go tool cover -html tmp/_cover.out -o tmp/cover.html

.PHONY: checkall
checkall: vet staticcheck

.PHONY: vet
vet:
go vet $(TEST_PACKAGE)

.PHONY: staticcheck
staticcheck:
staticcheck $(TEST_PACKAGE)

.PHONY: clean
clean:
go clean
rm -f tags
rm -f tmp/_cover.out tmp/cover.html

# based on: github.com/koron-go/_skeleton/Makefile
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# koron-go/fifo

[![PkgGoDev](https://pkg.go.dev/badge/github.com/koron-go/fifo)](https://pkg.go.dev/github.com/koron-go/fifo)
[![Actions/Go](https://github.com/koron-go/fifo/workflows/Go/badge.svg)](https://github.com/koron-go/fifo/actions?query=workflow%3AGo)
[![Go Report Card](https://goreportcard.com/badge/github.com/koron-go/fifo)](https://goreportcard.com/report/github.com/koron-go/fifo)

A simple generic FIFO queue.
48 changes: 48 additions & 0 deletions fifo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Package fifo provides simple FIFO (queue) implementation.
*/
package fifo

type entry[T any] struct {
value T
prev *entry[T]
}

// FIFO is a simple implementation of FIFO queue
type FIFO[T any] struct {
head *entry[T]
tail *entry[T]
len int
}

// Len gets a length of FIFO queue.
func (fifo FIFO[T]) Len() int {
return fifo.len
}

// Insert inserts a value into FIFO queue.
func (fifo *FIFO[T]) Insert(v T) {
item := &entry[T]{value: v}
if fifo.head != nil {
fifo.head.prev = item
} else {
fifo.tail = item
}
fifo.head = item
fifo.len++
}

// Evict evicts the last value from FIFO queue.
func (fifo *FIFO[T]) Evict() (T, bool) {
if fifo.tail == nil {
var zero T
return zero, false
}
v := fifo.tail.value
fifo.tail = fifo.tail.prev
if fifo.tail == nil {
fifo.head = nil
}
fifo.len--
return v, true
}
78 changes: 78 additions & 0 deletions fifo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package fifo_test

import (
"testing"

"github.com/koron-go/fifo"
)

func TestFIFO(t *testing.T) {
var fifo fifo.FIFO[int]
if d := fifo.Len(); d != 0 {
t.Fatalf("fifo length is not zero: got=%d", d)
}

// Insert
fifo.Insert(1)
fifo.Insert(2)
fifo.Insert(3)
if d := fifo.Len(); d != 3 {
t.Fatalf("fifo length is not 3: got=%d", d)
}
fifo.Insert(4)
fifo.Insert(5)
if d := fifo.Len(); d != 5 {
t.Fatalf("fifo length is not 5: got=%d", d)
}

// Evict
if v, ok := fifo.Evict(); !ok || v != 1 {
t.Fatalf("failed to evict want=1: ok=%t got=%d", ok, v)
}
if d := fifo.Len(); d != 4 {
t.Fatalf("fifo length is not 4: got=%d", d)
}
if v, ok := fifo.Evict(); !ok || v != 2 {
t.Fatalf("failed to evict want=2: ok=%t got=%d", ok, v)
}
if v, ok := fifo.Evict(); !ok || v != 3 {
t.Fatalf("failed to evict want=3: ok=%t got=%d", ok, v)
}
if v, ok := fifo.Evict(); !ok || v != 4 {
t.Fatalf("failed to evict want=4: ok=%t got=%d", ok, v)
}
if d := fifo.Len(); d != 1 {
t.Fatalf("fifo length is not 1: got=%d", d)
}
if v, ok := fifo.Evict(); !ok || v != 5 {
t.Fatalf("failed to evict want=5: ok=%t got=%d", ok, v)
}
if d := fifo.Len(); d != 0 {
t.Fatalf("fifo length is not 0: got=%d", d)
}

// Evict from empty FIFO queue.
if v, ok := fifo.Evict(); ok || v != 0 {
t.Fatalf("unexpectedly a value evicted from empty FIFO queue: ok=%t got=%d", ok, v)
}

// Insert and Evict again
fifo.Insert(6)
fifo.Insert(7)
fifo.Insert(8)
if d := fifo.Len(); d != 3 {
t.Fatalf("fifo length is not 3: got=%d", d)
}
if v, ok := fifo.Evict(); !ok || v != 6 {
t.Fatalf("failed to evict want=6: ok=%t got=%d", ok, v)
}
if v, ok := fifo.Evict(); !ok || v != 7 {
t.Fatalf("failed to evict want=7: ok=%t got=%d", ok, v)
}
if v, ok := fifo.Evict(); !ok || v != 8 {
t.Fatalf("failed to evict want=8: ok=%t got=%d", ok, v)
}
if d := fifo.Len(); d != 0 {
t.Fatalf("fifo length is not 0: got=%d", d)
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/koron-go/fifo

go 1.22
Empty file added go.sum
Empty file.
16 changes: 16 additions & 0 deletions rangefunc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build goexperiment.rangefunc

package fifo

import "iter"

// Seq returns iter.Seq[T] to enumerate items in FIFO queue.
func (fifo *FIFO[T]) Seq() iter.Seq[T] {
return func(yield func(T) bool) {
for p := fifo.tail; p != nil; p = p.prev {
if !yield(p.value) {
break
}
}
}
}
62 changes: 62 additions & 0 deletions rangefunc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//go:build goexperiment.rangefunc

package fifo_test

import (
"testing"

"github.com/koron-go/fifo"
)

func TestFIFO_Seq(t *testing.T) {
var fifo fifo.FIFO[int]
fifo.Insert(1)
fifo.Insert(2)
fifo.Insert(3)
fifo.Insert(4)
fifo.Insert(5)
wants := []int{1, 2, 3, 4, 5}
for got := range fifo.Seq() {
if len(wants) == 0 {
t.Fatalf("wants exhausted: got=%d", got)
}
want := wants[0]
wants = wants[1:]
if got != want {
t.Fatalf("unexpected value: want=%d got=%d", want, got)
}
}
if len(wants) != 0 {
t.Fatalf("wants remained: %+v", wants)
}
if d := fifo.Len(); d != 5 {
t.Fatalf("fifo length is not 5: got=%d", d)
}
}

func TestFIFO_SeqBreak(t *testing.T) {
var fifo fifo.FIFO[int]
fifo.Insert(1)
fifo.Insert(2)
fifo.Insert(3)
fifo.Insert(4)
fifo.Insert(5)

wants := []int{1, 2, 3}
for got := range fifo.Seq() {
if len(wants) == 0 {
break
}
want := wants[0]
wants = wants[1:]
if got != want {
t.Fatalf("unexpected value: want=%d got=%d", want, got)
}
}
if len(wants) != 0 {
t.Fatalf("wants remained: %+v", wants)
}
if d := fifo.Len(); d != 5 {
t.Fatalf("fifo length is not 5: got=%d", d)
}
}
5 changes: 5 additions & 0 deletions staticcheck.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# vim:set ft=toml:

checks = ["all"]

# based on: github.com/koron-go/_skeleton/staticcheck.conf

0 comments on commit 4162cd2

Please sign in to comment.