Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
igrmk committed Aug 20, 2018
0 parents commit a4dc948
Show file tree
Hide file tree
Showing 12 changed files with 2,617 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*
!*/
!*.*
*.swp
*.orig
workspace.xml
.idea
2 changes: 2 additions & 0 deletions .ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.git
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
TreeMap
=======

[![GoDoc](https://godoc.org/github.com/igrmk/treemap?status.svg)](https://godoc.org/github.com/igrmk/treemap)
[![WTFPL License](https://img.shields.io/badge/license-wtfpl-blue.svg)](http://www.wtfpl.net/about/)
[![Build Status](https://travis-ci.org/igrmk/treemap.svg)](https://travis-ci.org/igrmk/treemap)
[![Coverage Status](https://coveralls.io/repos/igrmk/treemap/badge.svg?branch=master)](https://coveralls.io/r/igrmk/treemap?branch=master)
[![GoReportCard](http://goreportcard.com/badge/igrmk/treemap)](http://goreportcard.com/report/igrmk/treemap)

Generic `TreeMap` uses red-black tree under the hood.
This is gotemplate ready package.
You can use it as a template to generate `TreeMap` with specific `Key` and `Value` types.
See example folder for generating `TreeMap<int, string>`.
The package is based on github.com/logrusorgru/ebony due to outstanding test coverage.
It is not thread safe.

### Usage

```go
package main

import "fmt"

//go:generate gotemplate "github.com/igrmk/treemap" "intStringTreeMap(int, string)"

func less(x int, y int) bool { return x < y }

func main() {
tr := newIntStringTreeMap(less)
tr.Set(0, "Hello")
tr.Set(1, "World")

for it := tr.Iterator(); it.HasNext(); {
k, v := it.Next()
fmt.Println(k, v)
}
}
```

### Complexity

| Name | Time |
|:-----------:|:---------:|
| `Set` | O(log*N*) |
| `Del` | O(log*N*) |
| `Get` | O(log*N*) |
| `Contains` | O(log*N*) |
| `Count` | O(1) |
| `Max` | O(log*N*) |
| `Min` | O(log*N*) |
| `Clear` | O(1) |
| `Range` | O(log*N*) |
| Iteration | O(*N*) |

### Memory usage

TreeMap uses O(N) memory.

### Install

Get or update

```bash
go get github.com/igrmk/treemap
```

### Licensing

Copyright &copy; 2018 Igor Mikushkin <igor.mikushkin@gmail.com>
This work is free. You can redistribute it and/or modify it under the
terms of the Do What The Fuck You Want To Public License, Version 2,
as published by Sam Hocevar. See the LICENSE file for more details.
263 changes: 263 additions & 0 deletions benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
//
// Copyright (c) 2018 Igor Mikushkin <igor.mikushkin@gmail.com>.
// All rights reserved. This program is free software. It comes without
// any warranty, to the extent permitted by applicable law. You can
// redistribute it and/or modify it under the terms of the Do What
// The Fuck You Want To Public License, Version 2, as published by
// Sam Hocevar. See LICENSE file for more details or see below.
//

//
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
// Version 2, December 2004
//
// Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
//
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
//
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
//
// 0. You just DO WHAT THE FUCK YOU WANT TO.
//

package treemap

import (
"math/rand"
"testing"
)

const MapSize = 10000

func BenchmarkSeqSet(b *testing.B) {
for i := 0; i < b.N; i++ {
tr := New(less)
for j := 0; j < MapSize; j++ {
tr.Set(j, "")
}
}
b.ReportAllocs()
}

func BenchmarkSeqGet(b *testing.B) {
b.StopTimer()
tr := New(less)
for i := 0; i < MapSize; i++ {
tr.Set(i, "")
}
b.StartTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < MapSize; j++ {
tr.Get(j)
}
}
b.ReportAllocs()
}

func BenchmarkSeqSetDel(b *testing.B) {
b.StopTimer()
tr := New(less)
b.StartTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < MapSize; j++ {
tr.Set(j, "")
}
for j := 0; j < MapSize; j++ {
tr.Del(j)
}
}
b.ReportAllocs()
}

func BenchmarkSeqExs(b *testing.B) {
b.StopTimer()
tr := New(less)
for i := 0; i < MapSize; i++ {
tr.Set(i, "")
}
b.StartTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < MapSize; j++ {
tr.Contains(j)
}
}
b.ReportAllocs()
}

func BenchmarkSeqMin(b *testing.B) {
b.StopTimer()
tr := New(less)
for i := 0; i < MapSize; i++ {
tr.Set(i, "")
}
b.StartTimer()
for i := 0; i < b.N; i++ {
tr.Min()
}
b.ReportAllocs()
}

func BenchmarkSeqMax(b *testing.B) {
b.StopTimer()
tr := New(less)
for i := 0; i < MapSize; i++ {
tr.Set(i, "")
}
b.StartTimer()
for i := 0; i < b.N; i++ {
tr.Max()
}
b.ReportAllocs()
}

func BenchmarkSeqIter(b *testing.B) {
b.StopTimer()
tr := New(less)
for i := 0; i < MapSize; i++ {
tr.Set(i, "")
}
b.StartTimer()
for i := 0; i < b.N; i++ {
for it := tr.Iterator(); it.HasNext(); {
it.Next()
}
}
b.ReportAllocs()
}

// random

func shuffle(ary []Key) {
for i := range ary {
j := rand.Intn(i + 1)
ary[i], ary[j] = ary[j], ary[i]
}
}

func BenchmarkRndSet(b *testing.B) {
t := int64(MapSize * 2)
b.StopTimer()
ks := make([]Key, MapSize)
for i := range ks {
ks[i] = int(rand.Int63n(t))
}
tr := New(less)
b.StartTimer()
for i := 0; i < b.N; i++ {
for _, k := range ks {
tr.Set(k, "")
}
tr.Clear()
}
b.ReportAllocs()
}

func BenchmarkRndGet(b *testing.B) {
b.StopTimer()
tr := New(less)
ks := make([]Key, MapSize)
t := int64(MapSize * 2)
for i := range ks {
k := int(rand.Int63n(t))
ks[i] = k
tr.Set(k, "")
}
shuffle(ks)
b.StartTimer()
for i := 0; i < b.N; i++ {
for _, k := range ks {
tr.Get(k)
}
}
b.ReportAllocs()
}

func BenchmarkRndSetDel(b *testing.B) {
b.StopTimer()
ks := make([]Key, MapSize)
t := int64(MapSize * 2)
for i := range ks {
ks[i] = int(rand.Int63n(t))
}
shuffle(ks)
tr := New(less)
b.StartTimer()
for i := 0; i < b.N; i++ {
for _, k := range ks {
tr.Set(k, "")
}
for _, k := range ks {
tr.Del(k)
}
}
b.ReportAllocs()
}

func BenchmarkRndExs(b *testing.B) {
b.StopTimer()
tr := New(less)
ks := make([]Key, MapSize)
t := int64(MapSize * 2)
for i := range ks {
ks[i] = int(rand.Int63n(t))
tr.Set(ks[i], "")
}
shuffle(ks)
b.StartTimer()
for i := 0; i < b.N; i++ {
for _, t := range ks {
tr.Contains(t)
}
}
b.ReportAllocs()
}

func BenchmarkRndMin(b *testing.B) {
b.StopTimer()
tr := New(less)
t := int64(MapSize * 2)
for i := 0; i < MapSize; i++ {
k := int(rand.Int63n(t))
tr.Set(k, "")
}
b.StartTimer()
for i := 0; i < b.N; i++ {
tr.Min()
}
b.ReportAllocs()
}

func BenchmarkRndMax(b *testing.B) {
b.StopTimer()
tr := New(less)
t := int64(MapSize * 2)
for i := 0; i < MapSize; i++ {
k := int(rand.Int63n(t))
tr.Set(k, "")
}
b.StartTimer()
for i := 0; i < b.N; i++ {
tr.Max()
}
b.ReportAllocs()
}

func BenchmarkRndIter(b *testing.B) {
b.StopTimer()
tr := New(less)
t := int64(MapSize * 2)
for i := 0; i < MapSize; i++ {
k := int(rand.Int63n(t))
tr.Set(k, "")
}
b.StartTimer()
for i := 0; i < b.N; i++ {
for it := tr.Iterator(); it.HasNext(); {
it.Next()
}
}
b.ReportAllocs()
}
Loading

0 comments on commit a4dc948

Please sign in to comment.