yast2-core
Macros | Functions | Variables
YCPBuiltinList.cc File Reference
#include <set>
#include <algorithm>
#include "ycp/YCPBuiltinList.h"
#include "ycp/YCPList.h"
#include "ycp/YCPMap.h"
#include "ycp/YCPSymbol.h"
#include "ycp/YCPString.h"
#include "ycp/YCPBoolean.h"
#include "ycp/YCPInteger.h"
#include "ycp/YCPVoid.h"
#include "ycp/YCPCode.h"
#include "ycp/YCPCodeCompare.h"
#include "ycp/YCPTerm.h"
#include "ycp/StaticDeclaration.h"
#include "ycp/y2log.h"

Macros

#define ETC   0, NULL, constTypePtr(), NULL
 
#define ETCf   NULL, constTypePtr(), NULL
 

Functions

static YCPValue l_find (const YCPSymbol &symbol, const YCPList &list, const YCPCode &expr)
 
static YCPValue l_prepend (const YCPList &list, const YCPValue &value)
 
static YCPValue l_contains (const YCPList &list, const YCPValue &value)
 
static YCPValue l_setcontains (const YCPList &list, const YCPValue &value)
 
static YCPValue l_unionlist (const YCPList &list1, const YCPList &list2)
 
static YCPValue l_mergelist (const YCPList &list1, const YCPList &list2)
 
static YCPValue l_sublist1 (const YCPList &list, const YCPInteger &offset)
 
static YCPValue l_sublist2 (const YCPList &list, const YCPInteger &offset, const YCPInteger &length)
 
static YCPValue l_filter (const YCPSymbol &symbol, const YCPList &list, const YCPCode &expr)
 
static YCPValue l_maplist (const YCPSymbol &symbol, const YCPList &list, const YCPCode &expr)
 
static YCPValue l_listmap (const YCPSymbol &symbol, const YCPList &list, const YCPCode &expr)
 
static YCPValue l_flatten (const YCPList &list)
 
static YCPValue l_toset (const YCPList &list)
 
static YCPValue l_sortlist (const YCPList &list)
 
static YCPValue l_sort (const YCPValue &sym1, const YCPValue &sym2, const YCPList &list, const YCPCode &order)
 
static YCPValue l_lsortlist (const YCPList &list)
 
static YCPValue l_splitstring (const YCPString &s, const YCPString &c)
 
static YCPValue l_changelist (YCPList &list, const YCPValue &value)
 
static YCPValue l_add (const YCPValue &list, const YCPValue &value)
 
static YCPValue l_isempty (const YCPList &l)
 
static YCPValue l_size (const YCPValue &list)
 
static YCPValue l_remove (const YCPList &list, const YCPInteger &i)
 
static YCPValue l_select (const YCPValue &list, const YCPValue &i, const YCPValue &def)
 
static YCPValue l_foreach (const YCPValue &sym, const YCPList &list, const YCPCode &expr)
 
static YCPValue l_reduce_helper (const YCPSymbol &x, const YCPSymbol &y, const YCPValue &initial, const YCPList::const_iterator &first, const YCPList::const_iterator &last, const YCPCode &expr)
 
static YCPValue l_reduce1 (const YCPSymbol &x, const YCPSymbol &y, const YCPList &list, const YCPCode &expr)
 
static YCPValue l_reduce2 (const YCPSymbol &x, const YCPSymbol &y, const YCPValue &initial, const YCPList &list, const YCPCode &expr)
 
static YCPValue l_swaplist (const YCPList &v, YCPInteger &i1, YCPInteger &i2)
 
static YCPValue l_reverselist (const YCPList &v)
 
static YCPValue l_tolist (const YCPValue &v)
 

Variables

StaticDeclaration static_declarations
 

Macro Definition Documentation

#define ETC   0, NULL, constTypePtr(), NULL
#define ETCf   NULL, constTypePtr(), NULL

Function Documentation

static YCPValue l_add ( const YCPValue list,
const YCPValue value 
)
static

add add-list Create a new list with a new element

Parameters
listLIST
anyVAR
Returns
list The new list Creates a new list that is identical to the list LIST but has the value VAR appended as additional element.

add ([1, 4], 8) -> [1, 4, 8]

References YCPElement::isNull(), and ycp2error.

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_changelist ( YCPList list,
const YCPValue value 
)
static

change change-list Changes a list. Deprecated, use LIST[size(LIST)] = value.

Parameters
listLIST
anyvalue
Returns
list

Before Code 9, this was used to change a list directly without creating a copy. Now it is a synonym for add.

See Also
add change ([1, 4], 8) -> [1, 4, 8]

References YCPList::add(), YCPElement::isNull(), and ycpinternal.

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_contains ( const YCPList list,
const YCPValue value 
)
static

contains Checks if a list contains an element

Parameters
listLIST List
anyELEMENT Element
Returns
boolean True if element is in the list.

Determines, if a certain value ELEMENT is contained in a list LIST.

contains ([1, 2, 5], 2) -> true

References YCPList::contains().

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_filter ( const YCPSymbol symbol,
const YCPList list,
const YCPCode expr 
)
static

filter filter-list Filters a List

Parameters
anyVAR Variable
listLIST List to be filtered
block<boolean>EXPR Block
Returns
list For each element of the list LIST the expression expr is executed in a new block, where the variable VAR is assigned to that value. If the expression evaluates to true under this circumstances, the value is appended to the result list.

filter (integer v, [1, 2, 3, 5], { return (v > 2); }) -> [3, 5]

References YCPList::add(), YCPList::begin(), YCPList::end(), YCPElement::isNull(), and ycp2error.

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_find ( const YCPSymbol symbol,
const YCPList list,
const YCPCode expr 
)
static

find find-list Searches for the first occurence of a certain element in a list

Parameters
anyVAR
listLIST
blockEXPR

Searches for a certain item in the list. It applies the expression EXPR to each element in the list and returns the first element the makes the expression evaluate to true, if VAR is bound to that element.

Returns
any Returns nil, if nothing is found. find (integer n, [3,5,6,4], ``(n >= 5)) -> 5

References YCPList::begin(), YCPList::end(), YCPElement::isNull(), and ycp2error.

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_flatten ( const YCPList list)
static

flatten Flattens List

Parameters
list<list>LIST
Returns
list

Gets a list of lists LIST and creates a single list that is the concatenation of those lists in LIST.

flatten ([ [1, 2], [3, 4] ]) -> [1, 2, 3, 4] flatten ([ [1, 2], [6, 7], [3, 4] ]) -> [1, 2, 6, 7, 3, 4]

References YCPList::add(), YCPElement::isNull(), YCPList::size(), YCPList::value(), and ycp2error.

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_foreach ( const YCPValue sym,
const YCPList list,
const YCPCode expr 
)
static

foreach foreach-list Processes the content of a list

Parameters
anyVAR
listLIST
blockEXPR

For each element of the list LIST the expression EXPR is executed in a new context, where the variable VAR is assigned to that value. The return value of the last execution of EXPR is the value of the foreach construct.

Returns
any return value of last execution of EXPR foreach (integer v, [1,2,3], { return v + 10; }) -> 13

References YCPList::begin(), YCPList::end(), YCPElement::isNull(), and ycp2error.

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_isempty ( const YCPList l)
static

isempty isempty-list Returns whether the list l is empty.

Parameters
listl List
Returns
boolean Emptiness of list l

Notice that the list l must not be nil.

isempty([]) -> true isempty([19, 29]) -> false

References YCPList::isEmpty().

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_listmap ( const YCPSymbol symbol,
const YCPList list,
const YCPCode expr 
)
static

listmap Maps an operation onto all elements of a list and thus creates a map.

Parameters
anyVAR
listLIST
blockEXPR
Returns
map

For each element VAR of the list LIST in the expression EXPR is evaluated in a new block. The result is the map of those evaluations.

The result of each evaluation must be a map with a single entry which will be added to the result map.

listmap (integer k, [1,2,3], { return $[k:"xy"]; }) -> $[1:"xy", 2:"xy"] listmap (integer k, [1,2,3], { integer a = k+10; any b = sformat ("x%1", k); return $[a:b]; }) -> $[11:"x1", 12:"x2", 13:"x3"]

References YCPMap::add(), YCPMap::begin(), YCPList::begin(), YCPList::end(), YCPElement::isNull(), YCPMap::size(), and ycp2error.

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_lsortlist ( const YCPList list)
static

lsort Sort A List respecting locale

Parameters
listLIST
Returns
list Sorted list Sort the list LIST according to the YCP builtin predicate >. Strings are compared using the current locale. Duplicates are not removed.

lsort (["česky", "slovensky", "německy", 2, 1]) -> [1, 2, "česky", "německy", "slovensky"]

References YCPElement::isNull(), and YCPList::lsortlist().

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_maplist ( const YCPSymbol symbol,
const YCPList list,
const YCPCode expr 
)
static

maplist maplist-list Maps an operation onto all elements of a list and thus creates a new list.

Parameters
anyVAR
list<any>LIST
blockEXPR
Returns
list<any>

For each element of the list LIST the expression EXPR is evaluated in a new block, where the variable VAR is assigned to that value. The result is the list of those evaluations.

maplist (integer v, [1, 2, 3, 5], { return (v + 1); }) -> [2, 3, 4, 6]

References YCPList::add(), YCPList::begin(), YCPList::end(), YCPElement::isNull(), and ycp2error.

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_mergelist ( const YCPList list1,
const YCPList list2 
)
static

merge Merges two lists into one

Parameters
listLIST1 First List
listLIST2 Second List
Returns
list

Interprets two lists as sets and returns a new list that has all elements of the first list and all of the second list. Identical elements are preserved. The order of the elements in the new list is preserved. Elements of l1 are prior to elements from l2. nil as either argument makes the result nil too.

See Also
union merge ([1, 2], [3, 4]) -> [1, 2, 3, 4] merge ([1, 2, 3], [2, 3, 4]) -> [1, 2, 3, 2, 3, 4]

References YCPList::add(), YCPElement::isNull(), YCPList::size(), and YCPList::value().

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_prepend ( const YCPList list,
const YCPValue value 
)
static

prepend Prepends a list with a new element

Parameters
listLIST List
anyELEMENT Element to prepend
Returns
list Creates a new list that is identical to the list LIST but has the value ELEMENT prepended as additional element.

prepend ([1, 4], 8) -> [8, 1, 4]

References YCPList::functionalAdd(), and YCPElement::isNull().

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_reduce1 ( const YCPSymbol x,
const YCPSymbol y,
const YCPList list,
const YCPCode expr 
)
static

list::reduce list.reduce-internal Reduces a list to a single value.

Parameters
flex1x
flex1y
list<flex1>list
block<flex1>expression
Returns
flex1

Apply expression cumulatively to the values of the list, from left to right, to reduce the list to a single value. See http://en.wikipedia.org/wiki/Reduce_(higher-order_function) for a detailed explanation.

In this version the initial value is the first value of the list. Thus the list must not be empty.

list::reduce (integer x, integer y, [2, 4, 6], { return x < y ? x : y; }) -> 2 list::reduce (integer x, integer y, [2, 4, 6], { return x > y ? x : y; }) -> 6

References YCPList::begin(), YCPList::end(), YCPList::isEmpty(), l_reduce_helper(), and ycp2error.

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_reduce2 ( const YCPSymbol x,
const YCPSymbol y,
const YCPValue initial,
const YCPList list,
const YCPCode expr 
)
static

list::reduce list.reduce-external Reduces a list to a single value.

Parameters
flex1x
flex2y
flex1value
list<flex2>list
block<flex1>expression
Returns
flex1

Apply expression cumulatively to the values of the list, from left to right, to reduce the list to a single value. See http://en.wikipedia.org/wiki/Reduce_(higher-order_function) for a detailed explanation.

In this version the initial value is explicitly provided. Thus the list can be empty. Also the return type can be different from the type of the list.

list::reduce (integer x, integer y, 0, [2, 4, 6], { return x + y; }) -> 12 list::reduce (integer x, integer y, 1, [2, 4, 6], { return x * y; }) -> 48

list::reduce (term t, float f, item(id(<tt>dummy)), [3.14, 2.71], { return add(t, tostring(f)); }) ->item (id (dummy), "3.14", "2.71")

References YCPList::begin(), YCPList::end(), and l_reduce_helper().

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_reduce_helper ( const YCPSymbol x,
const YCPSymbol y,
const YCPValue initial,
const YCPList::const_iterator first,
const YCPList::const_iterator last,
const YCPCode expr 
)
static

References YCPElement::isNull(), and ycp2error.

Referenced by l_reduce1(), and l_reduce2().

static YCPValue l_remove ( const YCPList list,
const YCPInteger i 
)
static

remove remove-list Removes element from a list

Parameters
listLIST
integere element index
Returns
list Returns unchanged list if the index is invalid. Removes the i'th value from a list. The first value has the index 0. The call remove ([1,2,3], 1) thus returns [1,3].

The yast2-core version < 2.17.16 returns nil if the key is invalid. This behavior has changed in version 2.17.16 to return unchanged list.

remove ([1, 2], 0) -> [2]

References YCPElement::isNull(), YCPList::remove(), YCPList::size(), toString(), and ycp2error.

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_reverselist ( const YCPList v)
static

list::reverse list.reverse Creates new list with reversed order of elements.

Parameters
list<flex1>v list
Returns
New reversed list.

Creates new list with reversed order of elements. Return nil if list is nil.

list::reverse ([0,1,2,3]) -> [3,2,1,0] list::reverse ([]) -> []

References YCPElement::isNull(), YCPList::reverse(), and ycp2error.

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_select ( const YCPValue list,
const YCPValue i,
const YCPValue def 
)
static

select select-list Selects a list element (deprecated, use LIST[INDEX]:DEFAULT)

Parameters
listLIST
integerINDEX
anyDEFAULT
Returns
any

Gets the INDEX'th value of a list. The first value has the index 0. The call select([1,2,3], 1) thus returns 2. Returns DEFAULT if the index is invalid or if the found entry has a different type than the default value. Functionality replaced by syntax: list numbers = [1, 2, 3, 4]; numbers[2]:nil -> 3 numbers[8]:5 -> 5

select ([1, 2], 22, 0) -> 0 select ([1, "two"], 0, "no") -> "no"

References YCPElement::isNull(), t_select(), and ycp2error.

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_setcontains ( const YCPList list,
const YCPValue value 
)
static

setcontains Checks if a sorted list contains an element

Parameters
listLIST List
anyELEMENT Element
Returns
boolean True if element is in the list. Determines, if a certain value ELEMENT is contained in a list LIST, but assumes that LIST is sorted. If LIST is not sorted, the result is undefined.

setcontains ([1, 2, 5], 2) -> true

References YCPList::begin(), and YCPList::end().

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_size ( const YCPValue list)
static

size size-list Returns size of list

Parameters
listLIST
Returns
integer size of the list

Returns the number of elements of the list LIST

size(["A", 1, true, "3", false]) -> 5

References YCPElement::isNull().

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_sort ( const YCPValue sym1,
const YCPValue sym2,
const YCPList list,
const YCPCode order 
)
static

sort sort-expr Sort list using an expression

Parameters
anyx
anyy
listLIST
blockEXPR
Returns
list

Sorts the list LIST. You have to specify an order on the list elements by naming formal variables x and y and specify an expression EXPR that evaluates to a boolean value depending on x and y. Return true if x>y to sort the list ascending.

The comparison must be an irreflexive one, that is ">" instead of ">=".

It is because we no longer use bubblesort (yuck) but std::sort which requires a <ulink url="href="http://www.sgi.com/tech/stl/StrictWeakOrdering.html">strict weak ordering</ulink>.

sort (integer x, integer y, [ 3,6,2,8 ], (x < y)) -> [ 2, 3, 6, 8 ] @usage sort (string x, string y, [ "A","C","B" ],(x > y)) -> ["C", "B", "A"]

References YCPList::fsortlist(), and YCPElement::isNull().

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_sortlist ( const YCPList list)
static

sort sort-lt Sorts a List according to the YCP builtin predicate

Parameters
listLIST
Returns
list Sorted list

Sorts the list LIST according to the YCP builtin predicate. Duplicates are not removed.

sort ([2, 1, true, 1]) -> [true, 1, 1, 2]

References YCPElement::isNull(), and YCPList::sortlist().

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_splitstring ( const YCPString s,
const YCPString c 
)
static

splitstring Split a string by delimiter

Parameters
stringSTR
stringDELIM
Returns
list<string>

Splits STR into sub-strings at delimiter chars DELIM. the resulting pieces do not contain DELIM

If STR starts with DELIM, the first string in the result list is empty If STR ends with DELIM, the last string in the result list is empty. If STR does not contain DELIM, the result is a singleton list with STR.

See Also
mergestring splitstring ("/abc/dev/ghi", "/") -> ["", "abc", "dev", "ghi" ] splitstring ("abc/dev/ghi/", "/") -> ["abc", "dev", "ghi", "" ] splitstring ("abc/dev/ghi/", ".") -> ["abc/dev/ghi/" ] splitstring ("text/with:different/separators", "/:") -> ["text", "with", "different", "separators"]

References YCPList::add(), YCPElement::isNull(), and ycp2error.

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_sublist1 ( const YCPList list,
const YCPInteger offset 
)
static

sublist sublist-rest Extracts a sublist

Extracts a sublist of the list LIST starting at OFFSET. The OFFSET starts with 0.

Parameters
listLIST
integerOFFSET
Returns
list

sublist ([ "a", "b", "c"], 0) -> [ "a", "b", "c" ] sublist ([ "a", "b", "c"], 2) -> [ "c" ]

References YCPList::add(), YCPElement::isNull(), YCPList::size(), toString(), YCPList::value(), and ycp2error.

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_sublist2 ( const YCPList list,
const YCPInteger offset,
const YCPInteger length 
)
static

sublist sublist-length Extracts a sublist

Extracts a sublist of the list LIST starting at OFFSET with length LENGTH. The OFFSET starts with 0.

Parameters
listLIST
integerOFFSET
integerLENGTH
Returns
list

sublist ([ "a", "b", "c"], 0, 2) -> [ "a", "b" ] sublist ([ "a", "b", "c"], 1, 1) -> [ "b" ]

References YCPList::add(), YCPElement::isNull(), YCPList::size(), toString(), YCPList::value(), and ycp2error.

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_swaplist ( const YCPList v,
YCPInteger i1,
YCPInteger i2 
)
static

list::swap list.swap Creates new list with swaped elements at offset i1 and i2.

Parameters
list<flex1>v list
integeri1 index of first element
integeri2 index of second element
Returns
New list. Changed if offset is correct, otherwise return unchanged list

Creates new list with swaped elemetns at offset i1 and i2. Return nil if list or any offset is nil. Return unchanged list if any offset is out of bounds.

list::swap ([0,1,2,3],0,2) -> [2,1,0,3] list::swap ([0,1,2,3],1,3) -> [0,3,2,1] list::swap ([0,1,2,3],0,4) -> [0,1,2,3]

References YCPElement::isNull(), YCPList::size(), YCPList::swap(), and ycp2error.

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_tolist ( const YCPValue v)
static

tolist Converts a value to a list (deprecated, use (list)VAR).

Parameters
anyVAR
Returns
list

If the value can't be converted to a list, nillist is returned. Functionality replaced by retyping: any l_1 = [1, 2, 3]; list <integer> l_2 = (list<integer>) l_1;

References YCPElement::isNull(), and YT_LIST.

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_toset ( const YCPList list)
static

toset Sorts list and removes duplicates

Parameters
listLIST
Returns
list Sorted list with unique items Scans a list for duplicates, removes them and sorts the list.

toset ([1, 5, 3, 2, 3, true, false, true]) -> [false, true, 1, 2, 3, 5]

References YCPList::begin(), and YCPList::end().

Referenced by YCPBuiltinList::YCPBuiltinList().

static YCPValue l_unionlist ( const YCPList list1,
const YCPList list2 
)
static

union union-list Unions of lists

Parameters
listLIST1 First List
listLIST2 Second List
Returns
list

Interprets two lists as sets and returns a new list that has all elements of the first list and all of the second list. Identical elements are merged. The order of the elements in the new list is preserved. Elements of l1 are prior to elements from l2. nil as either argument makes the result nil too.

WARNING: quadratic complexity so far

See Also
merge union ([1, 2], [3, 4]) -> [1, 2, 3, 4] union ([1, 2, 3], [2, 3, 4]) -> [1, 2, 3, 4] union ([1, 3, 5], [1, 2, 4, 6]) -> [1, 3, 5, 2, 4, 6]

References YCPList::add(), YCPList::contains(), YCPElement::isNull(), YCPList::size(), and YCPList::value().

Referenced by YCPBuiltinList::YCPBuiltinList().

Variable Documentation

StaticDeclaration static_declarations

Generated on a sunny day for yast2-core by doxygen 1.8.5