Skip to content

Commit

Permalink
feat(core): metadata and groups
Browse files Browse the repository at this point in the history
  • Loading branch information
AnzhiZhang committed Jul 17, 2024
1 parent 00448cb commit 0bb83ba
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/TypedMatrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ using LinearAlgebra

import Base: getindex, size

include("types.jl")
include("metadata.jl")

# all matrices
include("matrices/index.jl")

Expand Down
47 changes: 47 additions & 0 deletions src/matrices/index.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export
list_groups,
list_matrices

# include all matrices

# matrix groups
const MATRIX_GROUPS = Dict{Group,Set{Type{<:AbstractMatrix}}}()
MATRIX_GROUPS[Group(:builtin)] = Set([
])
MATRIX_GROUPS[Group(:user)] = Set([])

# group functions
list_groups() = collect(keys(MATRIX_GROUPS))
# function add_to_group(::Type{<:AbstractMatrix}, group::Symbol...=:user)
# function remove_from_all_groups(::Type{<:AbstractMatrix})
# function remove_from_group(::Type{<:AbstractMatrix}, group::Symbol)

# list matrices
list_matrices() = list_matrices(Group[], Property[])
list_matrices(properties::Property...) = list_matrices(Group[], collect(properties))
list_matrices(groups::Group...) = list_matrices(collect(groups), Property[])
function list_matrices(groups::Vector{Group}, props::Vector{Property})
# check properties
assert_properties_exists(props)

# groups
groups_results = union(values(MATRIX_GROUPS)...)
for group = groups
if group keys(MATRIX_GROUPS)
throw(ArgumentError("Group $group not exists"))
else
intersect!(groups_results, MATRIX_GROUPS[group])
end
end

# properties
results::Vector{Type{<:AbstractMatrix}} = []
for matrix = groups_results
if all(props .∈ Ref(properties(matrix)))
push!(results, matrix)
end
end

# return
return results
end
54 changes: 54 additions & 0 deletions src/metadata.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export
list_properties,
@properties,
properties

# property list
const PROPERTIES = [Property(i) for i in [
:symmetric,
:inverse,
:illcond,
:posdef,
:eigen,
:sparse,
:random,
:regprob,
:graph,
]]

# list properties
list_properties() = PROPERTIES

# assert properties exists
function assert_properties_exists(props::Vector{Property})
for prop in props
if prop PROPERTIES
throw(ArgumentError("Property $prop not exists"))
end
end
end

# register properties macro
macro properties(type::Symbol, ex::Expr)
return quote
register_properties($(esc(type)), $ex)
end
end

# register properties
function register_properties(T::Type, props::Vector{Property})
# check props
assert_properties_exists(props)

# register properties
@eval properties(::Type{$T}) = $props
end

# register properties alternative interfaces
register_properties(T::Type, props::Vector{Symbol}) = register_properties(T, [Property(prop) for prop in props])
register_properties(T::Type, props::Symbol...) = register_properties(T, collect(props))
register_properties(T::Type, props::Property...) = register_properties(T, collect(props))

# properties function interfaces
properties(::Type{<:AbstractMatrix})::Vector{Property} = []
properties(m::AbstractMatrix{}) = properties(typeof(m).name.wrapper)
11 changes: 11 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export
Property,
Group

struct Property
name::Symbol
end

struct Group
name::Symbol
end

0 comments on commit 0bb83ba

Please sign in to comment.