langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,172 @@
double MainTime
'===============
class RingMaster
'===============
'
indexbase 1
sys List[512] 'limit of 512 objects per ringmaster
sys max,acts
'
method Register(sys meth,obj) as sys
sys i
for i=1 to max step 2
if list[i]=0 then exit for 'vacant slot
next
if i>=max then max+=2
List[i]<=meth,obj
return i 'token for deregistration etc
end method
'
method Deregister(sys *i)
if i then List[i]<=0,0 : i=0
end method
'
method Clear()
max=0
end method
'
method Act() 'called by the timer
sys i,q
for i=1 to max step 2
q=List[i]
if q then
call q List[i+1] 'anon object
end if
next
acts++
end method
'
end class
'=================
class ActiveObject
'=================
'
double s,freq,t1,t2,v1,v2
sys nfun,acts,RingToken
RingMaster *Master
'
method fun0() as double
end method
'
method fun1() as double
return sin(2*pi()*freq*MainTime)
end method
'
method func() as double
select case nfun
case 0 : return fun0()
case 1 : return fun1()
end select
'error?
end method
'
method TimeBasedDuties()
t1=t2
v1=v2
t2=MainTime
v2=func
s=s+(v2+v1)*(t2-t1)*0.5 'add slice to integral
acts++
end method
'
method RegisterWith(RingMaster*r)
@Master=@r
if @Master then
RingToken=Master.register @TimeBasedDuties,@this
end if
end method
'
method Deregister()
if @Master then
Master.Deregister RingToken 'this is set to null
end if
end method
'
method Output() as double
return s
end method
'
method Input(double fr=0,fun=0)
if fr then freq=fr
nfun=fun
end method
method ClearIntegral()
s=0
end method
'
end class
'SETUP TIMING SYSTEM
'===================
extern library "kernel32.dll"
declare QueryPerformanceCounter (quad*c)
declare QueryPerformanceFrequency(quad*f)
declare Sleep(sys milliseconds)
end extern
'
quad scount,tcount,freq
QueryPerformanceFrequency freq
double tscale=1/freq
double t1,t2
QueryPerformanceCounter scount
macro PrecisionTime(time)
QueryPerformanceCounter tcount
time=(tcount-scount)*tscale
end macro
'====
'TEST
'====
double integral
double tevent1,tevent2
RingMaster Rudolpho
ActiveObject A
'
A.RegisterWith Rudolpho
A.input (fr=0.5, fun=1) 'start with the freqency function (1)
'
'SET EVENT TIMES
'===============
tEvent1=2.0 'seconds
tEvent2=2.5 'seconds
'
PrecisionTime t1 'mark initial time
MainTime=t1
'
'
'EVENT LOOP
'==========
'
do
PrecisionTime t2
MainTime=t2
if t2-t1>=0.020 'seconds interval
Rudolpho.Act 'service all active objects
t1=t2
end if
'
if tEvent1>=0 and MainTime>=tEvent1
A.input (fun=0) 'switch to null function (0)
tEvent1=-1 'disable this event from happening again
end if
if MainTime>=tEvent2
integral=A.output()
exit do 'end of session
end if
'
sleep 5 'hand control to OS for a while
end do
print str(integral,4)
Rudolpho.clear

View file

@ -0,0 +1,60 @@
declare
fun {Const X}
fun {$ _} X end
end
fun {Now}
{Int.toFloat {Property.get 'time.total'}} / 1000.0
end
class Integrator from Time.repeat
attr
k:{Const 0.0}
s:0.0
t1 k_t1
t2 k_t2
meth init(SampleIntervalMS)
t1 := {Now}
k_t1 := {@k @t1}
{self setRepAll(action:Update
delay:SampleIntervalMS)}
thread
{self go}
end
end
meth input(K)
k := K
end
meth output($)
@s
end
meth Update
t2 := {Now}
k_t2 := {@k @t2}
s := @s + (@k_t1 + @k_t2) * (@t2 - @t1) / 2.0
t1 := @t2
k_t1 := @k_t2
end
end
Pi = 3.14159265
F = 0.5
I = {New Integrator init(10)}
in
{I input(fun {$ T}
{Sin 2.0 * Pi * F * T}
end)}
{Delay 2000} %% ms
{I input({Const 0.0})}
{Delay 500} %% ms
{Show {I output($)}}
{I stop}

View file

@ -0,0 +1,72 @@
Prototype.d ValueFunction(f.d, t.d)
Class IntegralClass
Time0.i
Mutex.i
S.d
Freq.d
Thread.i
Quit.i
*func.ValueFunction
Protect Method Sampler()
Repeat
Delay(1)
If This\func And This\Mutex
LockMutex(This\Mutex)
This\S + This\func(This\Freq, ElapsedMilliseconds()-This\Time0)
UnlockMutex(This\Mutex)
EndIf
Until This\Quit
EndMethod
BeginPublic
Method Input(*func.ValueFunction)
LockMutex(This\Mutex)
This\func = *func
UnlockMutex(This\Mutex)
EndMethod
Method.d Output()
Protected Result.d
LockMutex(This\Mutex)
Result = This\S
UnlockMutex(This\Mutex)
MethodReturn Result
EndMethod
Method Init(F.d, *f)
This\Freq = F
This\func = *f
This\Mutex = CreateMutex()
This\Time0 = ElapsedMilliseconds()
This\Thread = CreateThread(This\Sampler, This)
ThreadPriority(This\Thread, 10)
EndMethod
Method Release()
This\Quit = #True
WaitThread(This\Thread)
EndMethod
EndPublic
EndClass
;- Procedures for generating values
Procedure.d n(f.d, t.d)
; Returns nothing
EndProcedure
Procedure.d f(f.d, t.d)
; Returns the function of this task
ProcedureReturn Sin(2*#PI*f*t)
EndProcedure
;- Test Code
*a.IntegralClass = NewObject.IntegralClass(0.5, @n()) ; Create the AO
*a\Input(@f()) ; Start sampling function f()
Delay(2000) ; Delay 2 sec
*a\Input(@n()) ; Change to sampling 'nothing'
Delay( 500) ; Wait 1/2 sec
MessageRequester("Info", StrD(*a\Output())) ; Present the result
*a= FreeObject

View file

@ -0,0 +1,74 @@
Module Module1
Sub Main()
Using active As New Integrator
active.Operation = Function(t As Double) Math.Sin(2 * Math.PI * 0.5 * t)
Threading.Thread.Sleep(TimeSpan.FromSeconds(2))
Console.WriteLine(active.Value)
active.Operation = Function(t As Double) 0
Threading.Thread.Sleep(TimeSpan.FromSeconds(0.5))
Console.WriteLine(active.Value)
End Using
Console.ReadLine()
End Sub
End Module
Class Integrator
Implements IDisposable
Private m_Operation As Func(Of Double, Double)
Private m_Disposed As Boolean
Private m_SyncRoot As New Object
Private m_Value As Double
Public Sub New()
m_Operation = Function(void) 0.0
Dim t As New Threading.Thread(AddressOf MainLoop)
t.Start()
End Sub
Private Sub MainLoop()
Dim epoch = Now
Dim t0 = 0.0
Do
SyncLock m_SyncRoot
Dim t1 = (Now - epoch).TotalSeconds
m_Value = m_Value + (Operation(t1) + Operation(t0)) * (t1 - t0) / 2
t0 = t1
End SyncLock
Threading.Thread.Sleep(10)
Loop Until m_Disposed
End Sub
Public Property Operation() As Func(Of Double, Double)
Get
SyncLock m_SyncRoot
Return m_Operation
End SyncLock
End Get
Set(ByVal value As Func(Of Double, Double))
SyncLock m_SyncRoot
m_Operation = value
End SyncLock
End Set
End Property
Public ReadOnly Property Value() As Double
Get
SyncLock m_SyncRoot
Return m_Value
End SyncLock
End Get
End Property
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
m_Disposed = True
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
End Class