Farean's Link List Array

From Gwen Morse's Wiki
Jump to: navigation, search
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Farean @ Lusternia forums
;;
;; http://forums.aetolia.com/index.php?showtopic=9698
;;
;; Linked list Array

;; Decided to expand upon some of this.

;; In my version, it imitates the behavior something like a linked list 
;; would have rather than an array (but does not have the efficient 
;; removal a real linked list has). Starting index is 1. No need to 
;; initialize the list. Like in CMud, if the list does not exist when 
;; you add an item to it, it is automatically created.

;; Thank you Khezar, Daelin, and Rudha for the TF help. 

;; Example of a linked list
;; If you have a list like..

;; 1=>A
;; 2=>B
;; 3=>C
;; 4=>D

;; If you remove, say, C, you get...

;; 1=>A
;; 2=>B
;; 3=>D

;; In a real array, the indices wouldn't shift.

/def get_val=\
     /result $[{1}]

/def sys_alert=\
    /echo -p @{Cbgblue,BCcyan}[@{n}@{Cbgblue,BCwhite}@{u}FareanSystem@{n}@{Cbgblue,BCcyan}]@{n}@{Cbgblue,BCwhite}: %{*} @{n}

/def sys_warning=\
    /echo -p @{Cbgred,BCyellow}[@{n}@{Cbgred,BCwhite}@{u}FareanSystem@{n}@{Cbgred,BCyellow}]@{n}@{Cbgred,BCwhite}: %{*} @{n}


/def list_contains=\
     /let listname=%{1}%;\
     /let item=%{2}%;\
     /let size=$[get_val({listname})]%;\
     /if (!size) \
         /let size=0%;\
     /endif%;\
     /let i=1%;\
     /while (i <= size)   \
          /let listvalue=$[get_val(strcat({listname},"_",{i}))]%;\
          /if (listvalue =~ item) \
              /result i%;\
          /endif%;\
          /let i=$[i + 1]%;\
     /done%;\
     /return -1

/def add_item=\
     /if ( getopts("l:i:r#","") ) \
         /let listname=%{opt_l}%;\
         /if (listname !~ "") \
             /let item=%{opt_i}%;\
             /if (item !~ "") \
                 /if (%{opt_r} | $[list_contains({listname},{item})]<0) \
                   /let size=$[get_val({listname})]%;\
                   /if (!size) \
                       /let size=0%;\
                   /endif %;\
                   /let size $[{size}+1]%;\
                   /set %{listname}_%{size}=%{item}%;\
                   /set %{listname}=$[{size}]%;\
                   /result 1%;\
                 /endif%;\
             /else \
                  /sys_warning /add_item: ERROR: item unspecified!%;\
                  /sys_warning Usage: /add_item -l(list-string) -i(item-string)%;\
             /endif %;\
         /else \
               /sys_warning add_item: ERROR: list unspecified!%;\
               /sys_warning Usage: /add_item -l(list-string) -i(item-string)%;\
         /endif %;\
     /else \
           /sys_warning add_item: adds an item to a list, does not allow duplicates.%;\
           /sys_warning Usage: /add_item -l(list-string) -i(item-string)%;\
     /endif%;\
     /return 0

/def clear_list=\
     /if (getopts("l:","")) \
         /let listname=%{opt_l}%;\
         /if (listname !~ "") \
             /let size=$[get_val({listname})]%;\
             /if (size) \
               /let i=1%;\
               /while (i <= size) \
                      /let name=$[strcat({listname},"_",{i})]%;\
                      /unset %{name}%;\
                      /let i=$[i + 1]%;\
               /done%;\
               /set %{listname} 0%;\
               /result 1%;\
             /endif%;\
         /else \
               /sys_warning clear_list: ERROR: list unspecified!;\
               /sys_warning Usage: /clear_list -l(list-string);\
         /endif%;\
     /else \
           /sys_warning clear_list: clears a list to size of 1.%;\
           /sys_warning Usage: /clear_list -l(list-string)%;\
     /endif%;\
     /return 0


/def destroy_list=\
     /if (getopts("l:","")) \
         /let listname=%{opt_l}%;\
         /if (listname !~ "") \
             /let size=$[get_val({listname})]%;\
             /if (size) \
               /let i=1%;\
               /while (i <= size) \
                      /let name=$[strcat({listname},"_",{i})]%;\
                      /unset %{name}%;\
                      /let i=$[i + 1]%;\
               /done%;\
               /unset %{listname}%;\
               /result 1%;\
             /endif%;\
         /else \
               /sys_warning destroy_list: ERROR: list unspecified!%;\
               /sys_warning Usage: /destroy_list -l(list-string)%;\
         /endif%;\
     /else \
           /sys_warning destroy_list: destroys a list and its contents.%;\
           /sys_warning Usage: /destroy_list -l(list-string)%;\
     /endif%;\
     /return 0

/def remove_item=\
     /if (getopts("l:i:","")) \
         /let listname=%{opt_l}%;\
         /if (listname !~ "") \
             /let item=%{opt_i}%;\
             /if (item !~ "") \
                 /let size=$[get_val({listname})]%;\
                 /let i=1%;\
                 /while (i <= size) \
                        /let currententry=$[get_val(strcat({listname},"_",{i}))]%;\
                        /if (currententry =~ item) \
                            /let j=%{i}%;\
                            /let size=$[{size}-1]%;\
                            /set %{listname} %{size}%;\
                            /while (j <= size)  \
                                   /let next=$[{j}+1]%;\
                                   /let currentname=$[strcat({listname},"_",{next})]%;\
                                   /let newvalue=$[get_val({currentname})]%;\
                                   /set %{listname}_%{j} %{newvalue}%;\
                                   /let j=$[j + 1]%;\
                            /done%;\
                            /let next $[{size}+1]%;\
                            /unset %{listname}_%{next}%;\
                            /result i%;\
                        /endif%;\
                        /let i=$[i+1]%;\
                 /done%;\
             /else \
                  /sys_warning remove_item: ERROR: item unspecified!%;\
                  /sys_warning Usage: /remove_item -l(list-string -i(item-string)%;\
             /endif%;\
         /else \
               /sys_warning remove_item: ERROR: list unspecified!%;\
               /sys_warning Usage: /remove_item -l(list-string -i(item-string)%;\
         /endif%;\
     /else \
           /sys_warning remove_item: removes an item from a list. If there are duplicates, only removes the first one.@{n}%;\
           /sys_warning Usage: /remove_item -l(list-string -i(item-string)%;\
     /endif%;\
     /return 0

/def list_item_at=\
     /if (getopts("l:i#","")) \
         /let listname=%{opt_l}%;\
         /if (listname !~ "") \
             /let index=%{opt_i}%;\
             /let size=$[get_val({listname})]%;\
             /if (index > 0 & index <= size) \
                 /result $[get_val(strcat({listname},"_",{index}))]%;\
             /else \
                   /sys_warning Item number out of range. Max is %{size}.%;\
             /endif%;\
         /else    \
              /sys_warning list_item_at: ERROR: list unspecified!%;\
              /sys_warning Usage: /list_item_at -l(list-string) -i(item-integer)%;\
         /endif%;\
     /else \
           /sys_warning list_item_at: returns an item index (i) from list (l).%;\
           /sys_warning Usage: /list_item_at -l(list-string) -i(item-integer)%;\
     /endif%;\
     /return 0