use bit_array instead of []bool
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package main
|
||||
|
||||
import "core:container/bit_array"
|
||||
import "core:fmt"
|
||||
import "core:math"
|
||||
import "core:os"
|
||||
@@ -28,7 +29,7 @@ read_data :: proc(file: string) -> (res: [NUMBER_OF_ITEMS]Item, ok := true) {
|
||||
return
|
||||
}
|
||||
|
||||
Chromosome :: [NUMBER_OF_ITEMS]bool
|
||||
Chromosome :: bit_array.Bit_Array
|
||||
|
||||
distance :: proc(x, y: int) -> int {
|
||||
return math.abs(x - y)
|
||||
@@ -39,10 +40,10 @@ penalize :: proc(d: int) -> int {
|
||||
}
|
||||
|
||||
// side-effect: reads global `items`
|
||||
fitness :: proc(chrom: Chromosome) -> int {
|
||||
fitness :: proc(chrom: ^Chromosome) -> int {
|
||||
tot_profit, tot_weight := 0, 0
|
||||
for bit, idx in chrom {
|
||||
if !bit {continue}
|
||||
for idx in 0 ..< bit_array.len(chrom) {
|
||||
if !bit_array.get(chrom, idx) {continue} // nb: eliminating branch may be faster
|
||||
tot_profit += items[idx].profit
|
||||
tot_weight += items[idx].weight
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user