merg list items

153 views
Skip to first unread message

max

unread,
Jan 2, 2024, 7:30:15 AMJan 2
to The Ring Programming Language
Hallo,

Does anyone know how the list works internally? I want to merge the elements of two lists without using for loop.  i want to save time because h

aList_1 = [1,2,3,4,5]

aList_2 = [6,7,8,9,10]


list3 = aList_1 + aList_2


? list2code(list3)


output: 


[
1,
2,
3,
4,
5,
[
6,
7,
8,
9,
10
]



i want to look like 


[
1,
2,
3,
4,
5,
6,
7,
8,
9,
10


Ilir Liburn

unread,
Jan 2, 2024, 8:10:59 AMJan 2
to The Ring Programming Language

Hello Max,

please note,  list3 = aList_1 + aList_2  effectively

1. adds aList_2 to aList_1
2. copies aList_1 to list3

aList_1 + aList_2  is only what you need to add one list to other

Unfortunately, what you are asking is not possible (to my knowledge). Ring is missing

aList1 += aList2 // add list content, do not add list

However, you can do following

1. if your list contains numbers in range 0-255, you can use ASCIIList2Str() to get string from each list, concatenate you strings, and generate final list by using Str2ASCIIList()

2. otherwise, use List2Code (which also works with strings) to get code for each list, concatenate code by removing ] at the end or [ at the beginning of the next middle code and generate list using eval.

Point 2 is (very) slow, so it might be faster to concatenate elements.

Greetings,
Ilir

Mansour Ayouni

unread,
Jan 2, 2024, 8:40:16 AMJan 2
to Ilir Liburn, The Ring Programming Language
Hello Max,

Using SoftanzaLib, it's easy to merge any number of lists, via the Merge() function, like this:

image.png

PS: SoftanzaLib is under heavy testing, and it's not available right now.

All the best,
Mansour

--

---
You received this message because you are subscribed to the Google Groups "The Ring Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ring-lang+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ring-lang/b1dca332-6495-49d4-9a0b-38db8c030892n%40googlegroups.com.

max

unread,
Jan 2, 2024, 9:37:45 AMJan 2
to The Ring Programming Language
Hallo  Ilir , Mahmoud

Ilir  thanks for the tips.

I'm not quite sure, but I assume that the list in ring internally use linked list, where each element points to the other element.  

and something like this statement
aList1 += aList2

will allow the last element from the first list to point to the first element on the second list with just a pointer without copying elements and generating new ones.

This also allows multiple threads to participate in the generation of a list

instead of

aList = []
for x = 1 to 1000_000
        aList + x
next

we can create 10 threads and each thread creates a part of the list and at the end each list part points with pointer to the other part

thread 1

aList_1 = []

for a = 1 to 500_000
        aList_1 + a
next

-------------------------------
thread 2

aList_2 = []

for x = 500_000 to 1000_000
        aList_2 + x
next

----------------------- at the end

aList_1 += aList_2 ( with pointer )

aList_1 (last item) ----> aList_2 (first item) (with poiter)

this will make the ring list very fast and strong

Greetings,
max

Ilir Liburn

unread,
Jan 2, 2024, 10:06:44 AMJan 2
to The Ring Programming Language
Hello Max,

list elements are always copied, never will element of one list point to element of another list. That can only happen during swap of elements inside one list.

With respect to threads: you can't add elements from different threads to the same list without synchronization. Instead, you should use jagged array of lists where each thread operates on its own list. And then you swap such list for example with parent list (can be done by parent or thread - check wavingcubes_threads sample in official Ring or wawingcubes_nosync sample in Ring2B).

Greetings,
Ilir

Bert Mariani

unread,
Jan 2, 2024, 1:12:39 PMJan 2
to The Ring Programming Language
Hello Max

You will need a "MergeSort" routine to sort the different values
 MergeSort is much faster than other sorts.
Create  List-C ,  then add each element of List-A  and then add each element of List-B 

See -- "C:\ring\samples\Drawing\VisualSort\Visualize-Sort.ring"
             Func MergeSortNR(values)     (NR means Non-Recursive)

max

unread,
Jan 3, 2024, 8:09:09 PMJan 3
to The Ring Programming Language
Hallo,

here is my experience in writing a c function to merge the elements.

void ring_vm_listfuncs_mergelist(void* pPointer)
{
    List* pList, * pList2, * pList3;
    Item* pItem;
    VM* pVM;
    pVM = (VM*)pPointer;
    if (RING_API_PARACOUNT != 2) {
        RING_API_ERROR(RING_API_MISS2PARA);
        return;
    }
    if (RING_API_ISLIST(1) && RING_API_ISLIST(2)) {

        pList  = RING_API_GETLIST(1);

        pList3 = RING_API_GETLIST(2);

        /* Avoid Objects */
        if (ring_vm_oop_isobject(pList) && ring_vm_oop_isobject(pList3)) {
            ring_vm_error(pVM, RING_VM_ERROR_VARISNOTLIST);
            return;
        }
        /* Check the Range */
        if (ring_list_getsize(pList) == UINT_MAX) {
            RING_API_ERROR(RING_API_RANGEEXCEEDED);
            return;
        }

       
        for (size_t x = 1; x <= ring_list_getsize(pList3); x++)
        {
            if (ring_list_isdouble(pList3, x)) {

                ring_list_adddouble_gc(pVM->pRingState, pList, ring_list_getdouble(pList3, x));
            }

            else if (ring_list_isstring(pList3,x)) {
               
                ring_list_addstring2_gc(pVM->pRingState, pList, ring_list_getstring(pList3, x), ring_list_getstringsize(pList3, x));
            }

            else if (ring_list_islist(pList3, x)) {

                pList2 = ring_list_getlist(pList3, x);

                ring_vm_addlisttolist(pVM, pList2, pList);

            }


        }

       
    }
    else {
        RING_API_ERROR(RING_API_BADPARATYPE);
    }
}


//-----------------------------------------------------

list1 = 1:5000_000

list2 = 5000_000:10000_000

? "hallo"

? len(list1)

t1 = clock()

mergelist(list1,list2)


? "Time : " + ((clock()-t1)/clockspersecond()) + " seconds"


? len(list1)

list1 = 1:5000_000

list2 = 5000_000:10000_000

nmax = len(list2)

t1 = clock()

for x in list2

list1 + x
next

? "Time : " + ((clock()-t1)/clockspersecond()) + " seconds"




list1 = 1:5000_000

list2 = 5000_000:10000_000

nmax = len(list2)

t1 = clock()

for x = 1 to nmax

list1 + list2[x]
next

? "Time : " + ((clock()-t1)/clockspersecond()) + " seconds"



//------- output

hallo
5000000
Time : 0.92 seconds
10000001
Time : 4.10 seconds
Time : 3.01 seconds

Ilir Liburn

unread,
Jan 4, 2024, 3:13:23 AMJan 4
to The Ring Programming Language
Hello Max,

Congratulations, you made Ring extension. It is good to have someone knowing Ring internals and willing to work on extensions.

Here is what you need to change:

1. if (ring_vm_oop_isobject(pList) && ring_vm_oop_isobject(pList3)) {

you should use || here because both list are not allowed to be objects

2. if (ring_list_getsize(pList) == UINT_MAX) {

this should be inside the loop because size can't be UINT_MAX until list starts expansion.

Greetings,
Ilir

max

unread,
Jan 4, 2024, 12:53:49 PMJan 4
to The Ring Programming Language
Hello Illir,

thanks for your tips. that makes more sense.

There is another function "  ring_vm_listfuncs_add "   in the ring that contains the following  

        else if (RING_API_ISLIST(2)) {
            pList2 = RING_API_GETLIST(2);
            if (ring_list_isref(pList2)) {
                ring_list_insertlist(pList, ring_list_getsize(pList));
                pItem = ring_list_getitem(pList, ring_list_getsize(pList));
                ring_list_assignreftoitem_gc(pVM->pRingState, pList2, pItem);
            }

            else {
                ring_vm_addlisttolist(pVM, pList2, pList);
            }
        }

Should I also consider the following?

Ilir Liburn

unread,
Jan 4, 2024, 2:24:34 PMJan 4
to max, The Ring Programming Language
Hello Max,

Unfortunately, Yes. Because in ring_vm_addlisttolist,
lAddSubListsByMove and lAddSubListsByFastCop are misplaced in front of
the check for.reference.

BTW, you don't need to move UINT_MAX check inside the loop if you do

if ( ( ring_list_getsize(pList) + ring_list_getsize(pList3) ) <
UINT_MAX ) // overflow

Greetings,
Ilir
>>>> You will need a *"MergeSort"* routine to sort the different values
>>>> MergeSort is much faster than other sorts.
>>>> Create List-C , then add each element of List-A and then add each
>>>> element of List-B
>>>>
>>>> See -- "C:\ring\samples\Drawing\VisualSort\*Visualize-Sort.ring*"
>>>> * Func MergeSortNR(values)* (NR means Non-Recursive)
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "The Ring Programming Language" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ring-lang+...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ring-lang/6696011b-80e3-4324-be2d-b8fa1f89f88cn%40googlegroups.com.
>

Ilir Liburn

unread,
Jan 4, 2024, 2:46:19 PMJan 4
to max, The Ring Programming Language
Hello Max,

sorry, my mistake. You need something like

if ( (RING_UNSIGNEDLONGLONG)( ring_list_getsize(pList) +
ring_list_getsize(pList3) ) >
UINT_MAX )

Safe way is: do check for equality inside the loop.

Greetings,
Ilir

max

unread,
Jan 5, 2024, 1:08:26 PMJan 5
to The Ring Programming Language
Hello Illir,

thanks for your help.

Greetings,
max

Ilir Liburn

unread,
Jan 5, 2024, 4:04:31 PMJan 5
to max, The Ring Programming Language
Hello Max,

You're Welcome.

Greetings,
Ilir
> You received this message because you are subscribed to a topic in the
> Google Groups "The Ring Programming Language" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ring-lang/9_GpzUnH-l4/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> ring-lang+...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ring-lang/601f4bc0-d38f-4a44-af3e-8fe884f3cecdn%40googlegroups.com.
>

Mahmoud Fayed

unread,
Jan 23, 2024, 10:11:46 PMJan 23
to The Ring Programming Language
Hello Max

>> "I want to merge the elements of two lists without using for loop"

This feature is added to the Add() function in Ring 1.20 in this commit: Update Visual Source (Using PWCT) - Add() function - Support a third … · ring-lang/ring@095570d (github.com)


Greetings,
Mahmoud
Reply all
Reply to author
Forward
0 new messages