<commit_before>-- Copyright (c) 2009-2011 Incremental IP Limited
-- see LICENSE for license information

local setmetatable = setmetatable

local object = require("rima.lib.object")
local proxy = require("rima.lib.proxy")
local lib = require("rima.lib")
local core = require("rima.core")

local typeinfo = object.typeinfo

module(...)


-- Elements -------------------------------------------------------------------

element = object:new_class(_M, "element")
proxy_mt = setmetatable({}, element)

function element:new(exp, key, value)
  return proxy:new(object.new(self, { exp=exp, key=key, value=value }), proxy_mt)
end


function element:key() return proxy.O(self).key end
function element:value() return proxy.O(self).value end
function element:expression() return proxy.O(self).exp end

function element:display()
  self = proxy.O(self)
  local v = self.value
  if v then
    local t = typeinfo(v)
    if not t.undefined_t and not t.table then
      return v
    end
  end
  return self.key
end


function element:__eval(S)
  local s = proxy.O(self)
  local value, type, addr = core.eval(s.exp, S)
  if value == s.exp then
    return self
  end
  if core.defined(value) then value = addr or s.exp end
  return element:new(value, s.key, s.value)
end


function element:__defined()
  local v = proxy.O(self).value
  return v ~= nil
end


function element:__arithmetic()
  return core.arithmetic(proxy.O(self).value)
end


function element:__repr(format)
  self = proxy.O(self)
  if format.format == "dump" then
    return ("element(%s, key=%s, value=%s)"):
      format(lib.repr(self.exp, format), lib.repr(self.key, format), lib.repr(self.value, format))
  else
    if core.arithmetic(self.value) then
      return lib.repr(self.value, format)
    else
      return lib.repr(self.exp, format)
    end
  end
end
proxy_mt.__tostring = lib.__tostring
element.__tostring = lib.__tostring


-- Operators -------------------------------------------------------------------

function element:extract()
  local e = proxy.O(self)
  local v = e.value
  if typeinfo(v).undefined_t then
    return e.exp
  else
    return v
  end
end


local function ex(a)
  return lib.convert(a, "extract")
end


function proxy_mt.__add(a, b) return ex(a) + ex(b) end
function proxy_mt.__sub(a, b) return ex(a) - ex(b) end
function proxy_mt.__mul(a, b) return ex(a) * ex(b) end
function proxy_mt.__div(a, b) return ex(a) / ex(b) end
function proxy_mt.__pow(a, b) return ex(a) ^ ex(b) end

function proxy_mt:__unm() return -proxy.O(self).value end
function proxy_mt:__index(i) return proxy.O(self).exp[i] end


-- EOF -------------------------------------------------------------------------<commit_msg>rima.sets.element no longer uses proxy<commit_after>-- Copyright (c) 2009-2011 Incremental IP Limited
-- see LICENSE for license information

local setmetatable = setmetatable

local object = require("rima.lib.object")
local lib = require("rima.lib")
local core = require("rima.core")

local typeinfo = object.typeinfo

module(...)


-- Elements -------------------------------------------------------------------

element = object:new_class(_M, "element")
local elements = setmetatable({}, { __mode="k" })

function element:new(exp, key, value)
  local e = { exp=exp, key=key, value=value }
  local p = object.new(element, {})
  elements[p] = e
  return p
end


function element:key() return elements[self].key end
function element:value() return elements[self].value end
function element:expression() return elements[self].exp end

function element:display()
  self = elements[self]
  local v = self.value
  if v then
    local t = typeinfo(v)
    if not t.undefined_t and not t.table then
      return v
    end
  end
  return self.key
end


function element:__eval(S)
  local s = elements[self]
  local value, type, addr = core.eval(s.exp, S)
  if value == s.exp then
    return self
  end
  if core.defined(value) then value = addr or s.exp end
  return element:new(value, s.key, s.value)
end


function element:__defined()
  local v = elements[self].value
  return v ~= nil
end


function element:__arithmetic()
  return core.arithmetic(elements[self].value)
end


function element:__repr(format)
  self = elements[self]
  if format.format == "dump" then
    return ("element(%s, key=%s, value=%s)"):
      format(lib.repr(self.exp, format), lib.repr(self.key, format), lib.repr(self.value, format))
  else
    if core.arithmetic(self.value) then
      return lib.repr(self.value, format)
    else
      return lib.repr(self.exp, format)
    end
  end
end
element.__tostring = lib.__tostring


-- Operators -------------------------------------------------------------------

function element:extract()
  self = elements[self]
  local v = self.value
  if typeinfo(v).undefined_t then
    return self.exp
  else
    return v
  end
end


local function ex(a)
  return lib.convert(a, "extract")
end


function element.__add(a, b) return ex(a) + ex(b) end
function element.__sub(a, b) return ex(a) - ex(b) end
function element.__mul(a, b) return ex(a) * ex(b) end
function element.__div(a, b) return ex(a) / ex(b) end
function element.__pow(a, b) return ex(a) ^ ex(b) end

function element:__unm() return -elements[self].value end
function element:__index(i) return elements[self].exp[i] end


-- EOF -------------------------------------------------------------------------
{'fraction_non_alphanumeric': 0.19365079365079366, 'fraction_numerical': 0.002987861811391223, 'mean_word_length': 2.75859649122807, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6168699', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1689, 'n_words': 493}