From 817bd31fc7241686837686cbc4c8a179a6d3b6c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Magalh=C3=A3es?= Date: Wed, 2 Sep 2020 18:36:06 -0300 Subject: [PATCH 1/2] adding 3 loop types for connector :foward :backward :yoyo --- src/connector.jl | 62 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/src/connector.jl b/src/connector.jl index 6e64fb5..f73257a 100644 --- a/src/connector.jl +++ b/src/connector.jl @@ -1,7 +1,5 @@ using Tables - abstract type AbstractConnector end - Base.length(conn::AbstractConnector) = Inf hasnext(conn::AbstractConnector) = true reset!(conn::AbstractConnector) = nothing @@ -10,42 +8,76 @@ mutable struct TablesConnector <: AbstractConnector rows state::Int args::Dict{Symbol, Any} + loop::Symbol + incremental::Int end -function TablesConnector(data; shuffle::Bool = false) +function TablesConnector(data; shuffle::Bool = false, loop::Symbol = :none) + if !Tables.istable(data) throw(ArgumentError("data must have the Tables.jl interface")) end + loopTypes =[:none,:foward,:backward,:yoyo] + if !(loop in loopTypes) + throw(ArgumentError("Symbol $loop is not valid")) + end if shuffle data = data[Random.shuffle(1:size(data,1)), :] end - - return TablesConnector(Tables.rows(data), 0, Dict{Symbol, Any}()) + state = loop == :backward ? size(data, 1) + 1 : 0 + incremental = loop == :backward ? -1 : 1 + return TablesConnector(Tables.rows(data), state, Dict{Symbol, Any}(),loop,incremental) end -function TablesConnector(data, orderBy::Symbol; rev::Bool = false) + + +function TablesConnector(data, orderBy::Symbol; rev::Bool = false, loop::Symbol = :none) if !(orderBy in propertynames(data)) throw(ArgumentError("data doesn't have the column $orderBy")) end - + loopTypes =[:none,:foward,:backward,:yoyo] + if !(loop in loopTypes) + throw(ArgumentError("Symbol $loop is not valid")) + end + state = loop == :backward ? size(data, 1) + 1 : 0 data = sort(data, orderBy, rev = rev) - - return TablesConnector(data) + return TablesConnector(data;loop = loop) end TablesConnector(filename::String) = TablesConnector(CSV.read(filename; header = false)) Base.length(conn::TablesConnector) = length(conn.rows) -hasnext(conn::TablesConnector) = conn.state < length(conn) +function hasnext(conn::TablesConnector) + if conn.loop == :none + return conn.state < length(conn) + elseif conn.loop == :foward || conn.loop == :backward ||conn.loop == :yoyo + return true + end +end function next(conn::TablesConnector) - if conn.state >= length(conn) - return nothing - end + if conn.loop == :none + if conn.state >= length(conn) + return nothing + end + + conn.state += 1 + elseif conn.loop == :foward || conn.loop == :backward + conn.state += conn.incremental + temp = conn.state % length(conn) + conn.state = temp == 0 ? length(conn) : temp + elseif conn.loop == :yoyo + conn.state += conn.incremental + if conn.state == length(conn) + conn.incremental *= - 1 + elseif conn.state == 0 + conn.state = 2 + conn.incremental *= - 1 + end - conn.state += 1 + end return DataFrame([conn.rows[conn.state]]) end @@ -64,6 +96,6 @@ end function next(conn::GeneratorConnector) total = 100 data = conn.generator(;n_samples = total, conn.args...) - + return DataFrame(data[1 + Int(floor(rand(1,1)[1] .* size(data)[1])), :]) end From cc6a8ac0bf33edf03d83cbecd6b839dc5c376483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Magalh=C3=A3es?= Date: Thu, 3 Sep 2020 16:56:08 -0300 Subject: [PATCH 2/2] Using Multiple Dispatch Altered the way loops are read by using multiple dispatch --- src/connector.jl | 79 +++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 35 deletions(-) diff --git a/src/connector.jl b/src/connector.jl index f73257a..159ed4b 100644 --- a/src/connector.jl +++ b/src/connector.jl @@ -4,43 +4,41 @@ Base.length(conn::AbstractConnector) = Inf hasnext(conn::AbstractConnector) = true reset!(conn::AbstractConnector) = nothing +abstract type LoopType end +struct Foward <: LoopType end +struct Backward <: LoopType end +struct Yoyo <: LoopType end +struct None <: LoopType end + mutable struct TablesConnector <: AbstractConnector rows state::Int args::Dict{Symbol, Any} - loop::Symbol + loop::LoopType incremental::Int end -function TablesConnector(data; shuffle::Bool = false, loop::Symbol = :none) +function TablesConnector(data; shuffle::Bool = false, loop::LoopType = None()) if !Tables.istable(data) throw(ArgumentError("data must have the Tables.jl interface")) end - loopTypes =[:none,:foward,:backward,:yoyo] - if !(loop in loopTypes) - throw(ArgumentError("Symbol $loop is not valid")) - end if shuffle data = data[Random.shuffle(1:size(data,1)), :] end - state = loop == :backward ? size(data, 1) + 1 : 0 - incremental = loop == :backward ? -1 : 1 + state = typeof(loop) == typeof(Backward()) ? size(data, 1) + 1 : 0 + incremental = typeof(loop) == typeof(Backward()) ? -1 : 1 return TablesConnector(Tables.rows(data), state, Dict{Symbol, Any}(),loop,incremental) end -function TablesConnector(data, orderBy::Symbol; rev::Bool = false, loop::Symbol = :none) +function TablesConnector(data, orderBy::Symbol; rev::Bool = false, loop::LoopType = None()) if !(orderBy in propertynames(data)) throw(ArgumentError("data doesn't have the column $orderBy")) end - loopTypes =[:none,:foward,:backward,:yoyo] - if !(loop in loopTypes) - throw(ArgumentError("Symbol $loop is not valid")) - end - state = loop == :backward ? size(data, 1) + 1 : 0 + data = sort(data, orderBy, rev = rev) return TablesConnector(data;loop = loop) end @@ -49,34 +47,45 @@ TablesConnector(filename::String) = TablesConnector(CSV.read(filename; header = Base.length(conn::TablesConnector) = length(conn.rows) function hasnext(conn::TablesConnector) - if conn.loop == :none + if typeof(conn.loop) == typeof(None()) return conn.state < length(conn) - elseif conn.loop == :foward || conn.loop == :backward ||conn.loop == :yoyo + else return true end end -function next(conn::TablesConnector) - if conn.loop == :none - if conn.state >= length(conn) - return nothing - end - - conn.state += 1 - elseif conn.loop == :foward || conn.loop == :backward - conn.state += conn.incremental - temp = conn.state % length(conn) - conn.state = temp == 0 ? length(conn) : temp - elseif conn.loop == :yoyo - conn.state += conn.incremental - if conn.state == length(conn) - conn.incremental *= - 1 - elseif conn.state == 0 - conn.state = 2 - conn.incremental *= - 1 - end +function next(conn::TablesConnector) return next(conn,conn.loop) end + +function next(conn::TablesConnector,::None) + if conn.state >= length(conn) + return nothing + end + + conn.state += 1 + return DataFrame([conn.rows[conn.state]]) +end + +function next(conn::TablesConnector,::Foward) + conn.state += conn.incremental + temp = conn.state % length(conn) + conn.state = temp == 0 ? length(conn) : temp + return DataFrame([conn.rows[conn.state]]) +end +function next(conn::TablesConnector,::Backward) + conn.state += conn.incremental + temp = conn.state % length(conn) + conn.state = temp == 0 ? length(conn) : temp + return DataFrame([conn.rows[conn.state]]) +end +function next(conn::TablesConnector,::Yoyo) + conn.state += conn.incremental + if conn.state == length(conn) + conn.incremental *= - 1 + elseif conn.state == 0 + conn.state = 2 + conn.incremental *= - 1 end return DataFrame([conn.rows[conn.state]]) end