summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/pgfplots/tex/pgfplotsutil.lua
blob: 40ac83bb385dfff3b9b3e04f99b8abe5f2196521 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
local math=math
local string=string
local type=type
local tostring = tostring
local tonumber = tonumber
local setmetatable = setmetatable
local getmetatable = getmetatable
local print=print
local pairs = pairs
local table=table
local texio=texio

do
local _ENV = pgfplots
---------------------------------------
--

log=texio.write_nl

local stringfind = string.find
local stringsub = string.sub
local tableinsert = table.insert

-- Splits 'str' at delimiter and returns a table of strings
function stringsplit( str, delimiter )
	if not str or not delimiter then error("arguments must not be nil") end
	local result = { }
	local start = 1
	local findStart, findEnd = stringfind( str, delimiter, start )
	while findStart do
		tableinsert( result, stringsub( str, start, findStart-1 ) )
		start = findEnd + 1
		findStart, findEnd = stringfind( str, delimiter, start )
	end
	tableinsert( result, stringsub( str, start ) )
	return result
end

function stringOrDefault(str, default)
    if str == nil or type(str) == 'string' and string.len(str) == 0 then
        return default
    end
    return tostring(str)
end


pgfplotsmath = {}

function pgfplotsmath.isfinite(x)
    if pgfplotsmath.isnan(x) or x == pgfplotsmath.infty or x == -pgfplotsmath.infty then
        return false
    end
    return true
end

local isnan = function(x)
    return x ~= x
end

pgfplotsmath.isnan = isnan

local infty = 1/0
pgfplotsmath.infty = infty

local nan = math.sqrt(-1)
pgfplotsmath.nan = nan

---------------------------------------
--


-- Creates and returns a new class object.
--
-- Usage:
-- complexclass = newClass()
-- function complexclass:constructor()
--      self.re = 0
--      self.im = 0
-- end
--
-- instance = complexclass.new()
--
function newClass()
    local result = {}

	-- we need this such that *instances* (which will have 'result' as meta table)
	-- will "inherit" the class'es methods.
    result.__index = result
    local allocator= function (...)
        local self = setmetatable({}, result)
        self:constructor(...)
        return self
    end
	result.new = allocator
    return result
end



-- Create a new class that inherits from a base class
--
-- base = pgfplots.newClass()
-- function base:constructor()
-- 	self.variable= 'a'
-- 	end
--
-- 	sub = pgfplots.newClassExtends(base)
-- 	function sub:constructor()
-- 		-- call super constructor.
-- 		-- it is ABSOLUTELY CRUCIAL to use <baseclass>.constructor here - not :constructor!
-- 		base.constructor(self)
-- 	end
--
-- 	instance = base.new()
--
-- 	instance2 = sub.new()
--
-- @see newClass
function newClassExtends( baseClass )
    if not baseClass then error "baseClass must not be nil" end

    local new_class = newClass()

    -- The following is the key to implementing inheritance:

    -- The __index member of the new class's metatable references the
    -- base class.  This implies that all methods of the base class will
    -- be exposed to the sub-class, and that the sub-class can override
    -- any of these methods.
    --
    local mt = {} -- getmetatable(new_class)
    mt.__index = baseClass
    setmetatable(new_class,mt)

    return new_class
end


end