Skip to content

Commit

Permalink
enumerations: Split constructor in 2 for ordered/unordered enumerations
Browse files Browse the repository at this point in the history
  • Loading branch information
Spyros Anastasopoulos committed Sep 14, 2023
1 parent 2acbbe8 commit 09ecf75
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
14 changes: 12 additions & 2 deletions enumeration.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,18 @@ func enumerationTypeToTileDB[T EnumerationType]() Datatype {
}
}

// NewEnumeration creates an enumeration with name and ordered or not values.
func NewEnumeration[T EnumerationType](tdbCtx *Context, name string, ordered bool, values []T) (*Enumeration, error) {
// NewOrderedEnumeration creates an ordered enumeration with name and values.
func NewOrderedEnumeration[T EnumerationType](tdbCtx *Context, name string, values []T) (*Enumeration, error) {
return newEnumeration[T](tdbCtx, name, true, values)
}

// NewOrderedEnumeration creates an unordered enumeration with name and values.
func NewUnorderedEnumeration[T EnumerationType](tdbCtx *Context, name string, values []T) (*Enumeration, error) {
return newEnumeration[T](tdbCtx, name, false, values)
}

// newEnumeration creates an enumeration with name and ordered or not values.
func newEnumeration[T EnumerationType](tdbCtx *Context, name string, ordered bool, values []T) (*Enumeration, error) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))

Expand Down
10 changes: 5 additions & 5 deletions enumeration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ func TestEnumeration(t *testing.T) {
tdbCtx, err := NewContext(config)
require.NoError(t, err)

romanNumerals, err := NewEnumeration(tdbCtx, "romanNumerals", true,
romanNumerals, err := NewOrderedEnumeration(tdbCtx, "romanNumerals",
[]string{"i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", "xi", "xii", "xiii", "xiv", "xv", "xvi"})
require.NoError(t, err)

powersOfTwo, err := NewEnumeration(tdbCtx, "powersOfTwo", true, []uint32{1, 2, 4, 8, 16, 32, 64, 128, 256})
powersOfTwo, err := NewOrderedEnumeration(tdbCtx, "powersOfTwo", []uint32{1, 2, 4, 8, 16, 32, 64, 128, 256})
require.NoError(t, err)

truth, err := NewEnumeration(tdbCtx, "truth", false, []bool{false, true})
truth, err := NewUnorderedEnumeration(tdbCtx, "truth", []bool{false, true})
require.NoError(t, err)

t.Run("Name", func(t *testing.T) {
Expand Down Expand Up @@ -272,11 +272,11 @@ func arraySchemaWithEnumerations(t *testing.T) *ArraySchema {
require.NoError(t, domain.AddDimensions(dimRows, dimCols))
require.NoError(t, schema.SetDomain(domain))

greekNumerals, err := NewEnumeration(tdbCtx, "greekNumerals", true,
greekNumerals, err := NewOrderedEnumeration(tdbCtx, "greekNumerals",
[]string{"α", "β", "γ", "δ", "ε", "στ", "ζ", "η", "θ", "ι", "ια", "ιβ", "ιγ", "ιδ", "ιε", "ιστ"})
require.NoError(t, err)
require.NoError(t, schema.AddEnumeration(greekNumerals))
romanNumerals, err := NewEnumeration(tdbCtx, "romanNumerals", true,
romanNumerals, err := NewOrderedEnumeration(tdbCtx, "romanNumerals",
[]string{"i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", "xi", "xii", "xiii", "xiv", "xv", "xvi"})
require.NoError(t, err)
require.NoError(t, schema.AddEnumeration(romanNumerals))
Expand Down

0 comments on commit 09ecf75

Please sign in to comment.