conflict-calendars 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7fdbd2d218e9911e722d49725d42aaed358e7f85
4
- data.tar.gz: e10c5cc4822beeb5b7bbe0fba43b2c3b96d7bc40
3
+ metadata.gz: 5a81e8553db9741c2e5ce3381161455abe8ef749
4
+ data.tar.gz: 07d51fbbf8d040eb3ba3d42656e635df7b2664c7
5
5
  SHA512:
6
- metadata.gz: cd2b529868cf1b5a31f877e1f34af3adf17698295e50e72401a4b2dbee0c7f3be93a05d38ff3e09166e82aeeeed091971d1f3843a58728ab084c2a99945bec8d
7
- data.tar.gz: 57338143d9a35e18c13d019ad9e46a2e02393f60fe26a53841e293d9fc3d157c515dcf25ca39b08d414c9e39c18a969b80930a5c165c3668a7a62f2ad61dc6e8
6
+ metadata.gz: 429b6819ff883c49bc229bc6f6388f0bb29d771fb742f532349ad92b5359113366009bc0e68a83af91269547738eac6288f93e8a43d8249753998644fff531bb
7
+ data.tar.gz: 0fa9e1a671588fb30eeae259acaa236edea41fbcab025df05b10d107d5a4bebcdfa08cf7ea689c574b296e303cb9f86d01416cf40cce286cfa769e3a7bf557a6
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
- # conflict-calendars
1
+ # Conflict Calendars
2
2
 
3
- The gem help you find the conflict-calendars from calendars
3
+ The gem help people find the conflicting calendars from given calendars
4
+
5
+ ## Welcome for PR
6
+ The gem need extend and raise, if you have any interests, welocome pull request.
4
7
 
5
8
  ## Installation
6
9
 
@@ -19,41 +22,47 @@ Or install it yourself as:
19
22
  $ gem install conflict-calendars
20
23
 
21
24
  ## Usage
22
- First you should require the library
25
+ Require the library first
23
26
 
24
27
  ```ruby
25
28
  require "conflict/calendars"
26
29
  ```
27
30
 
28
- Then you can create Ccs::Calendar object by this way
31
+ Create calendar object by this way, you can type the name of calendar or not.
29
32
 
30
33
  ```ruby
31
- calendar_A = Ccs::Calendar.new("enter the name of calendar",Time.new(2016,12,10,8,0),Time.new(2016,12,10,13,0)
32
- #=> #<Ccs::Calendar:0x007f9892be85e8 @name="enter the name of calendar", @start_time=2016-12-10 08:00:00 +0800, @end_time=2016-12-10 13:00:00 +0800>
34
+ calendar_A = Ccs::Calendar.new("enter the name of calendar", ,"2016-12-10 08:00", "2016-12-10 13:00")
35
+ calendar_B = Ccs::Calendar.new("2016-12-10 09:00", "2016-12-10 10:30")
36
+ calendar_C = Ccs::Calendar.new("2016-12-10 10:00", "2016-12-10 12:30")
37
+ calendar_D = Ccs::Calendar.new("2016-12-10 15:00", "2016-12-10 17:30")
38
+ calendar_E = Ccs::Calendar.new("2016-12-10 15:20", "2016-12-10 16:30")
33
39
  ```
40
+ Over here we have five calendars, we wanna know who are conflicting?
41
+ So we just do that :
42
+
34
43
  ```ruby
35
- calendar_B = Ccs::Calendar.new(Time.new(2016,12,10,9,0),Time.new(2016,12,10,10,30))
36
- #=> #<Ccs::Calendar:0x007fa36c55bf38 @name=nil, @start_time=2016-12-10 09:00:00 +0800, @end_time=2016-12-10 10:30:00 +0800>
44
+ conflicts = Ccs::Conflicts.new(calendar_A,calendar_B,calendar_C,calendar_D,calendar_E)
45
+
46
+ p conflicts #=> <Ccs::Conflict:0x007fba211e9d20 {2}>
47
+
48
+ puts conflicts.size #=> 2
37
49
  ```
38
- Then you can find the conflicts by this way
50
+
51
+ Now, you know there are exist two conflicting sets.
52
+ Then you can get each information of calendar by this way :
39
53
 
40
54
  ```ruby
41
- conflicts = Ccs::Conflicts.new(calendar_A,calendar_B)
42
- conflicts.size #=> 1
43
55
  conflicts.each do |conflict|
44
- p conflict #=> #<Set: {#<Ccs::Calendar:0x007fb..>, #<Ccs::Calendar:0x007fc..>} >
56
+ p conflict #=> <Set: {#<Ccs::Calendar:0x007fb..>, #<Ccs::Calendar:0x007fc..>} >
57
+ conflict.each do |calendar|
58
+ p calendar #=> <Ccs::Calendar>
59
+ puts calendar.calendarname #=> "calendarname"
60
+ puts calendar.stime #=> 2016-12-10 09:00
61
+ puts calendar.etime #=> 2016-12-10 10:30
62
+ end
45
63
  end
46
64
  ```
47
- Now, you know there are exist two calendars that conflict in the same set. So, you can get each information of calendar by this way :
48
65
 
49
- ```ruby
50
- conflict.each do |calendar|
51
- p calendar #=> #<Ccs::Calendar:0x007fb..>
52
- p calendar.name #=> "enter the name of calendar"
53
- p calendar.start_time #=> "2016-12-10 08:00:00 +0800"
54
- p calendar.end_time #=> "2016-12-10 13:00:00 +0800"
55
- end
56
- ```
57
66
  ## Development
58
67
 
59
68
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -5,7 +5,7 @@ require 'conflict/calendars/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "conflict-calendars"
8
- spec.version = Conflict::Calendars::VERSION
8
+ spec.version = Ccs::VERSION
9
9
  spec.authors = ["Tsao"]
10
10
  spec.email = ["tsaohucn@gmail.com"]
11
11
 
@@ -1,100 +1,9 @@
1
1
  require "conflict/calendars/version"
2
+ require "conflict/calendars/graph"
3
+ require "conflict/calendars/calendar"
4
+ require "conflict/calendars/conflicts"
5
+ require "time"
2
6
  require "set"
3
7
 
4
8
  module Ccs
5
-
6
- class Calendar
7
-
8
- attr_reader :name, :start_time, :end_time
9
-
10
- def initialize(cname = nil,start_time,end_time)
11
- @name = cname
12
- @start_time = start_time
13
- @end_time = end_time
14
- end
15
-
16
- end
17
-
18
- class Conflicts
19
-
20
- include Enumerable
21
-
22
- def initialize(*calendars)
23
- @calendars = calendars
24
- compute!
25
- end
26
-
27
- def inspect
28
- "#<Ccs::Conflict:#{'0x00%x' % (object_id << 1)} {#{@conflicts.size}}>"
29
- end
30
-
31
- def compute!
32
- result = @calendars.combination(2).to_a.map do |comb|
33
- calendar_first = comb.first
34
- calendar_last = comb.last
35
- comb if overlaps(calendar_first.start_time,calendar_first.end_time,calendar_last.start_time,calendar_last.end_time)
36
- end.compact
37
- @conflicts = Graph.new(result).find_maximum_cliques
38
- end
39
-
40
- def each(&block)
41
- @conflicts.each(&block)
42
- end
43
-
44
- def size
45
- @conflicts.size
46
- end
47
-
48
- private
49
-
50
- def overlaps(t1_start,t1_end,t2_start,t2_end)
51
- if (t1_start - t2_end) * (t2_start - t1_end) >= 0
52
- return true
53
- else
54
- return false
55
- end
56
- end
57
-
58
- end
59
-
60
- class Graph
61
-
62
- def initialize(edges)
63
- @edges = edges
64
- end
65
-
66
- def find_maximum_cliques
67
- @cliques ||= []
68
- bron_kerbosch(Set.new, nodes, Set.new) if @cliques.empty?
69
-
70
- @cliques
71
- end
72
-
73
- private
74
-
75
- def nodes
76
- @nodes ||= @edges.flatten.uniq
77
- end
78
-
79
- def neighbours
80
- @neighbours ||= nodes.map do |node|
81
- node_neighbours =
82
- @edges.select { |edge| edge.include? node }.flatten - [node]
83
-
84
- [node, node_neighbours]
85
- end.to_h
86
- end
87
-
88
- def bron_kerbosch(re, pe, xe)
89
- @cliques << re if pe.empty? && xe.empty?
90
-
91
- pe.each do |ve|
92
- bron_kerbosch(re | [ve], pe & neighbours[ve], xe & neighbours[ve])
93
- pe -= [ve]
94
- xe |= [ve]
95
- end
96
- end
97
-
98
- end
99
-
100
- end
9
+ end
@@ -0,0 +1,15 @@
1
+ module Ccs
2
+
3
+ class Calendar
4
+
5
+ attr_reader :calendarname, :stime, :etime
6
+
7
+ def initialize(calendarname = nil, stime, etime)
8
+ @calendarname = calendarname
9
+ @stime = Time.parse(stime)
10
+ @etime = Time.parse(etime)
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,45 @@
1
+ module Ccs
2
+
3
+ class Conflicts
4
+
5
+ include Enumerable
6
+
7
+ def initialize(*calendars)
8
+ @calendars = calendars
9
+ compute!
10
+ end
11
+
12
+ def inspect
13
+ "#<Ccs::Conflict:#{'0x00%x' % (object_id << 1)} {#{@conflicts.size}}>"
14
+ end
15
+
16
+ def compute!
17
+ result = @calendars.combination(2).to_a.map do |comb|
18
+ calendar_first = comb.first
19
+ calendar_last = comb.last
20
+ comb if overlaps(calendar_first.stime, calendar_first.etime, calendar_last.stime, calendar_last.etime)
21
+ end.compact
22
+ @conflicts = Ccs::Graph.new(result).find_maximum_cliques
23
+ end
24
+
25
+ def each(&block)
26
+ @conflicts.each(&block)
27
+ end
28
+
29
+ def size
30
+ @conflicts.size
31
+ end
32
+
33
+ private
34
+
35
+ def overlaps(t1_start,t1_end,t2_start,t2_end)
36
+ if (t1_start - t2_end) * (t2_start - t1_end) >= 0
37
+ return true
38
+ else
39
+ return false
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,43 @@
1
+ module Ccs
2
+
3
+ class Graph
4
+
5
+ def initialize(edges)
6
+ @edges = edges
7
+ end
8
+
9
+ def find_maximum_cliques
10
+ @cliques ||= []
11
+ bron_kerbosch(Set.new, nodes, Set.new) if @cliques.empty?
12
+
13
+ @cliques
14
+ end
15
+
16
+ private
17
+
18
+ def nodes
19
+ @nodes ||= @edges.flatten.uniq
20
+ end
21
+
22
+ def neighbours
23
+ @neighbours ||= nodes.map do |node|
24
+ node_neighbours =
25
+ @edges.select { |edge| edge.include? node }.flatten - [node]
26
+
27
+ [node, node_neighbours]
28
+ end.to_h
29
+ end
30
+
31
+ def bron_kerbosch(re, pe, xe)
32
+ @cliques << re if pe.empty? && xe.empty?
33
+
34
+ pe.each do |ve|
35
+ bron_kerbosch(re | [ve], pe & neighbours[ve], xe & neighbours[ve])
36
+ pe -= [ve]
37
+ xe |= [ve]
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -1,5 +1,3 @@
1
- module Conflict
2
- module Calendars
3
- VERSION = "0.1.5"
4
- end
1
+ module Ccs
2
+ VERSION = "0.2.0"
5
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: conflict-calendars
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tsao
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-31 00:00:00.000000000 Z
11
+ date: 2017-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -55,6 +55,9 @@ files:
55
55
  - bin/setup
56
56
  - conflict-calendars.gemspec
57
57
  - lib/conflict/calendars.rb
58
+ - lib/conflict/calendars/calendar.rb
59
+ - lib/conflict/calendars/conflicts.rb
60
+ - lib/conflict/calendars/graph.rb
58
61
  - lib/conflict/calendars/version.rb
59
62
  homepage: https://github.com/tsaohucn/conflict-calendars.git
60
63
  licenses: