@@ -230,7 +230,34 @@ func (c Cell) GridDisk(k int) ([]Cell, error) {
230230 return GridDisk (c , k )
231231}
232232
233+ // GridDisksUnsafe produces cells within grid distance k of all provided origin
234+ // cells.
235+ //
236+ // k-ring 0 is defined as the origin cell, k-ring 1 is defined as k-ring 0 and
237+ // all neighboring cells, and so on.
238+ //
239+ // Outer slice is ordered in the same order origins were passed in. Inner slices
240+ // are in no particular order.
241+ //
242+ // This does not call through to the underlying C.gridDisksUnsafe implementation
243+ // as it is slightly easier to do so to avoid unnecessary type conversions.
244+ func GridDisksUnsafe (origins []Cell , k int ) ([][]Cell , error ) {
245+ out := make ([][]Cell , len (origins ))
246+ for i := range origins {
247+ inner := make ([]C.H3Index , maxGridDiskSize (k ))
248+ errC := C .gridDiskUnsafe (C .H3Index (origins [i ]), C .int (k ), & inner [0 ])
249+ if err := toErr (errC ); err != nil {
250+ return out , err
251+ }
252+ out [i ] = cellsFromC (inner , true , false )
253+ }
254+ return out , nil
255+ }
256+
233257// GridDiskDistances produces cells within grid distance k of the origin cell.
258+ // This method optimistically tries the faster GridDiskDistancesUnsafe first.
259+ // If a cell was a pentagon or was in the pentagon distortion area, it falls
260+ // back to GridDiskDistancesSafe.
234261//
235262// k-ring 0 is defined as the origin cell, k-ring 1 is defined as k-ring 0 and
236263// all neighboring cells, and so on.
@@ -260,6 +287,9 @@ func GridDiskDistances(origin Cell, k int) ([][]Cell, error) {
260287}
261288
262289// GridDiskDistances produces cells within grid distance k of the origin cell.
290+ // This method optimistically tries the faster GridDiskDistancesUnsafe first.
291+ // If a cell was a pentagon or was in the pentagon distortion area, it falls
292+ // back to GridDiskDistancesSafe.
263293//
264294// k-ring 0 is defined as the origin cell, k-ring 1 is defined as k-ring 0 and
265295// all neighboring cells, and so on.
@@ -271,6 +301,94 @@ func (c Cell) GridDiskDistances(k int) ([][]Cell, error) {
271301 return GridDiskDistances (c , k )
272302}
273303
304+ // GridDiskDistancesUnsafe produces cells within grid distance k of the origin cell.
305+ // Output behavior is undefined when one of the cells returned by this
306+ // function is a pentagon or is in the pentagon distortion area.
307+ //
308+ // k-ring 0 is defined as the origin cell, k-ring 1 is defined as k-ring 0 and
309+ // all neighboring cells, and so on.
310+ //
311+ // Outer slice is ordered from origin outwards. Inner slices are in no
312+ // particular order. Elements of the output array may be left zero, as can
313+ // happen when crossing a pentagon.
314+ func GridDiskDistancesUnsafe (origin Cell , k int ) ([][]Cell , error ) {
315+ rsz := maxGridDiskSize (k )
316+ outHexes := make ([]C.H3Index , rsz )
317+ outDists := make ([]C.int , rsz )
318+
319+ if err := toErr (C .gridDiskDistancesUnsafe (C .H3Index (origin ), C .int (k ), & outHexes [0 ], & outDists [0 ])); err != nil {
320+ return nil , err
321+ }
322+
323+ ret := make ([][]Cell , k + 1 )
324+ for i := 0 ; i <= k ; i ++ {
325+ ret [i ] = make ([]Cell , 0 , ringSize (i ))
326+ }
327+
328+ for i , d := range outDists {
329+ ret [d ] = append (ret [d ], Cell (outHexes [i ]))
330+ }
331+
332+ return ret , nil
333+ }
334+
335+ // GridDiskDistancesUnsafe produces cells within grid distance k of the origin cell.
336+ // Output behavior is undefined when one of the cells returned by this
337+ // function is a pentagon or is in the pentagon distortion area.
338+ //
339+ // k-ring 0 is defined as the origin cell, k-ring 1 is defined as k-ring 0 and
340+ // all neighboring cells, and so on.
341+ //
342+ // Outer slice is ordered from origin outwards. Inner slices are in no
343+ // particular order. Elements of the output array may be left zero, as can
344+ // happen when crossing a pentagon.
345+ func (c Cell ) GridDiskDistancesUnsafe (k int ) ([][]Cell , error ) {
346+ return GridDiskDistancesUnsafe (c , k )
347+ }
348+
349+ // GridDiskDistancesSafe produces cells within grid distance k of the origin cell.
350+ // This is the safe, but slow version of GridDiskDistances.
351+ //
352+ // k-ring 0 is defined as the origin cell, k-ring 1 is defined as k-ring 0 and
353+ // all neighboring cells, and so on.
354+ //
355+ // Outer slice is ordered from origin outwards. Inner slices are in no
356+ // particular order. Elements of the output array may be left zero, as can
357+ // happen when crossing a pentagon.
358+ func GridDiskDistancesSafe (origin Cell , k int ) ([][]Cell , error ) {
359+ rsz := maxGridDiskSize (k )
360+ outHexes := make ([]C.H3Index , rsz )
361+ outDists := make ([]C.int , rsz )
362+
363+ if err := toErr (C .gridDiskDistancesSafe (C .H3Index (origin ), C .int (k ), & outHexes [0 ], & outDists [0 ])); err != nil {
364+ return nil , err
365+ }
366+
367+ ret := make ([][]Cell , k + 1 )
368+ for i := 0 ; i <= k ; i ++ {
369+ ret [i ] = make ([]Cell , 0 , ringSize (i ))
370+ }
371+
372+ for i , d := range outDists {
373+ ret [d ] = append (ret [d ], Cell (outHexes [i ]))
374+ }
375+
376+ return ret , nil
377+ }
378+
379+ // GridDiskDistancesSafe produces cells within grid distance k of the origin cell.
380+ // This is the safe, but slow version of GridDiskDistances.
381+ //
382+ // k-ring 0 is defined as the origin cell, k-ring 1 is defined as k-ring 0 and
383+ // all neighboring cells, and so on.
384+ //
385+ // Outer slice is ordered from origin outwards. Inner slices are in no
386+ // particular order. Elements of the output array may be left zero, as can
387+ // happen when crossing a pentagon.
388+ func (c Cell ) GridDiskDistancesSafe (k int ) ([][]Cell , error ) {
389+ return GridDiskDistancesSafe (c , k )
390+ }
391+
274392// GridRing produces the "hollow" ring of cells at exactly grid distance k from the origin cell.
275393//
276394// k-ring 0 returns just the origin hexagon.
0 commit comments