66[ ![ Stand With Ukraine] ( https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/badges/StandWithUkraine.svg )] ( https://stand-with-ukraine.pp.ua )
77[ ![ License: MIT] ( https://img.shields.io/badge/License-MIT-yellow.svg )] ( https://opensource.org/licenses/MIT )
88
9+ ## API CHANGE !!!
10+
11+ The API has changed from v0.1.0 to v0.2.0
12+
913## Overview
1014
1115` package interval ` is an immutable datastructure for fast lookups in one dimensional intervals.
1216
1317The implementation is based on treaps, augmented for intervals. Treaps are randomized self balancing binary search trees.
1418
15- Immutability is achieved because insert/delete will return a new treap which will share some nodes with the original treap.
16- All nodes are read-only after creation, allowing concurrent readers to operate safely with concurrent writers.
17-
18- The time complexity is ** O(log(n))** or ** O(k+log(n))** where k is the number of returned items, the space complexity is ** O(n)** .
19-
20- ```
21- Insert() O(log(n))
22- Delete() O(log(n))
23- Find() O(log(n))
24- Intersects() O(log(n))
25- CoverLCP() O(log(n))
26- CoverSCP() O(log(n))
27-
28- Covers() O(k+log(n))
29- CoveredBy() O(k+log(n))
30- Precedes() O(k+log(n))
31- PrecededBy() O(k+log(n))
32- Intersections() O(k+log(n))
33- ```
34-
3519The author is propably the first (in december 2022) using augmented treaps
3620as a very promising [ data structure] for the representation of dynamic IP address tables
3721for arbitrary ranges, that enables most and least specific range matching and even more lookup methods
@@ -79,15 +63,18 @@ To apply this library to types of one-dimensional intervals, you must provide a
7963 import " github.com/gaissmai/interval"
8064
8165 type Tree [T any] struct { ... }
82- func NewTree [T any](cmp func (a , b T ) (ll , rr , lr , rl int ), items ...T ) Tree[T]
66+ func NewTree [T any](cmp func (a , b T ) (ll , rr , lr , rl int ), items ...T ) * Tree[T]
8367
84- func NewTreeConcurrent[T any](jobs int, cmp func(a, b T) (ll, rr, lr, rl int), items ...T) Tree[T]
68+ func NewTreeConcurrent[T any](jobs int, cmp func(a, b T) (ll, rr, lr, rl int), items ...T) * Tree[T]
8569
86- func (t Tree[T]) Insert(items ...T) Tree[T]
87- func (t Tree[T]) Delete(item T) (Tree[T], bool)
70+ func (t *Tree[T]) Insert(items ...T)
71+ func (t *Tree[T]) Delete(item T) bool
72+ func (t *Tree[T]) Union(other *Tree[T], overwrite bool)
8873
89- func (t *Tree[T]) InsertMutable(items ...T)
90- func (t *Tree[T]) DeleteMutable(item T) bool
74+ func (t Tree[T]) InsertImmutable(items ...T) *Tree[T]
75+ func (t Tree[T]) DeleteImmutable(item T) (*Tree[T], bool)
76+ func (t Tree[T]) UnionImmutable(other *Tree[T], overwrite bool) *Tree[T]
77+ func (t Tree[T]) Clone() *Tree[T]
9178
9279 func (t Tree[T]) Find(item T) (result T, ok bool)
9380 func (t Tree[T]) CoverLCP(item T) (result T, ok bool)
@@ -102,9 +89,6 @@ To apply this library to types of one-dimensional intervals, you must provide a
10289
10390 func (t Tree[T]) Intersections(item T) []T
10491
105- func (t Tree[T]) Clone() Tree[T]
106- func (t Tree[T]) Union(other Tree[T], overwrite bool, immutable bool) Tree[T]
107-
10892 func (t Tree[T]) Visit(start, stop T, visitFn func(item T) bool)
10993 func (t Tree[T]) Fprint(w io.Writer) error
11094 func (t Tree[T]) String() string
@@ -135,21 +119,14 @@ goos: linux
135119goarch: amd64
136120pkg: github.com/gaissmai/interval
137121cpu: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
138- BenchmarkInsert/Into1-8 7488871 189 ns/op 128 B/op 2 allocs/op
139- BenchmarkInsert/Into10-8 3276116 398 ns/op 256 B/op 4 allocs/op
140- BenchmarkInsert/Into100-8 1496136 951 ns/op 512 B/op 8 allocs/op
141- BenchmarkInsert/Into1_000-8 533299 1959 ns/op 960 B/op 15 allocs/op
142- BenchmarkInsert/Into10_000-8 727627 1398 ns/op 832 B/op 13 allocs/op
143- BenchmarkInsert/Into100_000-8 342596 3339 ns/op 1600 B/op 25 allocs/op
144- BenchmarkInsert/Into1_000_000-8 396890 3681 ns/op 1728 B/op 27 allocs/op
145-
146- BenchmarkInsertMutable/Into1-8 10415164 128 ns/op 64 B/op 1 allocs/op
147- BenchmarkInsertMutable/Into10-8 5160836 245 ns/op 64 B/op 1 allocs/op
148- BenchmarkInsertMutable/Into100-8 2707705 443 ns/op 64 B/op 1 allocs/op
149- BenchmarkInsertMutable/Into1_000-8 1694250 723 ns/op 64 B/op 1 allocs/op
150- BenchmarkInsertMutable/Into10_000-8 1204220 963 ns/op 64 B/op 1 allocs/op
151- BenchmarkInsertMutable/Into100_000-8 966566 1249 ns/op 64 B/op 1 allocs/op
152- BenchmarkInsertMutable/Into1_000_000-8 645523 1863 ns/op 64 B/op 1 allocs/op
122+
123+ BenchmarkInsert/Into1-8 10415164 128 ns/op 64 B/op 1 allocs/op
124+ BenchmarkInsert/Into10-8 5160836 245 ns/op 64 B/op 1 allocs/op
125+ BenchmarkInsert/Into100-8 2707705 443 ns/op 64 B/op 1 allocs/op
126+ BenchmarkInsert/Into1_000-8 1694250 723 ns/op 64 B/op 1 allocs/op
127+ BenchmarkInsert/Into10_000-8 1204220 963 ns/op 64 B/op 1 allocs/op
128+ BenchmarkInsert/Into100_000-8 966566 1249 ns/op 64 B/op 1 allocs/op
129+ BenchmarkInsert/Into1_000_000-8 645523 1863 ns/op 64 B/op 1 allocs/op
153130```
154131
155132### Delete
@@ -162,31 +139,27 @@ goos: linux
162139goarch: amd64
163140pkg: github.com/gaissmai/interval
164141cpu: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
165- BenchmarkDelete/DeleteFrom10-8 8018518 150 ns/op 128 B/op 2 allocs/op
166- BenchmarkDelete/DeleteFrom100-8 2349710 708 ns/op 448 B/op 7 allocs/op
167- BenchmarkDelete/DeleteFrom1_000-8 616173 2051 ns/op 1536 B/op 24 allocs/op
168- BenchmarkDelete/DeleteFrom10_000-8 446180 2362 ns/op 1856 B/op 29 allocs/op
169- BenchmarkDelete/DeleteFrom100_000-8 272798 4224 ns/op 2816 B/op 44 allocs/op
170- BenchmarkDelete/DeleteFrom1_000_000-8 231808 5897 ns/op 3520 B/op 55 allocs/op
171-
172- BenchmarkDeleteMutable/DeleteFrom10-8 7682869 156 ns/op 0 B/op 0 allocs/op
173- BenchmarkDeleteMutable/DeleteFrom100-8 13009023 92 ns/op 0 B/op 0 allocs/op
174- BenchmarkDeleteMutable/DeleteFrom1_000-8 1912417 627 ns/op 0 B/op 0 allocs/op
175- BenchmarkDeleteMutable/DeleteFrom10_000-8 1362752 889 ns/op 0 B/op 0 allocs/op
176- BenchmarkDeleteMutable/DeleteFrom100_000-8 893157 1334 ns/op 0 B/op 0 allocs/op
177- BenchmarkDeleteMutable/DeleteFrom1_000_000-8 647199 1828 ns/op 0 B/op 0 allocs/op
142+
143+ BenchmarkDelete/DeleteFrom10-8 7682869 156 ns/op 0 B/op 0 allocs/op
144+ BenchmarkDelete/DeleteFrom100-8 13009023 92 ns/op 0 B/op 0 allocs/op
145+ BenchmarkDelete/DeleteFrom1_000-8 1912417 627 ns/op 0 B/op 0 allocs/op
146+ BenchmarkDelete/DeleteFrom10_000-8 1362752 889 ns/op 0 B/op 0 allocs/op
147+ BenchmarkDelete/DeleteFrom100_000-8 893157 1334 ns/op 0 B/op 0 allocs/op
148+ BenchmarkDelete/DeleteFrom1_000_000-8 647199 1828 ns/op 0 B/op 0 allocs/op
178149```
179150
180151### Lookups
181152
182- The benchmark for `CoverLCP()` (a.k.a. longest-prefix-match if the interval is an IP CIDR prefix) is very promising:
153+ The benchmark for `CoverLCP()` (also known as longest-prefix-match when the interval is an IP-CIDR prefix) is very
154+ promising, as it is a generalized algorithm that is not specifically optimized only for IP address lookup:
183155
184156```
185157$ go test -benchmem -bench='CoverLCP'
186158goos: linux
187159goarch: amd64
188160pkg: github.com/gaissmai/interval
189161cpu: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
162+
190163BenchmarkCoverLCP/In100-8 4833073 239 ns/op 0 B/op 0 allocs/op
191164BenchmarkCoverLCP/In1_000-8 3714054 323 ns/op 0 B/op 0 allocs/op
192165BenchmarkCoverLCP/In10_000-8 1897353 630 ns/op 0 B/op 0 allocs/op
@@ -203,6 +176,7 @@ goos: linux
203176goarch: amd64
204177pkg: github.com/gaissmai/interval
205178cpu: Intel(R) Core(TM) i5-7500T CPU @ 2.70GHz
179+
206180BenchmarkIntersects/In1-4 58757540 19 ns/op 0 B/op 0 allocs/op
207181BenchmarkIntersects/In10-4 27267051 40 ns/op 0 B/op 0 allocs/op
208182BenchmarkIntersects/In100-4 17559418 60 ns/op 0 B/op 0 allocs/op
@@ -220,6 +194,7 @@ goos: linux
220194goarch: amd64
221195pkg: github.com/gaissmai/interval
222196cpu: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
197+
223198BenchmarkFind/In100-8 17299449 63 ns/op 0 B/op 0 allocs/op
224199BenchmarkFind/In1_000-8 17327350 69 ns/op 0 B/op 0 allocs/op
225200BenchmarkFind/In10_000-8 12858908 90 ns/op 0 B/op 0 allocs/op
0 commit comments