Advertisement
imk0tter

mSL++ List Class

Jan 22nd, 2020
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
mIRC 5.79 KB | None | 0 0
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ; Description: This class can be used as a dynamically sized      ;
  3. ; List to store any data type                                     ;
  4. ;                                                                 ;
  5. ; Usage: $List(<ListPtr>,<Params>,...).<Member>                   ;
  6. ; Example: var %x = $List                                         ;
  7. ; Example: var %itemID = $List(%x,$long(500)).add                 ;
  8. ; Example: $List(%x,%itemID,$long(499)).set                       ;
  9. ; Example: $List(%x,%ItemID).remdel                               ;
  10. ; Example: $List(%x).delete                                       ;
  11. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  12. ;;;;;;;;;;;;;;;;;;;;;;;
  13. ; Public Declarations ;
  14. ;;;;;;;;;;;;;;;;;;;;;;;
  15. alias List.DELETE.Public
  16. alias List.Add.PUBLIC
  17. alias List.Remove.PUBLIC
  18. alias List.Get.PUBLIC
  19. alias List.Count.PUBLIC
  20. alias List.Set.PUBLIC
  21. alias List.Remdel.PUBLIC
  22. alias List.Insert.PUBLIC
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ; Exception Declarations ;
  25. ;;;;;;;;;;;;;;;;;;;;;;;;;;
  26. alias List.EXCEPTION.RangeErr {
  27.   var %object $1, %error $2, %message $5, %scriptLine $3, %scriptDir $4
  28.   return Exception Caught on line $+($chr(40),%scriptLine,:,%scriptDir,$chr(41))from Object ( $+ %object $+ : $+ $IsInstance(%object) $+ ): %error $+  - %message
  29. }
  30. ;;;;;;;;;;;;;;
  31. ; List Alias ;
  32. ;;;;;;;;;;;;;;
  33. alias List {
  34.   var %Class List
  35.   if (!$prop) {
  36.     var %object $Class
  37.     - $inheritsFrom(%object,%class)
  38.     if ($IsPrivate(%class,INIT)) {
  39.       return $($+($,%class,.INIT,$chr(40),%object,$chr(44),$left($regsubex($str(.,$0),/(.)/g,$\n $+ $chr(44)),-1),$chr(41)),2)
  40.     }
  41.   }
  42.   if ($IsPublic(%class,$prop)) {
  43.     return $($+($,%Class,.,$prop,$chr(40),$left($regsubex($str(.,$0),/(.)/g,$\n $+ $chr(44)),-1),$chr(41)),2)
  44.   }
  45.   return $catch(0,MemberErr,$scriptline, $token($script,-1,92),$qt($prop) is not a public member of %Class)
  46. }
  47.  
  48. ;;;;;;;;;;;;;;;;
  49. ; List Methods ;
  50. ;;;;;;;;;;;;;;;;
  51. alias List.INIT {
  52.   var %instance $1
  53.   ;;;;;;;;;;;;;;;;;;;;;;;;
  54.   ; Do Initializing here ;
  55.   ;;;;;;;;;;;;;;;;;;;;;;;;
  56.   return %instance
  57. }
  58. alias List.COUNT {
  59.   return $class($1,COUNT).GetV
  60. }
  61. alias List.REMOVE {
  62.   if $2 !isnum 1- $+ $Class($1,COUNT).GetV { return $catch($1,RangeErr,$scriptline, $token($script,-1,92), $qt($2) is not a valid list index for object $qt($1)) }
  63.   - $Class($1, COUNT, $calc($Class($1,COUNT).GetV - 1)).SetV
  64.   - $Class($1,ITEMS,&items).getb
  65.   var %x $calc(1 + (($2 - 1) * 16)),%y %x + 16,%z $bvar(&items,%x,16).text
  66.   if %y > $bvar(&items,0) { dec %y }
  67.   var %var $bvar(&items,%x,16).text
  68.   bcopy -c &items %x &items %y -1
  69.   - $Class($1,%var).remv
  70.   - $Class($1,ITEMS,&items).setb
  71. }
  72. alias List.INSERT {
  73.   if $3 !isnum 0- $+ $Class($1,COUNT).GetV { return $catch($1,RangeErr,$scriptline, $token($script,-1,92),$qt($3) is not a valid list index for object $qt($1) $chr(40) $+ Valid Ranges: 1- $+ $class($1,COUNT).GetV $+ $chr(41)) }
  74.   - $Class($1, COUNT, $calc($Class($1,COUNT).GetV + 1)).SetV
  75.   - $Class($1, TOTAL, $calc($Class($1,TOTAL).GetV + 1)).SetV
  76.   var %value $base($Class($1,TOTAL).GetV,10,10,16)
  77.  
  78.   - $Class($1,ITEMS,&items).GetB
  79.  
  80.   var %x $calc(1 + (($3 - 1) * 16)),%y $calc(%x + 16)
  81.   bcopy &items %y &items %x -1
  82.   bset -at &items %x %value
  83.   - $Class($1,%value,$2).setv
  84.   - $Class($1,ITEMS,&items).setb
  85.   return $$3
  86. }
  87. alias List.ADD {
  88.   - $Class($1,COUNT,$calc($Class($1,COUNT).GetV + 1)).SetV
  89.   - $Class($1,TOTAL,$calc($Class($1,TOTAL).GetV + 1)).SetV
  90.  
  91.   var %x $base($Class($1,TOTAL).GetV,10,10,16)
  92.   - $Class($1,ITEMS,&items).getb
  93.   bset -at &items $calc(1 + (($class($1,COUNT).GetV - 1) * 16)) %x
  94.   - $Class($1,ITEMS,&items).setb
  95.   - $iif($bvar($2,0),$Class($1,%x,$2).setb,$Class($1,%x,$2).setv)
  96.   return $Class($1,Count).GetV
  97. }
  98. alias List.GET {
  99.   if $2 !isnum 0- $+ $Class($1,COUNT).GetV { return $catch($1,RangeErr,$scriptline, $token($script,-1,92),$qt($2) is not a valid list index for object $qt($1) $chr(40) $+ Valid Ranges: 1- $+ $class($1,COUNT).GetV $+ $chr(41)) }
  100.   $iif($2 == 0,return $Class($1,COUNT).GetV)
  101.   - $Class($1,ITEMS,&items).GetB
  102.   var %var2 $bvar(&items,$calc(1 + (($2 - 1) * 16)),16).text
  103.   return $Class($1,%var2).GetV
  104. }
  105. alias List.SET {
  106.   if $2 !isnum 0- $+ $Class($1,COUNT).GetV { return $catch($1,RangeErr,$scriptline, $token($script,-1,92),$qt($2) is not a valid list index for object $qt($1) $chr(40) $+ Valid Ranges: 1- $+ $class($1,COUNT).GetV $+ $chr(41)) }
  107.   var %z $bvar(&items,$calc(1 + (($2 - 1) * 16)),16).text
  108.   var %x $Class($1,%z).getv
  109.   if $IsInstance(%x) { - $Object(%x).delete }
  110.   - $Class($1,%z,$3).setv
  111. }
  112. alias List.IMPORT {
  113.   var %x $List($1).count
  114.   while %x {
  115.     if $Class($List($1,%x).get).IMPORT {
  116.       + Loaded object $qt($List($1,%x).get) of type $qt($IsInstance($List($1,%x).get))
  117.     }
  118.     dec %x
  119.   }
  120.   if $exists($+(object/,$1.bvr)) {
  121.     bread $+(object/,$1.bvr) 1 $file($+(object/,$1.bvr)).size &data
  122.     - $Class($1,ITEMS,&data).setb
  123.   }
  124. }
  125.  
  126. alias List.EXPORT {
  127.   var %x $List($1).count
  128.   while %x {
  129.     var %y $List($1,%x).get
  130.     if $IsInstance(%y) {
  131.       - $ExportObject(%y)
  132.       + Saved object $qt($List($1,%x).get)
  133.     }
  134.     dec %x
  135.   }
  136.   - $Class($1,ITEMS,&data).getb
  137.   if $exists($+(object/,$1.bvr)) {
  138.     .remove $+(object/,$1.bvr)
  139.   }
  140.   bwrite $+(object/,$1.bvr) 1 -1 &data
  141. }
  142. alias List.DELETE {
  143.   ;;;;;;;;;;;;;;;;;;;;;;
  144.   ; Do Destroying here ;
  145.   ;;;;;;;;;;;;;;;;;;;;;;
  146.   - $Class($1,ITEMS).remv
  147.   - $Class($1,TOTAL).remv
  148.   - $Class($1,COUNT).remv
  149.   if $2 {
  150.     while $hget($1,1).item {
  151.       var %y $v1
  152.       var %x $hget($1,%y)
  153.       if $IsInstance(%x) {
  154.         - $Object(%x,1).delete
  155.         - Removed object: $qt(%x) from list $qt($1)
  156.       }
  157.       - $Class($1,%y).remv
  158.     }
  159.   }
  160.   return $Class($1).DELETE
  161. }
  162. ;;;;;;;;;;;;
  163. ; End List ;
  164. ;;;;;;;;;;;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement