Author Topic: Structured list: index or not?  (Read 2451 times)

0 Members and 1 Guest are viewing this topic.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Structured list: index or not?
« on: December 10, 2017, 05:45:15 AM »
Because, as usual, I did a little 'of confusion with my previous post "Puzzle: flat list to nested indexed list", I will try to write as little as possible by just giving examples.
(I hope the subject of the discussion is correct)

My question is:
is the list #Info6_NO_Index or #Info6_Index better?
It is better to get the data through the position (nth) or through assoc?

Code: [Select]
(defun Dxf (DxfCod EntDat)  (cdr (assoc DxfCod EntDat)))

(DXF 1 (cdr (DXF 1 (cdr (DXF 1 #Info6_Index)))))    => ("Tropicana" "Vitamin E" "Protein 7" "1.1")
(DXF 2 (cdr (DXF 2 (cdr (DXF 1 #Info6_Index)))))    => (nil)

(nth 1 (cdr (nth 1 (cdr (nth 1 #Info6_NO_Index))))) => ("Tropicana" "Vitamin E" "Protein 7" "1.1")
(nth 2 (cdr (nth 2 (cdr (nth 1 #Info6_NO_Index))))) => (nil)
Code: [Select]
(setq #Info6_NO_Index '(
("SubGroup 0"
  ("Class 00"
    ("SubClass 000" "000 DescriptionA" "000 DescriptionB" "000 Measure")
    ("SubClass 001" "001 DescriptionA" "001 DescriptionB" "001 Measure")
    ("SubClass 002" "002 DescriptionA" "002 DescriptionB" "002 Measure")
  )
  ("Class 01"
    ("SubClass 010" "010 DescriptionA" "010 DescriptionB" "010 Measure")
    ("SubClass 011" "011 DescriptionA" "011 DescriptionB" "011 Measure")
    ("SubClass 012" "012 DescriptionA" "012 DescriptionB" "012 Measure")
  )
  ("Class 02"
    ("SubClass 020" "020 DescriptionA" "020 DescriptionB" "020 Measure")
    ("SubClass 021" "021 DescriptionA" "021 DescriptionB" "021 Measure")
    ("SubClass 022" "022 DescriptionA" "022 DescriptionB" "022 Measure")
  )
)
("Fruits"
  ("Apple"
    ("Winesap"      "Vitamin A" "Protein 2" "5.5")
    ("McIntosh"     "Vitamin B" "Protein 3" "7.6")
    ("Granny Smith" "Vitamin C" "Protein 1" "2.8")
  )
  ("Orange"
    ("Valencia"     "Vitamin D" "Protein 5" "2.2")
    ("Tropicana"    "Vitamin E" "Protein 7" "1.1")
    ("Generic"      "Vitamin F" "Protein 9" "1.9")
  )
  (nil 
    (nil)
    (nil)
    (nil)
  )
)
("SubGroup 2"
  ("Class 20"
    ("SubClass 200" "200 DescriptionA" "200 DescriptionB" "200 Measure")
    ("SubClass 201" "201 DescriptionA" "201 DescriptionB" "201 Measure")
    ("SubClass 202" "202 DescriptionA" "202 DescriptionB" "202 Measure")
  )
  ("Class 21"
    ("SubClass 210" "210 DescriptionA" "210 DescriptionB" "210 Measure")
    ("SubClass 211" "211 DescriptionA" "211 DescriptionB" "211 Measure")
    ("SubClass 212" "212 DescriptionA" "212 DescriptionB" "212 Measure")
  )
  ("Class 22"
    ("SubClass 220" "220 DescriptionA" "220 DescriptionB" "220 Measure")
    ("SubClass 221" "221 DescriptionA" "221 DescriptionB" "221 Measure")
    ("SubClass 222" "222 DescriptionA" "222 DescriptionB" "222 Measure")
  )
)
))
(setq #Info6_Index '(
(0 "SubGroup 0"
  (0 "Class 00"
    (0 "SubClass 000" "000 DescriptionA" "000 DescriptionB" "000 Measure")
    (1 "SubClass 001" "001 DescriptionA" "001 DescriptionB" "001 Measure")
    (2 "SubClass 002" "002 DescriptionA" "002 DescriptionB" "002 Measure")
  )
  (1 "Class 01"
    (0 "SubClass 010" "010 DescriptionA" "010 DescriptionB" "010 Measure")
    (1 "SubClass 011" "011 DescriptionA" "011 DescriptionB" "011 Measure")
    (2 "SubClass 012" "012 DescriptionA" "012 DescriptionB" "012 Measure")
  )
  (2 "Class 02"
    (0 "SubClass 020" "020 DescriptionA" "020 DescriptionB" "020 Measure")
    (1 "SubClass 021" "021 DescriptionA" "021 DescriptionB" "021 Measure")
    (2 "SubClass 022" "022 DescriptionA" "022 DescriptionB" "022 Measure")
  )
)
(1 "Fruits"
  (0 "Apple"
    (0 "Winesap"      "Vitamin A" "Protein 2" "5.5")
    (1 "McIntosh"     "Vitamin B" "Protein 3" "7.6")
    (2 "Granny Smith" "Vitamin C" "Protein 1" "2.8")
  )
  (1 "Orange"
    (0 "Valencia"     "Vitamin D" "Protein 5" "2.2")
    (1 "Tropicana"    "Vitamin E" "Protein 7" "1.1")
    (2 "Generic"      "Vitamin F" "Protein 9" "1.9")
  )
  (2 nil 
    (0 nil)
    (1 nil)
    (2 nil)
  )
)
(2 "SubGroup 2"
  (0 "Class 20"
    (0 "SubClass 200" "200 DescriptionA" "200 DescriptionB" "200 Measure")
    (1 "SubClass 201" "201 DescriptionA" "201 DescriptionB" "201 Measure")
    (2 "SubClass 202" "202 DescriptionA" "202 DescriptionB" "202 Measure")
  )
  (1 "Class 21"
    (0 "SubClass 210" "210 DescriptionA" "210 DescriptionB" "210 Measure")
    (1 "SubClass 211" "211 DescriptionA" "211 DescriptionB" "211 Measure")
    (2 "SubClass 212" "212 DescriptionA" "212 DescriptionB" "212 Measure")
  )
  (2 "Class 22"
    (0 "SubClass 220" "220 DescriptionA" "220 DescriptionB" "220 Measure")
    (1 "SubClass 221" "221 DescriptionA" "221 DescriptionB" "221 Measure")
    (2 "SubClass 222" "222 DescriptionA" "222 DescriptionB" "222 Measure")
  )
)
))

ribarm

  • Gator
  • Posts: 3265
  • Marko Ribar, architect
Re: Structured list: index or not?
« Reply #1 on: December 10, 2017, 05:53:35 AM »
NO Index is shorter and no need for (assoc) - only (nth) is needed... And you can of course use (assoc) directly on needed "SubGroup?" to get its whole branch, just make sure list is correctly wrapped with brackets and you supply (assoc) on correct tree (root) to get its branch... IMHO...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Structured list: index or not?
« Reply #2 on: December 10, 2017, 07:46:36 AM »
Same as Marko, I'd prefer #Info6_NO_Index along with assoc as a list accessor, because its simplier.
Then you could make use of this subfunction.
 
But makes me ask, why would you want the index? To cover that, I'd use something like:

Code - Auto/Visual Lisp: [Select]
  1. ; Usage: just like (assoc)
  2. ; Returns: the associated item, where the key is substituted with the nth position found.
  3. (defun _assoc+nth ( k L / i )
  4.   (setq i -1) (vl-some (function (lambda (x) (setq i (1+ i)) (if (= (car x) k) (cons i (cdr x))))) L)
  5. )

So then:
Code - Auto/Visual Lisp: [Select]
  1. _$ (_assoc+nth "Fruits" #Info6_NO_Index)
  2. (1
  3.   ("Apple"
  4.     ("Winesap" "Vitamin A" "Protein 2" "5.5")
  5.     ("McIntosh" "Vitamin B" "Protein 3" "7.6")
  6.     ("Granny Smith" "Vitamin C" "Protein 1" "2.8")
  7.   )
  8.   ("Orange"
  9.     ("Valencia" "Vitamin D" "Protein 5" "2.2")
  10.     ("Tropicana" "Vitamin E" "Protein 7" "1.1")
  11.     ("Generic" "Vitamin F" "Protein 9" "1.9")
  12.   )
  13.   (nil (nil) (nil) (nil))
  14. )
  15.  
  16.  
  17. _$ (_assoc+nth "Orange" (cdr (_assoc+nth "Fruits" #Info6_NO_Index)))
  18. (1
  19.   ("Valencia" "Vitamin D" "Protein 5" "2.2")
  20.   ("Tropicana" "Vitamin E" "Protein 7" "1.1")
  21.   ("Generic" "Vitamin F" "Protein 9" "1.9")
  22. )
  23.  
  24.  
  25. _$ (_assoc+nth nil (cdr (_assoc+nth "Fruits" #Info6_NO_Index)))
  26. (2 (nil) (nil) (nil))
  27.  
 
Where the return is: [car = index] [cdr = branch],
and IMO since the 'key' is provided as an argument you won't need it in the return as you probably already stored it in a variable.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

VovKa

  • Water Moccasin
  • Posts: 1629
  • Ukraine
Re: Structured list: index or not?
« Reply #3 on: December 10, 2017, 08:55:23 AM »
It is better to get the data through the position (nth) or through assoc?
assoc'ed lists is one of the advantaged of lisp over other 'array oriented' languages
it makes lists much easier to manage
why deprive yourself of this?
i would assoc every atom in the list

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Structured list: index or not?
« Reply #4 on: December 10, 2017, 08:55:43 AM »
Marko & Grrr1337, if you look at all my examples I have never used the key to access the data but only the index:
Code: [Select]
(DXF 1 (cdr (DXF 1 (cdr (DXF 1 #Info6_Index)))))    => ("Tropicana" "Vitamin E" "Protein 7" "1.1")
(DXF 2 (cdr (DXF 2 (cdr (DXF 1 #Info6_Index)))))    => (nil)

(nth 1 (cdr (nth 1 (cdr (nth 1 #Info6_NO_Index))))) => ("Tropicana" "Vitamin E" "Protein 7" "1.1")
(nth 2 (cdr (nth 2 (cdr (nth 1 #Info6_NO_Index))))) => (nil)

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Structured list: index or not?
« Reply #5 on: December 10, 2017, 09:21:45 AM »
Marko & Grrr1337, if you look at all my examples I have never used the key to access the data but only the index:
...

Ah right, well then like Marko mentioned "only (nth) is needed".
But then why decide "index or not?" if we only use nth function and don't know how the result is processed later...

Is your question "What is better to use: assoc or nth?"
If my list doesn't depents on the keys, but rather subitem's positions then I'd use something like:
Code - Auto/Visual Lisp: [Select]
  1. ; (nnth '(1 2 0) ...)
  2. ; nested nths
  3. (defun nnth ( nL L ) (foreach n nL (setq L (nth n L))) L)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Structured list: index or not?
« Reply #6 on: December 10, 2017, 09:40:31 AM »
It is better to get the data through the position (nth) or through assoc?
assoc'ed lists is one of the advantaged of lisp over other 'array oriented' languages
it makes lists much easier to manage
why deprive yourself of this?
i would assoc every atom in the list
I agree with you even though I am surprised by this result:
Code: [Select]
(defun GetByAssoc (k1 k2 k3)
  (cdr (assoc k3 (cddr (assoc k2 (cddr (assoc k1 #Info6_Index))))))
)
(defun GetByNth (k1 k2 k3)
  (nth k3 (cdr (nth k2 (cdr (nth k1 #Info6_NO_Index)))))
)

Elapsed milliseconds / relative speed for 131072 iteration(s):
    (GETBYNTH 0 0 0).......1437 / 2.27 <fastest>
    (GETBYASSOC 0 0 0).....3266 / 1 <slowest>

Elapsed milliseconds / relative speed for 131072 iteration(s):
    (GETBYNTH 5 5 5).......1328 / 1.08 <fastest>
    (GETBYASSOC 5 5 5).....1437 / 1 <slowest>

Elapsed milliseconds / relative speed for 131072 iteration(s):
    (GETBYASSOC 9 9 9).....1375 / 1.03 <fastest>
    (GETBYNTH 9 9 9).......1421 / 1 <slowest>
   
Now I try the function of Grrr1337

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Structured list: index or not?
« Reply #7 on: December 10, 2017, 09:49:41 AM »
Is your question "What is better to use: assoc or nth?"
If my list doesn't depents on the keys, but rather subitem's positions then I'd use something like:
Code - Auto/Visual Lisp: [Select]
  1. ; (nnth '(1 2 0) ...)
  2. ; nested nths
  3. (defun nnth ( nL L ) (foreach n nL (setq L (nth n L))) L)
Code: [Select]
Comando: (nnth     '(2 2 2) #Info6_NO_Index)
("SubClass 211" "211 DescriptionA" "211 DescriptionB" "211 Measure")

Comando: (nnth     '(1 1 1) #Info6_NO_Index)
("Winesap" "Vitamin A" "Protein 2" "5.5")

Comando: (nnth     '(1 0 2) #Info6_NO_Index)
; errore: tipo di argomento errato: consp "Fruits

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Structured list: index or not?
« Reply #8 on: December 10, 2017, 10:03:59 AM »
Quick correction:

Code: [Select]
(defun nnth ( nL L )
  (while (and nL L (listp L))
    (setq L (nth (car nL) L)) (setq nL (cdr nL))
  )
  L
)

Code - Auto/Visual Lisp: [Select]
  1. _$ (nnth '(2 2 2) #Info6_NO_Index)
  2. ("SubClass 211" "211 DescriptionA" "211 DescriptionB" "211 Measure")
  3.  
  4. _$ (nnth '(1 1 1) #Info6_NO_Index)
  5. ("Winesap" "Vitamin A" "Protein 2" "5.5")
  6.  
  7. _$ (nnth '(1 0 2) #Info6_NO_Index)
  8. "Fruits"
  9.  
  10. _$ (nnth '(1 84 2) #Info6_NO_Index)
  11. nil
  12.  

But as I understood you prompt the user with DCL and then use this. - I'd prefer the simpliest approach,
regardless of the speed(cause no one would notice milisecs that are not used within big iterations[hence - summed]).

EDIT:  :idea: Something between the two:

Code: [Select]
(defun AssocOrNth ( anL L / n )
  (while (and anL L (listp L))
    (setq n (car anL))
    (setq L
      (cond
        ( (eq 'INT (type n)) (nth n (cdr L)) )
        ( (assoc n (cdr L)) )
      )
    )
    (setq anL (cdr anL))
  )
  L
)

Code - Auto/Visual Lisp: [Select]
  1. _$ (AssocOrNth '("Fruits" "Apple" 1) #Info6_NO_Index)
  2. ("McIntosh" "Vitamin B" "Protein 3" "7.6")
  3.  
  4. _$ (AssocOrNth '("Fruits" 1 "Tropicana") #Info6_NO_Index)
  5. ("Tropicana" "Vitamin E" "Protein 7" "1.1")

But that means no assoc for integer keys.
« Last Edit: December 10, 2017, 10:20:51 AM by Grrr1337 »
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Structured list: index or not?
« Reply #9 on: December 10, 2017, 11:38:41 AM »
Sorry but I agree with VovKa, with assoc'ed lists (for example) I can clean all empty data without loose the structure:

Code: [Select]
Example with full list of 1000 Class: 10 SubGroup - 100 Class - 1000 SubClass

Mediumly the density of data can be:
a) SubGroup = 10 > 1   empty
b) Classes = 100 > 15  empty in the not empty SubGroups
c) SubClasses = 1000 > 180 empty in the not empty SubGroups or Classes

So my SubClass are:
a) 1000 - 100 = 900
b) 900 - (15 x 10) = 750
c) 750 - 180 = 570