| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
”; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
package build |
| 33 |
|
| 34 |
import ( |
| 35 |
"github.com/yourbasic/graph" |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
type Virtual struct { |
| 44 |
|
| 45 |
|
| 46 |
order int |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
h; |
| 51 |
|
| 52 |
|
| 53 |
); |
| 54 |
|
| 55 |
|
| 56 |
edge func(v, w int) bool |
| 57 |
cost func(v, w int) int64 |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
v. |
| 65 |
|
| 66 |
a in |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
0. |
| 71 |
|
| 72 |
degree func(v int) int |
| 73 |
visit func(v int, a int, do func(w int, c int64) (skip bool)) (aborted bool) |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
type FilterFunc func(v, w int) bool |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
type CostFunc func(v, w int) int64 |
| 83 |
|
| 84 |
|
| 85 |
func Cost(n int64) CostFunc { |
| 86 |
return func(int, int) int64 { return n } |
| 87 |
} |
| 88 |
|
| 89 |
func neverEdge(int, int) bool { return false } |
| 90 |
func alwaysEdge(v, w int) bool { return v != w } |
| 91 |
|
| 92 |
func zero(int, int) int64 { return 0 } |
| 93 |
|
| 94 |
func degreeZero(int) int { return 0 } |
| 95 |
func degreeOne(int) int { return 1 } |
| 96 |
|
| 97 |
func noNeighbors(int, int, func(w int, c int64) bool) bool { return false } |
| 98 |
|
| 99 |
const bitsPerWord = 32 << uint(^uint(0)>>63) |
| 100 |
|
| 101 |
func min(m, n int) int { |
| 102 |
if m > n { |
| 103 |
return n |
| 104 |
} |
| 105 |
return m |
| 106 |
} |
| 107 |
|
| 108 |
func max(m, n int) int { |
| 109 |
if m < n { |
| 110 |
return n |
| 111 |
} |
| 112 |
return m |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
var null = new(Virtual) |
| 117 |
|
| 118 |
|
| 119 |
func singleton() *Virtual { |
| 120 |
return &Virtual{ |
| 121 |
order: 1, |
| 122 |
edge: neverEdge, |
| 123 |
cost: zero, |
| 124 |
degree: degreeZero, |
| 125 |
visit: noNeighbors, |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
func edge() *Virtual { |
| 131 |
g := &Virtual{ |
| 132 |
order: 2, |
| 133 |
cost: zero, |
| 134 |
edge: alwaysEdge, |
| 135 |
degree: degreeOne, |
| 136 |
} |
| 137 |
g.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { |
| 138 |
w := 1 - v |
| 139 |
if w < a { |
| 140 |
return |
| 141 |
} |
| 142 |
return do(w, 0) |
| 143 |
} |
| 144 |
return g |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
func line(n int) *Virtual { |
| 149 |
switch { |
| 150 |
case n < 0: |
| 151 |
return nil |
| 152 |
case n == 0: |
| 153 |
return null |
| 154 |
case n == 1: |
| 155 |
return singleton() |
| 156 |
case n == 2: |
| 157 |
return edge() |
| 158 |
} |
| 159 |
g := generic0(n, func(v, w int) (edge bool) { |
| 160 |
switch v - w { |
| 161 |
case -1, 1: |
| 162 |
edge = true |
| 163 |
} |
| 164 |
return |
| 165 |
}) |
| 166 |
g.degree = func(v int) int { |
| 167 |
switch v { |
| 168 |
case 0, n - 1: |
| 169 |
return 1 |
| 170 |
default: |
| 171 |
return 2 |
| 172 |
} |
| 173 |
} |
| 174 |
g.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { |
| 175 |
if w := v - 1; w >= a && do(w, 0) { |
| 176 |
return true |
| 177 |
} |
| 178 |
if w := v + 1; w >= a && w < n && do(w, 0) { |
| 179 |
return true |
| 180 |
} |
| 181 |
return |
| 182 |
} |
| 183 |
return g |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
func generic(n int, cost CostFunc, edge func(v, w int) bool) *Virtual { |
| 188 |
switch { |
| 189 |
case n < 0: |
| 190 |
return nil |
| 191 |
case n == 0: |
| 192 |
return null |
| 193 |
case n == 1: |
| 194 |
return singleton() |
| 195 |
} |
| 196 |
g := &Virtual{ |
| 197 |
order: n, |
| 198 |
edge: func(v, w int) bool { return v != w && edge(v, w) }, |
| 199 |
cost: cost, |
| 200 |
} |
| 201 |
g.degree = func(v int) (deg int) { |
| 202 |
g.visit(v, 0, func(int, int64) (skip bool) { |
| 203 |
deg++ |
| 204 |
return |
| 205 |
}) |
| 206 |
return |
| 207 |
} |
| 208 |
g.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { |
| 209 |
for w := a; w < n; w++ { |
| 210 |
if g.edge(v, w) && do(w, cost(v, w)) { |
| 211 |
return true |
| 212 |
} |
| 213 |
} |
| 214 |
return |
| 215 |
} |
| 216 |
return g |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
func generic0(n int, edge func(v, w int) bool) *Virtual { |
| 221 |
switch { |
| 222 |
case n < 0: |
| 223 |
return nil |
| 224 |
case n == 0: |
| 225 |
return null |
| 226 |
case n == 1: |
| 227 |
return singleton() |
| 228 |
} |
| 229 |
g := &Virtual{ |
| 230 |
order: n, |
| 231 |
edge: func(v, w int) bool { return v != w && edge(v, w) }, |
| 232 |
cost: zero, |
| 233 |
} |
| 234 |
g.degree = func(v int) (deg int) { |
| 235 |
g.visit(v, 0, func(int, int64) (skip bool) { |
| 236 |
deg++ |
| 237 |
return |
| 238 |
}) |
| 239 |
return |
| 240 |
} |
| 241 |
g.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { |
| 242 |
for w := a; w < n; w++ { |
| 243 |
if g.edge(v, w) && do(w, 0) { |
| 244 |
return true |
| 245 |
} |
| 246 |
} |
| 247 |
return |
| 248 |
} |
| 249 |
return g |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
e. |
| 254 |
func Generic(n int, edge FilterFunc) *Virtual { |
| 255 |
switch { |
| 256 |
case n < 0: |
| 257 |
return nil |
| 258 |
case n == 0: |
| 259 |
return null |
| 260 |
case n == 1: |
| 261 |
return singleton() |
| 262 |
case edge == nil: |
| 263 |
return Kn(n) |
| 264 |
} |
| 265 |
return generic0(n, edge) |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
func Specific(g graph.Iterator) *Virtual { |
| 273 |
h := graph.Sort(g) |
| 274 |
stats := graph.Check(h) |
| 275 |
if stats.Multi != 0 || stats.Loops != 0 { |
| 276 |
panic("Virtual doesn't support multiple edges or self-loops") |
| 277 |
} |
| 278 |
res := &Virtual{ |
| 279 |
order: h.Order(), |
| 280 |
edge: h.Edge, |
| 281 |
visit: h.VisitFrom, |
| 282 |
degree: h.Degree, |
| 283 |
} |
| 284 |
if stats.Weighted == 0 { |
| 285 |
res.cost = zero |
| 286 |
return res |
| 287 |
} |
| 288 |
res.cost = func(v, w int) (cost int64) { |
| 289 |
if !h.Edge(v, w) { |
| 290 |
return 0 |
| 291 |
} |
| 292 |
h.VisitFrom(v, w, func(w int, c int64) (skip bool) { |
| 293 |
cost = c |
| 294 |
return true |
| 295 |
}) |
| 296 |
return |
| 297 |
} |
| 298 |
return res |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
func Empty(n int) *Virtual { |
| 303 |
switch { |
| 304 |
case n < 0: |
| 305 |
return nil |
| 306 |
case n == 0: |
| 307 |
return null |
| 308 |
case n == 1: |
| 309 |
return singleton() |
| 310 |
} |
| 311 |
return &Virtual{ |
| 312 |
order: n, |
| 313 |
edge: neverEdge, |
| 314 |
cost: zero, |
| 315 |
degree: degreeZero, |
| 316 |
visit: noNeighbors, |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
func Kn(n int) *Virtual { |
| 322 |
switch { |
| 323 |
case n < 0: |
| 324 |
return nil |
| 325 |
case n == 0: |
| 326 |
return null |
| 327 |
case n == 1: |
| 328 |
return singleton() |
| 329 |
} |
| 330 |
g := &Virtual{ |
| 331 |
order: n, |
| 332 |
edge: alwaysEdge, |
| 333 |
cost: zero, |
| 334 |
degree: func(v int) int { return n - 1 }, |
| 335 |
} |
| 336 |
g.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { |
| 337 |
for w := a; w < g.order; w++ { |
| 338 |
if v != w && do(w, 0) { |
| 339 |
return true |
| 340 |
} |
| 341 |
} |
| 342 |
return |
| 343 |
} |
| 344 |
return g |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
func (g *Virtual) Complement() *Virtual { |
| 352 |
n := g.order |
| 353 |
switch n { |
| 354 |
case 0: |
| 355 |
return null |
| 356 |
case 1: |
| 357 |
return singleton() |
| 358 |
} |
| 359 |
res := generic0(n, func(v, w int) (edge bool) { |
| 360 |
return v != w && !g.edge(v, w) |
| 361 |
}) |
| 362 |
res.degree = func(v int) int { return n - 1 - g.degree(v) } |
| 363 |
res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { |
| 364 |
prev := a |
| 365 |
if g.visit(v, a, func(w0 int, _ int64) (skip bool) { |
| 366 |
for w := prev; w < w0; w++ { |
| 367 |
if v != w && do(w, 0) { |
| 368 |
return true |
| 369 |
} |
| 370 |
} |
| 371 |
prev = w0 + 1 |
| 372 |
return |
| 373 |
}) { |
| 374 |
return true |
| 375 |
} |
| 376 |
for w := prev; w < n; w++ { |
| 377 |
if v != w && do(w, 0) { |
| 378 |
return true |
| 379 |
} |
| 380 |
} |
| 381 |
return |
| 382 |
} |
| 383 |
return res |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
func (g *Virtual) Keep(edge FilterFunc) *Virtual { |
| 388 |
n := g.order |
| 389 |
switch { |
| 390 |
case n == 0: |
| 391 |
return null |
| 392 |
case n == 1: |
| 393 |
return singleton() |
| 394 |
case edge == nil: |
| 395 |
return g |
| 396 |
} |
| 397 |
res := generic(g.order, g.cost, func(v, w int) bool { |
| 398 |
return edge(v, w) && g.edge(v, w) |
| 399 |
}) |
| 400 |
res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { |
| 401 |
return g.visit(v, a, func(w int, c int64) bool { |
| 402 |
return edge(v, w) && do(w, c) |
| 403 |
}) |
| 404 |
} |
| 405 |
return res |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
func (g *Virtual) AddCost(c int64) *Virtual { |
| 410 |
res := *g |
| 411 |
res.cost = Cost(c) |
| 412 |
res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { |
| 413 |
return g.visit(v, a, func(w int, _ int64) bool { |
| 414 |
return do(w, c) |
| 415 |
}) |
| 416 |
} |
| 417 |
return &res |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
func (g *Virtual) AddCostFunc(c CostFunc) *Virtual { |
| 422 |
if c == nil { |
| 423 |
h := g.AddCost(0) |
| 424 |
return h |
| 425 |
} |
| 426 |
res := *g |
| 427 |
res.cost = c |
| 428 |
res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) { |
| 429 |
return g.visit(v, a, func(w int, _ int64) bool { |
| 430 |
return do(w, c(v, w)) |
| 431 |
}) |
| 432 |
} |
| 433 |
return &res |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
func (g *Virtual) Order() int { |
| 438 |
return g.order |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
func (g *Virtual) Degree(v int) int { |
| 443 |
if v < 0 || v >= g.order { |
| 444 |
panic("vertex out of range") |
| 445 |
} |
| 446 |
return g.degree(v) |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
func (g *Virtual) Edge(v, w int) bool { |
| 451 |
if v < 0 || v >= g.order || w < 0 || w >= g.order { |
| 452 |
return false |
| 453 |
} |
| 454 |
return g.edge(v, w) |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
func (g *Virtual) Cost(v, w int) int64 { |
| 459 |
if v < 0 || v >= g.order || w < 0 || w >= g.order { |
| 460 |
return 0 |
| 461 |
} |
| 462 |
if g.edge(v, w) { |
| 463 |
return g.cost(v, w) |
| 464 |
} |
| 465 |
return 0 |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
func (g *Virtual) Visit(v int, do func(w int, c int64) bool) bool { |
| 474 |
if v < 0 || v >= g.order { |
| 475 |
panic("vertex out of range") |
| 476 |
} |
| 477 |
return g.visit(v, 0, do) |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
w. |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
func (g *Virtual) VisitFrom(v int, a int, do func(w int, c int64) bool) bool { |
| 486 |
n := g.order |
| 487 |
switch { |
| 488 |
case v < 0 || v >= n: |
| 489 |
panic("vertex out of range") |
| 490 |
case a < 0: |
| 491 |
a = 0 |
| 492 |
case a > n: |
| 493 |
a = n |
| 494 |
} |
| 495 |
return g.visit(v, a, do) |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
func (g *Virtual) String() string { |
| 500 |
return graph.String(g) |
| 501 |
} |