Loading ...
Sorry, an error occurred while loading the content.

29008Virtual structures for vim : can it be fast ?

Expand Messages
  • Marc Chantreux
    Jun 2, 2002
      Hi all,

      I've just written "yet another array script" for vim.
      I've done it because all those i've tested doesn't escape the separator.
      For the moment , my array has just new, push and pop functions ... it's
      a start.

      But it uses a lot of function call and exec function ... i wonder about
      the performances
      of this script.

      Viml hackers, please give me your feelings about this code.

      Thanks
      Marc

      --------- mc_stdlib.vim :
      function QuoteString(string)
      return "'" . a:string . "'"
      endfunction

      function DoubleQuoteString(string)
      return '"' . a:string . '"'
      endfunction

      function JoinArgsSimply(separator,...)
      if a:0 == 0
      return ''
      endif
      let l:index = 2
      let l:result = a:1
      while l:index <= a:0
      exec 'let l:result = l:result . a:separator . a:' . l:index
      let l:index = l:index + 1
      endwhile
      return l:result
      endfunction
      --------- virtual_structures.vim :
      " This script create virtual structures using the s:VVS_Root namespace
      " a structure as :
      " <person>
      " <name>Doe</name>
      " <firstname>John</firstname>
      " </person>
      " will be stored as it in vim :
      " s:VVStructures_person_name
      " s:VVStructures_person_firstname
      "
      " Arrays are just defined structure :
      "
      " s:VVStructures_crew_lastIndex = 3 (store the index of last element )
      " s:VVStructures_crew_1 (store the element 1)
      " s:VVStructures_crew_2 (store the element 2)
      " s:VVStructures_crew_3 (store the element 3)

      source mc_stdlib.vim

      " Stuff to know the right variable name which store the information

      let s:VVS_Root = 's:VVStructures'
      let s:PropertySeparator = '_'

      function VVS_StructName(structure,key)
      return JoinArgsSimply(s:PropertySeparator,s:VVS_Root,a:structure,a:key)
      endfunction

      " Ecriture des données

      function VVS_Set(structure,key,value)
      exec 'let '
      \. VVS_StructName(a:structure,a:key)
      \. '=' . DoubleQuoteString(a:value)
      endfunction

      " Lecture des données
      function VVS_Get(structure,key)
      exec 'let l:result=' . VVS_StructName(a:structure,a:key)
      return l:result
      endfunction

      function VVA_LastElement(table)
      exec 'let l:result=' . VVS_StructName(a:table,'lastIndex')
      return l:result
      endfunction

      " Destruction des données

      function VVS_destroy(structure,key)
      exec 'unlet ' . VVS_StructName(a:structure,a:key)
      endfunction

      "-------------------------------------------------------------------------------
      " High Level "
      "

      function VVA_New(table,...)
      call VVS_Set(a:table,'lastIndex',a:0)
      if a:0 > 0
      let l:index = 1
      while l:index <= a:0
      exec 'let l:value = a:' . l:index
      call VVS_Set(a:table,l:index,l:value)
      let l:index = l:index + 1
      endwhile
      endif
      endfunction

      function VVA_Push(table,element)
      let l:index = VVS_Get(a:table,'lastIndex')
      let l:index = l:index + 1
      call VVS_Set(a:table,l:index,a:element)
      call VVS_Set(a:table,'lastIndex',l:index)
      endfunction

      function VVA_Pop(table)
      let l:index = VVS_Get(a:table,'lastIndex')
      if l:index < 1
      return ''
      endif
      let l:result = VVS_Get(a:table,l:index)
      call VVS_destroy(a:table,l:index)
      let l:index = l:index - 1
      call VVS_Set(a:table,'lastIndex',l:index)
      return l:result
      endfunction
      --------- test.vim :
      source virtual_structures.vim
      call VVA_New('table','this','is','a')
      call VVA_Push('table','test')
      echo VVA_Pop('table')
      echo VVA_Pop('table')
      echo VVA_Pop('table')
      echo VVA_Pop('table')
      echo VVA_Pop('table')
      echo VVA_Pop('table')