Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
43
Task/Dining-philosophers/J/dining-philosophers-1.j
Normal file
43
Task/Dining-philosophers/J/dining-philosophers-1.j
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
reqthreads=: {{ 0&T.@''^:(0>.y-1 T.'')0 }}
|
||||
dispatchwith=: (t.'')every
|
||||
newmutex=: (; 10&T.@0)@>
|
||||
lock=: 11&T.@{:
|
||||
unlock=: 13&T.@{:
|
||||
dl=: 6!:3
|
||||
|
||||
dine=: {{
|
||||
'forkA forkB'=. <"1 /:~ n
|
||||
announce=. m {{ echo m,' ',y }}
|
||||
announce 'will use fork ',(":;{.forkA),' first and put it down last'
|
||||
announce 'will use fork ',(":;{.forkB),' second and put it down first'
|
||||
dl 1
|
||||
while. do.
|
||||
announce 'is hungry'
|
||||
lock forkA
|
||||
announce 'picked up fork ',":;{.forkA
|
||||
lock forkB
|
||||
announce 'picked up fork ',":;{.forkB
|
||||
announce 'is eating'
|
||||
dl 2+(?3e3)%1e3
|
||||
announce 'has finished eating'
|
||||
unlock forkB
|
||||
announce 'has put down fork ',":;{.forkB
|
||||
unlock forkA
|
||||
announce 'has put down fork ',":;{.forkA
|
||||
announce 'has left the room'
|
||||
dl 4+(?1e4)%1e3
|
||||
end.
|
||||
y
|
||||
}}
|
||||
|
||||
start=: {{
|
||||
echo 'Hit enter to exit'
|
||||
dl 1
|
||||
reqthreads 5
|
||||
forks=. newmutex i.5
|
||||
for_philosopher.;:' Aristotle Kant Spinoza Marx Russell' do.
|
||||
forks=. 1|.forks
|
||||
(;philosopher) dine (2{.forks) dispatchwith EMPTY
|
||||
end.
|
||||
exit 1!:1]1
|
||||
}}
|
||||
48
Task/Dining-philosophers/J/dining-philosophers-2.j
Normal file
48
Task/Dining-philosophers/J/dining-philosophers-2.j
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
start''
|
||||
Hit enter to exit
|
||||
Aristotle will use fork 1 first and put it down last
|
||||
Kant will use fork 2 first and put it down last
|
||||
Marx will use fork 0 first and put it down last
|
||||
Spinoza will use fork 3 first and put it down last
|
||||
Russell will use fork 0 first and put it down last
|
||||
Aristotle will use fork 2 second and put it down first
|
||||
Kant will use fork 3 second and put it down first
|
||||
Marx will use fork 4 second and put it down first
|
||||
Spinoza will use fork 4 second and put it down first
|
||||
Russell will use fork 1 second and put it down first
|
||||
Spinoza is hungry
|
||||
Marx is hungry
|
||||
Aristotle is hungry
|
||||
Aristotle picked up fork 1
|
||||
Spinoza picked up fork 3
|
||||
Marx picked up fork 0
|
||||
Kant is hungry
|
||||
Aristotle picked up fork 2
|
||||
Spinoza picked up fork 4
|
||||
Aristotle is eating
|
||||
Spinoza is eating
|
||||
Russell is hungry
|
||||
Aristotle has finished eating
|
||||
Spinoza has finished eating
|
||||
Aristotle has put down fork 2
|
||||
Kant picked up fork 2
|
||||
Spinoza has put down fork 4
|
||||
Marx picked up fork 4
|
||||
Aristotle has put down fork 1
|
||||
Spinoza has put down fork 3
|
||||
Kant picked up fork 3
|
||||
Marx is eating
|
||||
Aristotle has left the room
|
||||
Spinoza has left the room
|
||||
Kant is eating
|
||||
Kant has finished eating
|
||||
Marx has finished eating
|
||||
Kant has put down fork 3
|
||||
Marx has put down fork 4
|
||||
Kant has put down fork 2
|
||||
Marx has put down fork 0
|
||||
Russell picked up fork 0
|
||||
Kant has left the room
|
||||
Marx has left the room
|
||||
Russell picked up fork 1
|
||||
Russell is eating
|
||||
27
Task/Dining-philosophers/J/dining-philosophers-3.j
Normal file
27
Task/Dining-philosophers/J/dining-philosophers-3.j
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
". noun define -. CRLF NB. Fixed tacit simulation code...
|
||||
|
||||
simulate=.
|
||||
''"_@:((<@:(1 -~ 1&({::)) 1} ])@:(([ 0 0&$@(1!:2&2)@:(((6j3 ": 9&({::)) , ':
|
||||
'"_) , ' starts waiting and thinking about hunger.' ,~ 8&({::) {:: 0&({::)))@
|
||||
:(<@:(6&({::) , 8&({::)) 6} ])@:((<@:((0 (0 {:: ])`(<@:(1 {:: ]))`(2 {:: ])}
|
||||
])@:(3 8 2&{)) 2} ])@:(<@:2: 3} ]))@:((<@:((0 (0 {:: ])`(<@:(1 {:: ]))`(2 {::
|
||||
])} ])@:(5 8 4&{)) 4} ])@:(<@:_: 5} ]))`(([ 0 0&$@(1!:2&2)@:(((6j3 ": 9&({::
|
||||
)) , ': '"_) , ' starts eating.' ,~ 8&({::) {:: 0&({::)))@:((<@:((0 (0 {:: ])
|
||||
`(<@:(1 {:: ]))`(2 {:: ])} ])@:(3 8 2&{)) 2} ])@:(<@:1: 3} ]))@:((<@:((0 (0 {
|
||||
:: ])`(<@:(1 {:: ]))`(2 {:: ])} ])@:(5 8 4&{)) 4} ])@:(<@:(_2 * ^.@:?@:0:) 5}
|
||||
])))@.(7&({::) > 1 +/@:= 2&({::))`((<@:(}.@:(6&({::))) 6} ])@:(([ 0 0&$@(1!:
|
||||
2&2)@:(((6j3 ": 9&({::)) , ': '"_) , ' starts eating.' ,~ 8&({::) {:: 0&({::)
|
||||
))@:((<@:((0 (0 {:: ])`(<@:(1 {:: ]))`(2 {:: ])} ])@:(3 8 2&{)) 2} ])@:(<@:1:
|
||||
3} ]))@:((<@:((0 (0 {:: ])`(<@:(1 {:: ]))`(2 {:: ])} ])@:(5 8 4&{)) 4} ])@:(
|
||||
<@:(_2 * ^.@:?@:0:) 5} ])))@:(<@:({.@:(6&({::))) 8} ])^:(1 <: #@:(6&({::)))@:
|
||||
([ 0 0&$@(1!:2&2)@:(((6j3 ": 9&({::)) , ': '"_) , ' starts thinking.' ,~ 8&({
|
||||
::) {:: 0&({::)))@:((<@:((0 (0 {:: ])`(<@:(1 {:: ]))`(2 {:: ])} ])@:(3 8 2&{)
|
||||
) 2} ])@:(<@:0: 3} ]))@:((<@:((0 (0 {:: ])`(<@:(1 {:: ]))`(2 {:: ])} ])@:(5 8
|
||||
4&{)) 4} ])@:(<@:(_1 * ^.@:?@:0:) 5} ])))@.('' ($ ,) 8&({::) { 2&({::)))@:(<
|
||||
@:(0 I.@:= 4&({::)) 8} ])@:(<@:((- <./)@:(4&({::))) 4} ])@:(<@:(9&({::) + <./
|
||||
@:(4&({::))) 9} ])^:(0 < 1&({::))^:_)@:(([ 0 0&$@(1!:2&2)@:(((6j3 ": 9&({::))
|
||||
, ': '"_) , 'All of them start thinking.'"_))@:((0 ; <.@:(2 %~ #@:(0&({::)))
|
||||
) 9 7} ])@:((0:"_1 ,&< (_1 * ^.@:?@:0:)&>)@:(0&({::)) 2 4} ])@:((;:@:(0&({::)
|
||||
) ,&< ''"_) 0 6} ]))@:(,&(;:8$','))@:;
|
||||
|
||||
)
|
||||
17
Task/Dining-philosophers/J/dining-philosophers-4.j
Normal file
17
Task/Dining-philosophers/J/dining-philosophers-4.j
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
'Aristotle Kant Spinoza Marx Russell' simulate 11
|
||||
0.000: All of them start thinking.
|
||||
0.097: Spinoza starts eating.
|
||||
0.474: Aristotle starts eating.
|
||||
0.950: Russell starts waiting and thinking about hunger.
|
||||
1.125: Kant starts waiting and thinking about hunger.
|
||||
2.263: Spinoza starts thinking.
|
||||
2.263: Russell starts eating.
|
||||
2.762: Marx starts waiting and thinking about hunger.
|
||||
2.771: Spinoza starts waiting and thinking about hunger.
|
||||
4.769: Russell starts thinking.
|
||||
4.769: Kant starts eating.
|
||||
4.845: Russell starts waiting and thinking about hunger.
|
||||
5.166: Aristotle starts thinking.
|
||||
5.166: Marx starts eating.
|
||||
5.915: Marx starts thinking.
|
||||
5.915: Spinoza starts eating.
|
||||
30
Task/Dining-philosophers/J/dining-philosophers-5.j
Normal file
30
Task/Dining-philosophers/J/dining-philosophers-5.j
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
'Aristotle Kant Spinoza Marx Russell Laozi Nezahualcoyotl Averroes' simulate 22
|
||||
0.000: All of them start thinking.
|
||||
0.077: Nezahualcoyotl starts eating.
|
||||
0.312: Marx starts eating.
|
||||
0.424: Laozi starts eating.
|
||||
0.502: Kant starts eating.
|
||||
0.541: Marx starts thinking.
|
||||
0.545: Marx starts eating.
|
||||
0.660: Laozi starts thinking.
|
||||
0.715: Laozi starts eating.
|
||||
0.766: Aristotle starts waiting and thinking about hunger.
|
||||
0.871: Laozi starts thinking.
|
||||
0.871: Aristotle starts eating.
|
||||
0.893: Averroes starts waiting and thinking about hunger.
|
||||
1.035: Nezahualcoyotl starts thinking.
|
||||
1.035: Averroes starts eating.
|
||||
1.071: Laozi starts waiting and thinking about hunger.
|
||||
1.168: Kant starts thinking.
|
||||
1.168: Laozi starts eating.
|
||||
1.614: Russell starts waiting and thinking about hunger.
|
||||
1.660: Spinoza starts waiting and thinking about hunger.
|
||||
1.813: Aristotle starts thinking.
|
||||
1.813: Russell starts eating.
|
||||
2.022: Marx starts thinking.
|
||||
2.022: Spinoza starts eating.
|
||||
2.164: Russell starts thinking.
|
||||
2.182: Aristotle starts eating.
|
||||
2.339: Marx starts waiting and thinking about hunger.
|
||||
2.446: Aristotle starts thinking.
|
||||
2.446: Marx starts eating.
|
||||
116
Task/Dining-philosophers/J/dining-philosophers-6.j
Normal file
116
Task/Dining-philosophers/J/dining-philosophers-6.j
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
NB. Quick and dirty tacit toolkit...
|
||||
|
||||
o=. @:
|
||||
c=."_
|
||||
|
||||
ver=. (0:`)([:^:)
|
||||
|
||||
d=. (fix=. (;:'f.')ver) (train=.(;:'`:')ver&6) (an=. <@:((,'0') (,&<) ]))
|
||||
ver=. (an f. o fix'ver')ver o an f.
|
||||
z=. ((an'')`($ ,)`) (`:6)
|
||||
d=. (a0=. `'') (a1=. (@[) ((<'&')`) (`:6)) (a2=. (`(<(":0);_)) (`:6))
|
||||
av=. ((an o fix'a0')`) (`(an o fix'a1')) (`(an o fix'a2') ) (`:6)
|
||||
|
||||
Fetch=. (ver o train ;:'&{::')&.> o i. f.av
|
||||
tie=. ver o train ;:'`'
|
||||
|
||||
indices=. (, $~ 1 -.~ $) o (train"0 o ((1 -: L.)S:1 # <S:1) o (tie&'') o fix :: ])
|
||||
f=. ((ver o train ;:'&{')) o indices o train f.av
|
||||
|
||||
'A B'=. 2 Fetch
|
||||
head=. (;:'<@:') {.~ 2 * 1 = #@[
|
||||
h=. train o (indices o train o (A f) (head , (B f)@] , < o an@[ , (;:'}]')c) ]) f.av
|
||||
|
||||
DropIfNB=. < o ('('"_ , ] , ')'"_) o ((}: ^: ('NB.' -: 3&{. o > o {:)) &. ;:)
|
||||
pipe=. ([ , ' o ' , ])&:>/ o |.
|
||||
|
||||
is=. ". o (, o ": o > , '=. ' , pipe o (DropIfNB;._2) o ". o ('0 ( : 0)'c)) f.av
|
||||
|
||||
NB.--------------------------------------------------------------------------------------
|
||||
|
||||
NB. Producing the verb simulate...
|
||||
|
||||
Note 0
|
||||
|
||||
NB. X and Y...
|
||||
N - Philosophers names
|
||||
C - Number of chronological events to simulate
|
||||
|
||||
NB. Local...
|
||||
A - Activity (0 - Thinking, 1 -eating, 2 - Thinking while queuing,)
|
||||
B - New activity
|
||||
T - Residual time left for the activity
|
||||
S - Starting time for the new activity
|
||||
Q - Queue
|
||||
U - Upper bound for the number of philosophers who can eat simultaneously
|
||||
P - Active philosopher
|
||||
E - Elapsed Time (only for information purposes)
|
||||
)
|
||||
|
||||
amend=. 0 (0 {:: ])`(<@:(1 {:: ]))`(2 {:: ])} ]
|
||||
|
||||
'N C A B T S Q U P E'=. 10 Fetch NB. 10 Boxes
|
||||
|
||||
thinktime=. _1 * ^. o ? o 0: NB. Exponentially distributed at a rate of one
|
||||
eattime =. _2 * ^. o ? o 0: NB. Exponentially distributed at a rate of one-half
|
||||
j=. ,&<
|
||||
|
||||
time=. (6j3 ": E) , ': 'c
|
||||
|
||||
start is
|
||||
(N Q)`((;: o N) j (''c)) h NB. Boxing the names, empty queue
|
||||
(A T)`((0:items j thinktime&>) o N) h NB. All start thinking
|
||||
(E U)`(0 ; <. o (2 %~ # o N)) h NB. Elapsed time 0, Upper bound
|
||||
[ echo o (time , 'All of them start thinking.'c)
|
||||
)
|
||||
|
||||
CanEat=. U > 1 +/ o = A NB. Can eat if there is a suitable place at the table
|
||||
|
||||
eat is
|
||||
T`(amend o ((S P T)f))h o (S`eattime h) NB. Eating time
|
||||
A`(amend o ((B P A)f))h o (B`1: h) NB. Activity: eating
|
||||
[ echo o (time , ' starts eating.' ,~ P {:: N)
|
||||
)
|
||||
|
||||
enqueue is
|
||||
T`(amend o ((S P T)f))h o (S`_:h) NB. Inactive until someone else ends eating
|
||||
A`(amend o ((B P A)f))h o (B`2:h) NB. Activity: thinking while queuing
|
||||
Q`(Q , P)h NB. Enqueuing
|
||||
[ echo o (time , ' starts waiting and thinking about hunger.' ,~ P {:: N)
|
||||
)
|
||||
|
||||
thinking=. enqueue`eat@.CanEat NB. Either enqueues or eats after thinking
|
||||
|
||||
dequeue is
|
||||
P`({. o Q)h NB. Activating the one in front of the queue
|
||||
eat NB. and starts eating
|
||||
Q`(}. o Q)h NB. dequeuing
|
||||
)
|
||||
|
||||
eating is NB. Thinks after eating
|
||||
T`(amend o ((S P T)f))h o (S`thinktime h) NB. Thinking time
|
||||
A`(amend o ((B P A)f))h o (B`0: h) NB. Activity: thinking
|
||||
[ echo o ( time , ' starts thinking.' ,~ P {:: N)
|
||||
dequeue ^: (1 <: # o Q) NB. Dequeuing a philosopher (if possible)
|
||||
)
|
||||
|
||||
update is
|
||||
E`(E + <./ o T)h NB. Updating the elapsed time
|
||||
T`((- <./)@:T) h NB. Updating the residual times
|
||||
P`(0 I. o = T) h NB. Setting the active philosopher
|
||||
thinking`eating@.((P { A)z) NB. Was thinking or eating?
|
||||
C`(1 -~ C) h NB. One chronological event completed
|
||||
)
|
||||
|
||||
simulate is NB. Discrete event simulation (dyadic verb)
|
||||
; NB. Linking the arguments (N C)
|
||||
,&(;:8$',') NB. Appending 8 local boxes (A B T S Q U P E)
|
||||
start
|
||||
update ^: (0 < C) ^: _ NB. Updating while events are less than C
|
||||
''c
|
||||
)
|
||||
|
||||
simulate=. simulate f.
|
||||
|
||||
NB. The simulation code is produced by the sentence,
|
||||
NB. 77 (-@:[ ]\ 5!:5@<@:]) 'simulate'
|
||||
Loading…
Add table
Add a link
Reference in a new issue