diff --git a/CHANGELOG.md b/CHANGELOG.md index a7998ec..b30580d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ This project adheres to [Semantic Versioning](http://semver.org/) and - a `RDF.Graph` builder DSL available under the `RDF.Graph.build/2` function - `RDF.Graph.new/2` and `RDF.Graph.add/2` support the addition of `RDF.Dataset`s +- `RDF.Description.empty?/1`, `RDF.Graph.empty?/1` and `RDF.Dataset.empty?/1` which + are significantly faster than `Enum.empty?/1` - new guards in `RDF.Guards`: `is_statement/1` and `is_quad/1` - `RDF.PropertyMap.terms/1` and `RDF.PropertyMap.iris/1` diff --git a/lib/rdf/dataset.ex b/lib/rdf/dataset.ex index 6ec6b70..54c5bee 100644 --- a/lib/rdf/dataset.ex +++ b/lib/rdf/dataset.ex @@ -656,6 +656,16 @@ defmodule RDF.Dataset do end) end + @doc """ + Returns if the given `dataset` is empty. + + Note: You should always prefer this over the use of `Enum.empty?/1` as it is significantly faster. + """ + @spec empty?(t) :: boolean + def empty?(%__MODULE__{} = dataset) do + Enum.empty?(dataset.graphs) or dataset |> graphs() |> Enum.all?(&Graph.empty?/1) + end + @doc """ Checks if the given `input` statements exist within `dataset`. diff --git a/lib/rdf/description.ex b/lib/rdf/description.ex index 724ead8..ab10ffc 100644 --- a/lib/rdf/description.ex +++ b/lib/rdf/description.ex @@ -637,6 +637,16 @@ defmodule RDF.Description do defdelegate count(description), to: __MODULE__, as: :statement_count + @doc """ + Returns if the given `description` is empty. + + Note: You should always prefer this over the use of `Enum.empty?/1` as it is significantly faster. + """ + @spec empty?(t) :: boolean + def empty?(%__MODULE__{} = description) do + Enum.empty?(description.predications) + end + @doc """ Checks if the given `input` statements exist within `description`. """ diff --git a/lib/rdf/graph.ex b/lib/rdf/graph.ex index 838b9f9..dea1f54 100644 --- a/lib/rdf/graph.ex +++ b/lib/rdf/graph.ex @@ -928,6 +928,16 @@ defmodule RDF.Graph do defdelegate statements(graph, opts \\ []), to: __MODULE__, as: :triples + @doc """ + Returns if the given `graph` is empty. + + Note: You should always prefer this over the use of `Enum.empty?/1` as it is significantly faster. + """ + @spec empty?(t) :: boolean + def empty?(%__MODULE__{} = graph) do + Enum.empty?(graph.descriptions) + end + @doc """ Checks if the given `input` statements exist within `graph`. """ diff --git a/test/unit/dataset_test.exs b/test/unit/dataset_test.exs index 0be6d33..ca39229 100644 --- a/test/unit/dataset_test.exs +++ b/test/unit/dataset_test.exs @@ -1621,6 +1621,12 @@ defmodule RDF.DatasetTest do assert Dataset.statement_count(Dataset.new(statement())) == 1 end + test "empty?/1" do + assert Dataset.empty?(dataset()) == true + assert Dataset.empty?(Dataset.new(statement())) == false + assert Dataset.empty?(Dataset.new(graph())) == true + end + describe "include?/3" do test "valid cases" do dataset = diff --git a/test/unit/description_test.exs b/test/unit/description_test.exs index cafa3b2..25cc90d 100644 --- a/test/unit/description_test.exs +++ b/test/unit/description_test.exs @@ -792,6 +792,11 @@ defmodule RDF.DescriptionTest do assert Description.statement_count(description({EX.p(), EX.O})) == 1 end + test "empty?/1" do + assert Description.empty?(Description.new(EX.S)) == true + assert Description.empty?(description({EX.p(), EX.O})) == false + end + describe "include?/3" do test "valid cases" do desc = diff --git a/test/unit/graph_test.exs b/test/unit/graph_test.exs index 246d92e..2675acc 100644 --- a/test/unit/graph_test.exs +++ b/test/unit/graph_test.exs @@ -1288,6 +1288,11 @@ defmodule RDF.GraphTest do assert Graph.statement_count(Graph.new(statement())) == 1 end + test "empty?/1" do + assert Graph.empty?(graph()) == true + assert Graph.empty?(Graph.new(statement())) == false + end + describe "include?/3" do test "valid cases" do graph =