Commit 34ecc3a

Richard Luby <richluby@gmail.com>
2016-12-08 09:42:47
updated tag structure to be more logical
1 parent 20e71a6
Changed files (1)
structures.go
@@ -19,23 +19,15 @@ var usedRecordsArray []Record
 // mapLock locks the maps to prevent concurrency issues
 var mapLock *sync.Mutex
 
-// stores information for all categories
-var (
-	categoricalData CategoricalData
-)
-
 const CATEGORY_SEPARATOR = ":"
 
-type CategoricalData struct {
-	// categoryPaths stores the path of a category from the
-	// categories array. These indices must match exactly.
-	// Implmentation based on closure table from
-	// http://www.slideshare.net/billkarwin/models-for-hierarchical-data
-	// The path is stored using CATEGORY_SEPARATOR as the delimiter
-	// in the string.
-	CategoryPath []string
-	Category     []Category
-}
+// categoryPaths stores the path of a category from the
+// categories array. These indices must match exactly.
+// Implmentation based on closure table from
+// http://www.slideshare.net/billkarwin/models-for-hierarchical-data
+// The path is stored using CATEGORY_SEPARATOR as the delimiter
+// in the string.
+var categories []Category
 
 // define colors for printing to terminals
 const (
@@ -102,8 +94,9 @@ type Category struct {
 	// 'categoricalData' of the children. Similar to a pointer,
 	// but can be sent across the network.
 
-	Parent, // the index in the array of 'categoricalData'
-	PathIndex int // index to categoryPath array
+	Parent int // the index in the array of 'categoricalData'
+	// a negative value indicates no parent
+	fullCategoryPath,
 	Value string // leaf portion of path
 }
 
@@ -111,10 +104,19 @@ type Category struct {
 // category, or the empty string if the PathIndex
 // variable is too large
 func (cat Category) String() string {
-	if len(categoricalData.CategoryPath) < cat.PathIndex {
-		return ""
+	return cat.Value
+}
+
+// uses lazy evaluation and short circuting
+// to generate a fully qualified tag path for this category
+func (cat Category) FullCategoryPath() string {
+	if cat.fullCategoryPath != "" {
+		return cat.fullCategoryPath
+	}
+	if cat.Parent < 0 {
+		return cat.Value
 	}
-	return categoricalData.CategoryPath[cat.PathIndex]
+	return categories[cat.Parent].FullCategoryPath() + CATEGORY_SEPARATOR + cat.Value
 }
 
 // P incrememnts the counter here
@@ -155,6 +157,7 @@ func (cSem *CountingSemaphore) SetCapacity(cap int) {
 func init() {
 	recordsArray = []Record{}
 	usedRecordsArray = []Record{}
-	categoricalData = CategoricalData{}
+	categories = []Category{Category{Value: "Cyber",
+		Parent: -1}}
 	mapLock = &sync.Mutex{}
 }