Created
December 15, 2011 21:28
-
-
Save PhDP/1482974 to your computer and use it in GitHub Desktop.
A species object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"os" | |
) | |
type Species struct { | |
Name string // Name of the species (dah!) | |
IsProducer bool // True if the species is a primary producer | |
Subpops uint // Number of subpopulations (the order of the spatial network). | |
N, C, R float64 // Niche value. | |
Group []uint // The group for each subpopulations | |
} | |
func (s *Species) ToXML(w io.Writer) { | |
fmt.Fprintf(w, "<species>\n") | |
fmt.Fprintf(w, " <name>%s</name>\n", s.Name) | |
fmt.Fprintf(w, " <producer>%t</producer>\n", s.IsProducer) | |
fmt.Fprintf(w, " <subpops>%d</subpops>\n", s.Subpops) | |
fmt.Fprintf(w, " <n>%.4f</n>\n", s.N) | |
fmt.Fprintf(w, " <c>%.4f</c>\n", s.C) | |
fmt.Fprintf(w, " <r>%.4f</r>\n", s.R) | |
fmt.Fprintf(w, "</species>\n") | |
} | |
func (s *Species) Pops() bool { | |
sum := 0 | |
for i := 0; i < s.Subpops; i++ { | |
if s.Group[i] > 0 { | |
sum++ | |
} | |
} | |
return sum | |
} | |
func (s *Species) IsExtinct() bool { | |
for i := 0; i < s.Subpops; i++ { | |
if s.Group[i] > 0 { | |
return false | |
} | |
} | |
return true | |
} | |
func (s *Species) IsExtant() bool { | |
for i := 0; i < s.Subpops; i++ { | |
if s.Group[i] > 0 { | |
return true | |
} | |
} | |
return false | |
} | |
func main() { | |
s := new(Species) | |
s.Name = "竜" | |
s.IsProducer = false // Let's be serious for a moment: it's a dragon! | |
s.Subpops = 5 | |
s.N = 0.97648 | |
s.C = 0.829111 | |
s.R = 0.32 | |
s.ToXML(os.Stdout) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment