Skip to content

Commit

Permalink
HN-237 basic new structure and insertion at root
Browse files Browse the repository at this point in the history
  • Loading branch information
lubiepiwo committed Sep 19, 2019
0 parents commit 6e529b2
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
elixir_rtree-*.tar

# Ignore IntelliJ
/.idea/

21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# ElixirRtree

**TODO: Add description**

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `elixir_rtree` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:elixir_rtree, "~> 0.1.0"}
]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/elixir_rtree](https://hexdocs.pm/elixir_rtree).

19 changes: 19 additions & 0 deletions elixir_rtree.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="ELIXIR_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/_build/dev/lib/elixir_rtree/ebin" />
<output-test url="file://$MODULE_DIR$/_build/test/lib/elixir_rtree/ebin" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/lib" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/assets/node_modules/phoenix" />
<excludeFolder url="file://$MODULE_DIR$/assets/node_modules/phoenix_html" />
<excludeFolder url="file://$MODULE_DIR$/cover" />
<excludeFolder url="file://$MODULE_DIR$/deps" />
<excludeFolder url="file://$MODULE_DIR$/doc" />
<excludeFolder url="file://$MODULE_DIR$/logs" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
47 changes: 47 additions & 0 deletions lib/elixir_rtree.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule ElixirRtree do


def new(width \\ 6)do
w = [{:min_width,:math.floor(width) / 2.0},{:max_width,width}]
ets = :ets.new(:rtree,[:set])

tree = %{
:metadata => %{params: w, ets_table: ets},
'root' => []
}

:ets.insert(ets,{:metadata,Map.get(tree,:metadata)})
:ets.insert(ets,{'root',
{Map.get(tree,'root'),[{:x,{0,0}},{:y,{0,0}}]}
})
tree
end

def insert(tree,{box,id} = leaf)do
meta = tree[:metadata]
params = meta[:params]

recursive_insertion(%{
tree: tree,
max_width: params[:max_width],
min_width: params[:min_width],
ets: meta[:ets_table]
},'root',id)

end

def recursive_insertion(rbundle,key,leaf)do

childs = rbundle.tree |> Map.get(key)
available_space = rbundle.max_width - length(childs)
tree_update = case available_space do
x when x > 0 -> rbundle.tree |> Map.update!(key,fn ch -> [leaf.id] ++ ch end)
_ -> IO.puts "Merge"
end

tree_update
end



end
28 changes: 28 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule ElixirRtree.MixProject do
use Mix.Project

def project do
[
app: :elixir_rtree,
version: "0.1.0",
elixir: "~> 1.9",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end
8 changes: 8 additions & 0 deletions test/elixir_rtree_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule ElixirRtreeTest do
use ExUnit.Case
doctest ElixirRtree

test "greets the world" do
assert ElixirRtree.hello() == :world
end
end
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()

0 comments on commit 6e529b2

Please sign in to comment.