collections

package module
v2.0.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2022 License: MIT Imports: 18 Imported by: 1

README

collections

A Go implementation of List (Key/Value and Indexed), and Table (Rows/Columns)

The collection package implements a key/value List and a rows/columns Table.

List
  • Access elements via Map or Indexed Array.
  • Elements can be of any type (multiple types in the same List).
  • Fast search (a b-tree like search via thread workers).
  • Sort in both directions (asc and desc).
  • Includes KeyExists(), ValueExists() methods to avoid duplicates.
  • Remove and Insert by key/value or array index.
Example
var coll = collections.NewCollection()
tx := time.Now()
for i := 0; i < 1000000; i++ {
   k := fmt.Sprintf("%d", i)

// Add different types
   var v interface{}

   if i%2 == 0 {
      v = fmt.Sprintf("%d Green Dolphin Street", i)
   } else {
      v = i + 1250
   }
   coll.List.Add(k, v)
}
txd := time.Since(tx)
fmt.Println("took:", txd, `to add 1,000,000 elements to the list.`)
fmt.Println(`searching for "Green Dolphin Street 694823"`)

tx = time.Now()
fmt.Println("IndexOf:", coll.List.IndexOf("694823"))
txd = time.Since(tx)
fmt.Println("took:", txd, `to find "Dolphin Street 694823" in the list.`)
Output:
took: 313.756394ms to add 1,000,000 elements to the list.
searching for "Green Dolphin Street 694823"
IndexOf: 694823
took: 4.335141ms to find "Dolphin Street 694823" in the list.

// Iterate thru the list.
for x := 0; x < 10; x++ {
	l, _ := coll.List.GetItem(x)
	fmt.Println("key:", l.Key, "value:", l.Value)
}
Table

Table is a classic representation of a data-table with rows and columns.

  • Access rows via Map or Indexed Array.
  • Add a tag for selected rows.
Example
var coll = collections.NewCollection()
tbl := coll.Table.Create("Test")
tbl.Columns.Add("State")
tbl.Columns.Add("Capital")

oneRow := tbl.Rows.Add()
oneRow["State"] = "Maine"
oneRow["Capital"] = "Augusta"

oneRow = tbl.Rows.Add()
oneRow["State"] = "Oregon"
oneRow["Capital"] = "Salem"

oneRow = tbl.Rows.Add()
oneRow["State"] = "Georgia"
oneRow["Capital"] = "Atlanta"

cols := tbl.Columns.Get()
rows := tbl.Rows.GetMap()

fmt.Print(strings.Repeat(" ", 4))
for j := 0; j < len(cols); j++ {
   fmt.Print(cols[j].Name, strings.Repeat(" ", 7))
}
fmt.Print("\n")
fmt.Println("---------------------------")

for i := 0; i < len(rows); i++ {
   for j := 0; j < len(cols); j++ {
      fmt.Print(strings.Repeat(" ", 4), rows[i][cols[j].Name], strings.Repeat(" ", 4))
   }
   fmt.Print("\n")
}

Output:
State Capital 
---------------------------
Maine Augusta 
Oregon Salem 
Georgia Atlanta 

// Row examples:

// Get a row
tbl.Rows.GetRows()[0][10] // row, col indexes

// Get a row
tbl.Rows.GetMap()[0]       // row index, map

// Get a single column in a row
tbl.Rows.GetMap()[0]["my-column-name"] // row index, map-string-value

Documentation

Overview

(c) Kamiar Bahri

(c) Kamiar Bahri

(c) Kamiar Bahri

(c) Kamiar Bahri

(c) Kamiar Bahri

(c) Kamiar Bahri

(c) Kamiar Bahri

(c) Kamiar Bahri

(c) Kamiar Bahri

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decrypt

func Decrypt(data []byte, passphrase string) ([]byte, error)

Decrypt decryptes an array of bytes using the AES algorythm.

func DecryptFile

func DecryptFile(p string, pwdPhrase string) error

DecryptFile decryptes a file using the AES algorythm.

func Encrypt

func Encrypt(plainData []byte, passphrase string) ([]byte, error)

Encrypt encryptes an array of bytes using the AES algorythm.

func EncryptFile

func EncryptFile(p string, pwdPhrase string) error

Encrypt encryptes a file using the AES algorythm.

Types

type Collection

type Collection struct {
	List    listInterface
	Table   ITable
	Dataset IDataset
}

Collection defines the List and Table interfaces.

func NewCollection

func NewCollection() *Collection

NewCollection makes a new instance of collections. It includes List, and Table (Rows/Columns)

type Cols

type Cols struct {
	Columns []Column
	Rows    IRows
	// contains filtered or unexported fields
}

Columns is the IColumn interface handler.

func (*Cols) Add

func (c *Cols) Add(name string) *Column

func (*Cols) Clear

func (c *Cols) Clear()

func (*Cols) ColDataCount

func (c *Cols) ColDataCount(colName string) int

func (*Cols) ColDataNoNULL

func (c *Cols) ColDataNoNULL(colName string) int

func (*Cols) Count

func (c *Cols) Count() int

func (*Cols) Exists

func (c *Cols) Exists(colName string) bool

func (*Cols) Get

func (c *Cols) Get() []Column

func (*Cols) GetData

func (c *Cols) GetData(colName string) []interface{}

func (*Cols) GetDataDistinct

func (c *Cols) GetDataDistinct(colName string) ([]interface{}, []interface{})

func (*Cols) GetOccurrence

func (c *Cols) GetOccurrence(colName string, value interface{}, data []interface{}) int

func (*Cols) GetOccurrenceMatrix

func (c *Cols) GetOccurrenceMatrix(colName string, tbl *Table)

func (*Cols) InsertAt

func (c *Cols) InsertAt(pos int, col Column) error

func (*Cols) ResetColTypes

func (c *Cols) ResetColTypes()

func (*Cols) SetColumns

func (c *Cols) SetColumns(colArry []Column)

type Column

type Column struct {
	// Tag is used to identify a row. All columns are assinged
	// the same tag to identify one row.
	Tag string `json:"tag"`

	Name string `json:"name"`
	Type string `json:"type"`
}

type Dataset

type Dataset struct {
	Tables []Table
}

Dataset is the handler for the IDatasetHndlr interface.

func (*Dataset) Add

func (d *Dataset) Add(tbl Table) error

func (*Dataset) Deserialize

func (d *Dataset) Deserialize(data []byte) ([]Table, error)

func (*Dataset) DeserializeFromFile

func (d *Dataset) DeserializeFromFile(fPath string) ([]Table, error)

func (*Dataset) Remove

func (d *Dataset) Remove(i int) error

func (*Dataset) RemoveByName

func (d *Dataset) RemoveByName(tblName string) error

func (*Dataset) Serialize

func (d *Dataset) Serialize() ([]byte, error)

func (*Dataset) SerializeToFile

func (d *Dataset) SerializeToFile(fPath string) error

func (*Dataset) TableExists

func (d *Dataset) TableExists(tbl *Table) bool

type Element

type Element struct {
	Key   string      `json:"key"`
	Value interface{} `json:"value"`
}

Element is a key/value structure that holds an item in the list.

type IColumn

type IColumn interface {
	Add(name string) *Column
	Get() []Column
	SetColumns(colArry []Column)

	// Clear drops all columns
	Clear()

	Exists(colName string) bool
	Count() int
	InsertAt(pos int, col Column) error

	// GetOccurrence gets a count of number-of-times a values is
	// repeated in a column's data.
	GetOccurrence(colName string, value interface{}, data []interface{}) int

	// GetOccurrenceMatrix reports the number of ccurrence
	// for each value in a column.
	//GetOccurrenceMatrix(colName string) []OccurrenceMatrix
	GetOccurrenceMatrix(colName string, tbl *Table)

	// ResetColTypes re-examins the values in columns to
	// esure that the correct type is set; i.e. when loadng
	// CSV all columns' type may be set to string (e.g
	// "433" vs 433).
	ResetColTypes()

	// GetData retrieves all values of a column.
	GetData(colName string) []interface{}
	ColDataCount(colName string) int
	ColDataNoNULL(colName string) int

	// GetDataDistinct gets all distinct values of a column.
	GetDataDistinct(colName string) ([]interface{}, []interface{})
	// contains filtered or unexported methods
}

IColumn is the column interface.

type IDataset

type IDataset interface {
	Add(tbl Table) error
	Remove(i int) error
	RemoveByName(tblName string) error
	Serialize() ([]byte, error)
	SerializeToFile(fPath string) error
	Deserialize(data []byte) ([]Table, error)
	DeserializeFromFile(fPath string) ([]Table, error)
}

IDataset is the Dataset interface (describes the Dataset interface).

type IRows

type IRows interface {

	// New creates an empty row and returns its map.
	New() Row

	// Add adds a row the Rows array.
	Add(row Row)

	SetColumns(cols []Column)

	// GetColumns returs a list of []Column, The first element is a built-in
	// column called _rowid; it holds the index position of the row at the
	// time it was created.
	GetColumns() []Column
	Count() int

	// GetJSON returns a json representation of the entire table.
	GetJSON() string

	GetRows() []Row
	GetRow(rowIndex int) Row
	GetLastRow() Row

	GetRowIndex(row Row) int
	GetLastRowIndex() int
	UpdateRow(irow Row) error

	GetRowJSON(i int) string

	GetRowsByTagName(tagName string) []Row

	SetTag(rowIndex int, tag Tag)
	GetTag(rowIndex int) Tag

	AddSharedData(sharedDataItem SharedDataItem) error
	GetSharedData(tagName string) SharedDataItem

	// InsertRecords creates new rows from a two demintional array of string.
	// Example of input is a result-set from reading CSV file.
	InsertRecords(input [][]string, verbose bool)

	// InsertSingleRecord creates a new row from an array of string.
	InsertSingleRecord(input []string)

	// Clear drops all rows.
	Clear()
}

rowInterface defines the methods for Row operations.

type ITable

type ITable interface {
	Create(name string) (*Table, error)

	// GetJSON gets a json string of the entire table.
	GetJSON(tbl *Table) string

	// Serialize create a []byte representation of the entire
	// table, which can written to disk.
	Serialize(tbl *Table) ([]byte, error)

	// Deserialize transforms []byte to *Table.
	Deserialize(data []byte) (*Table, error)

	SerializeToFile(tbl *Table, fPath string) error
	DeserializeFromFile(fPath string) (*Table, error)
}

ITable is the table interface.

type OccurrenceMatrix

type OccurrenceMatrix struct {
	Value    interface{} `json:"value"`
	Occurred int         `json:"occurred"`
}

type Row

type Row map[string]interface{}

Row is a map of columns; map[<column name><any value>

type RowHash

type RowHash struct {
	MD5   string
	RowID int
}

RowHash identifies a row by an MD5 value of its columns.

type Rows

type Rows struct {
	Rows    []Row
	Columns []Column
	Tags    []Tag

	// TODO:
	RowHashes []RowHash

	SharedData []SharedDataItem
}

Rows defines fields that comprise one Row; and it acts as a bridge between the caller and its interface.

func (*Rows) Add

func (r *Rows) Add(row Row)

func (*Rows) AddSharedData

func (r *Rows) AddSharedData(sharedDataItem SharedDataItem) error

func (*Rows) Clear

func (r *Rows) Clear()

func (*Rows) Count

func (r *Rows) Count() int

func (*Rows) GetColumns

func (r *Rows) GetColumns() []Column

func (*Rows) GetJSON

func (r *Rows) GetJSON() string

func (*Rows) GetLastRow

func (r *Rows) GetLastRow() Row

func (*Rows) GetLastRowIndex

func (r *Rows) GetLastRowIndex() int

func (*Rows) GetRow

func (r *Rows) GetRow(indx int) Row

func (*Rows) GetRowIndex

func (r *Rows) GetRowIndex(row Row) int

func (*Rows) GetRowJSON

func (r *Rows) GetRowJSON(inx int) string

func (*Rows) GetRows

func (r *Rows) GetRows() []Row

func (*Rows) GetRowsByTagName

func (r *Rows) GetRowsByTagName(tagName string) []Row

func (*Rows) GetSharedData

func (r *Rows) GetSharedData(tagName string) SharedDataItem

func (*Rows) GetTag

func (r *Rows) GetTag(i int) Tag

func (*Rows) InsertRecords

func (r *Rows) InsertRecords(input [][]string, verbose bool)

InsertRecords reads a two-dim. string arrary into the Table. Note: there is perfomance hit when verbose is on

func (*Rows) InsertSingleRecord

func (r *Rows) InsertSingleRecord(input []string)

func (*Rows) New

func (r *Rows) New() Row

func (*Rows) SetColumns

func (r *Rows) SetColumns(cols []Column)

func (*Rows) SetTag

func (r *Rows) SetTag(i int, tag Tag)

func (*Rows) UpdateRow

func (r *Rows) UpdateRow(row Row) error

type SharedDataItem

type SharedDataItem struct {
	TagName string
	Data    interface{}
}

SharedDataItem is a data is linked to rows by tag name.

type SortOrder

type SortOrder int
const (
	Asc SortOrder = iota
	Desc
)

func (SortOrder) String

func (i SortOrder) String() string

type Table

type Table struct {
	Name string
	Cols IColumn
	Rows IRows
}

Table holds the structure for the ITable interface.

func (*Table) Create

func (t *Table) Create(name string) (*Table, error)

Create initializes an empty table.

func (*Table) Deserialize

func (t *Table) Deserialize(b []byte) (*Table, error)

func (*Table) DeserializeFromFile

func (t *Table) DeserializeFromFile(fPath string) (*Table, error)

func (*Table) GetJSON

func (t *Table) GetJSON(tbl *Table) string

func (*Table) Serialize

func (t *Table) Serialize(tbl *Table) ([]byte, error)

Serialze turns a data-table into bytes of gob.

func (*Table) SerializeToFile

func (t *Table) SerializeToFile(tbl *Table, fPath string) error

type Tag

type Tag struct {
	Name string      // this is not unique; it helps to group related rows
	Data interface{} //optional user-data
}

Tag provides additional info that can be added to a row. Each has an associated tag, which can be empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL