55)
66
77type dbscanClusterer struct {
8- minpts int
9- eps float64
8+ minpts int
9+ workers int
10+ eps float64
1011
1112 l , s , o , f int
1213
@@ -35,11 +36,15 @@ type nearestJob struct {
3536}
3637
3738/* Implementation of DBSCAN algorithm with concurrent moditication */
38- func DbscanClusterer (minpts int , eps float64 , distance DistanceFunc ) (HardClusterer , error ) {
39+ func DbscanClusterer (minpts int , eps float64 , workers int , distance DistanceFunc ) (HardClusterer , error ) {
3940 if minpts < 1 {
4041 return nil , ErrZeroMinpts
4142 }
4243
44+ if workers < 0 {
45+ return nil , ErrZeroWorkers
46+ }
47+
4348 if eps <= 0 {
4449 return nil , ErrZeroEpsilon
4550 }
@@ -55,6 +60,7 @@ func DbscanClusterer(minpts int, eps float64, distance DistanceFunc) (HardCluste
5560
5661 return & dbscanClusterer {
5762 minpts : minpts ,
63+ workers : workers ,
5864 eps : eps ,
5965 distance : d ,
6066 }, nil
@@ -76,7 +82,7 @@ func (c *dbscanClusterer) Learn(data [][]float64) error {
7682 c .mu .Lock ()
7783
7884 c .l = len (data )
79- c .s = numWorkers ( c . l )
85+ c .s = c . numWorkers ( )
8086 c .o = c .s - 1
8187 c .f = c .l / c .s
8288
@@ -196,8 +202,6 @@ func (c *dbscanClusterer) nearest(p *int, l *int, r *[]int) {
196202
197203 * r = (* r )[:0 ]
198204
199- c .m = & sync.Mutex {}
200- c .w = & sync.WaitGroup {}
201205 c .p = & c .d [* p ]
202206 c .r = r
203207
@@ -222,7 +226,10 @@ func (c *dbscanClusterer) nearest(p *int, l *int, r *[]int) {
222226}
223227
224228func (c * dbscanClusterer ) startWorkers () {
225- c .j = make (chan * nearestJob , c .s )
229+ c .j = make (chan * nearestJob , c .l )
230+
231+ c .m = & sync.Mutex {}
232+ c .w = & sync.WaitGroup {}
226233
227234 for i := 0 ; i < c .s ; i ++ {
228235 go c .nearestWorker ()
@@ -247,16 +254,32 @@ func (c *dbscanClusterer) nearestWorker() {
247254 }
248255}
249256
250- func numWorkers (a int ) int {
257+ func (c * dbscanClusterer ) numWorkers () int {
258+ var (
259+ a int = c .l
260+ b int
261+ )
262+
251263 if a < 1000 {
252- return 1
264+ b = 1
253265 } else if a < 10000 {
254- return 10
266+ b = 10
255267 } else if a < 100000 {
256- return 100
268+ b = 100
257269 } else if a < 1000000 {
258- return 1000
270+ b = 1000
259271 } else {
260- return 10000
272+ b = 10000
273+ }
274+
275+ if c .workers == 0 {
276+ return b
261277 }
278+
279+ if c .workers < b {
280+ return c .workers
281+ }
282+
283+ return b
284+
262285}
0 commit comments