github.com/whatap/golib@v0.0.22/util/list/IntList.go (about)

     1  package list
     2  
     3  import (
     4  	//"bytes"
     5  	"fmt"
     6  	"math"
     7  	"sort"
     8  	"strconv"
     9  	"sync"
    10  
    11  	"github.com/whatap/golib/io"
    12  	"github.com/whatap/golib/lang/value"
    13  	"github.com/whatap/golib/util/compare"
    14  )
    15  
    16  type IntList struct {
    17  	size  int
    18  	table []int
    19  	lock  sync.Mutex
    20  }
    21  
    22  func NewIntListDefault() *IntList {
    23  	o := new(IntList)
    24  	o.table = make([]int, 0)
    25  	return o
    26  }
    27  
    28  func NewIntList(initialCapa int) *IntList {
    29  	o := new(IntList)
    30  	o.table = make([]int, initialCapa)
    31  	return o
    32  }
    33  
    34  func (this *IntList) GetType() byte {
    35  	return ANYLIST_INT
    36  }
    37  
    38  func (this *IntList) ensure(minCapacity int) {
    39  	if minCapacity > len(this.table) {
    40  		if this.table == nil {
    41  			minCapacity = int(math.Min(float64(ANYLIST_DEFAULT_CAPACITY), float64(minCapacity)))
    42  		}
    43  		oldSize := len(this.table)
    44  		newSize := oldSize + (oldSize >> 1)
    45  		if newSize < minCapacity {
    46  			newSize = minCapacity
    47  		}
    48  		if newSize > ANYLIST_MAX_SIZE {
    49  			panic("too big size")
    50  		}
    51  
    52  		newTable := make([]int, newSize)
    53  		copy(newTable, this.table)
    54  		this.table = newTable
    55  	}
    56  }
    57  
    58  func (this *IntList) get(i int) int {
    59  	if i >= this.size {
    60  		panic(fmt.Sprintln("Index out of bounds ", "Index: ", i, ", Size: ", this.size))
    61  	}
    62  	return this.table[i]
    63  }
    64  
    65  func (this *IntList) set(i, v int) int {
    66  	if i >= this.size {
    67  		panic(fmt.Sprintln("Index out of bounds Index: ", i, ", Size: ", this.size))
    68  	}
    69  
    70  	ov := this.table[i]
    71  	this.table[i] = v
    72  	return ov
    73  }
    74  
    75  func (this *IntList) remove(i int) int {
    76  	if i >= this.size {
    77  		panic(fmt.Sprintln("Index out of bounds Index: ", i, ", Size: ", this.size))
    78  	}
    79  	ov := this.table[i]
    80  
    81  	movNum := this.size - i - 1
    82  	if movNum > 0 {
    83  		copy(this.table[i:i+movNum], this.table[i+1:i+movNum])
    84  		//System.arraycopy(table, i + 1, table, i, movNum);
    85  	}
    86  	//table[--size] = 0;
    87  	last := this.size - 1
    88  	this.table[last] = 0
    89  	return ov
    90  }
    91  
    92  func (this *IntList) add(e int) {
    93  	this.ensure(this.size + 1)
    94  	//table[size++] = e;
    95  	this.table[this.size] = e
    96  	this.size++
    97  }
    98  
    99  func (this *IntList) AddAll(other *IntList) {
   100  	this.ensure(this.size + other.size)
   101  	for i := 0; i < other.size; i++ {
   102  		//this.table[this.size++] = other.table[i];
   103  		this.table[this.size] = other.table[i]
   104  		this.size++
   105  	}
   106  }
   107  
   108  func (this *IntList) AddAllArray(other []int) {
   109  	this.ensure(this.size + len(other))
   110  	n := len(other)
   111  	for i := 0; i < n; i++ {
   112  		//this.table[this.size++] = other[i];
   113  		this.table[this.size] = other[i]
   114  		this.size++
   115  	}
   116  }
   117  
   118  func (this *IntList) ToArray() []int {
   119  	newArray := make([]int, this.size)
   120  	copy(newArray, this.table)
   121  	return newArray
   122  }
   123  
   124  func (this *IntList) ToString() string {
   125  	return fmt.Sprintf("%v", this.table)
   126  }
   127  
   128  func (this *IntList) SetInt(i, v int) {
   129  	this.set(i, v)
   130  }
   131  func (this *IntList) SetFloat(i int, v float32) {
   132  	this.set(i, int(v))
   133  }
   134  func (this *IntList) SetLong(i int, v int64) {
   135  	this.set(i, int(v))
   136  }
   137  func (this *IntList) SetDouble(i int, v float64) {
   138  	this.set(i, int(v))
   139  }
   140  func (this *IntList) SetString(i int, v string) {
   141  	n, err := strconv.Atoi(v)
   142  	if err == nil {
   143  		this.set(i, n)
   144  	} else {
   145  		panic(" Parse error string(" + v + ") to int")
   146  	}
   147  
   148  }
   149  
   150  func (this *IntList) AddInt(v int) {
   151  	this.add(v)
   152  }
   153  func (this *IntList) AddFloat(v float32) {
   154  	this.add(int(v))
   155  }
   156  func (this *IntList) AddLong(v int64) {
   157  	this.add(int(v))
   158  }
   159  func (this *IntList) AddDouble(v float64) {
   160  	this.add(int(v))
   161  }
   162  func (this *IntList) AddString(v string) {
   163  
   164  	n, err := strconv.Atoi(v)
   165  	if err == nil {
   166  		this.add(n)
   167  	} else {
   168  		panic(" Parse error string(" + v + ") to int")
   169  	}
   170  }
   171  
   172  func (this *IntList) GetInt(i int) int {
   173  	return this.get(i)
   174  }
   175  func (this *IntList) GetFloat(i int) float32 {
   176  	return float32(this.get(i))
   177  }
   178  func (this *IntList) GetDouble(i int) float64 {
   179  	return float64(this.get(i))
   180  }
   181  func (this *IntList) GetLong(i int) int64 {
   182  	return int64(this.get(i))
   183  }
   184  func (this *IntList) GetString(i int) string {
   185  	return strconv.Itoa(this.get(i))
   186  }
   187  func (this *IntList) GetObject(i int) interface{} {
   188  	return nil
   189  }
   190  func (this *IntList) GetValue(i int) value.Value {
   191  
   192  	return value.NewDecimalValue(int64(this.get(i)))
   193  }
   194  
   195  func (this *IntList) Write(out *io.DataOutputX) {
   196  	out.WriteInt3(int32(this.Size()))
   197  	for i := 0; i < this.size; i++ {
   198  		out.WriteDecimal(int64(this.get(i)))
   199  	}
   200  }
   201  func (this *IntList) Read(in *io.DataInputX) {
   202  	count := int(in.ReadInt3())
   203  	for i := 0; i < count; i++ {
   204  		this.add(int(in.ReadDecimal()))
   205  	}
   206  }
   207  
   208  func (this *IntList) Size() int {
   209  	return this.size
   210  }
   211  
   212  func (this *IntList) Sorting(asc bool) []int {
   213  	this.lock.Lock()
   214  	defer this.lock.Unlock()
   215  
   216  	table := make([]*IntListKeyVal, this.size)
   217  	for i := 0; i < this.size; i++ {
   218  		table[i] = &IntListKeyVal{i, this.get(i)}
   219  	}
   220  
   221  	c := func(o1, o2 *IntListKeyVal) bool {
   222  		if asc {
   223  			if compare.CompareToInt(o1.value, o2.value) > 0 {
   224  				return false
   225  			} else {
   226  				return true
   227  			}
   228  		} else {
   229  			if compare.CompareToInt(o2.value, o1.value) > 0 {
   230  				return false
   231  			} else {
   232  				return true
   233  			}
   234  		}
   235  	}
   236  
   237  	sort.Sort(IntListSortable{compare: c, data: table})
   238  
   239  	out := make([]int, this.size)
   240  	for i := 0; i < this.size; i++ {
   241  		out[i] = table[i].key
   242  	}
   243  	return out
   244  }
   245  func (this *IntList) SortingAnyList(asc bool, child AnyList, childAsc bool) []int {
   246  	this.lock.Lock()
   247  	defer this.lock.Unlock()
   248  
   249  	table := make([]*IntListKeyVal, this.size)
   250  	for i := 0; i < this.size; i++ {
   251  		table[i] = &IntListKeyVal{i, this.get(i)}
   252  	}
   253  
   254  	c := func(o1, o2 *IntListKeyVal) bool {
   255  		var rt int
   256  		if asc {
   257  			rt = compare.CompareToInt(o1.value, o2.value)
   258  		} else {
   259  			rt = compare.CompareToInt(o2.value, o1.value)
   260  		}
   261  
   262  		if rt != 0 {
   263  			if rt > 0 {
   264  				return false
   265  			} else {
   266  				return true
   267  			}
   268  		} else {
   269  			rt = CompareChild(child, childAsc, o1.key, o2.key)
   270  			if rt > 0 {
   271  				return false
   272  			} else {
   273  				return true
   274  			}
   275  		}
   276  	}
   277  
   278  	sort.Sort(IntListSortable{compare: c, data: table})
   279  
   280  	out := make([]int, this.size)
   281  	for i := 0; i < this.size; i++ {
   282  		out[i] = table[i].key
   283  	}
   284  	return out
   285  }
   286  func (this *IntList) Filtering(index []int) AnyList {
   287  	out := NewIntList(this.size)
   288  	sz := len(index)
   289  	for i := 0; i < sz; i++ {
   290  		out.add(this.get(index[i]))
   291  	}
   292  	return out
   293  }
   294  
   295  type IntListSortable struct {
   296  	compare func(a, b *IntListKeyVal) bool
   297  	data    []*IntListKeyVal
   298  }
   299  
   300  func (this IntListSortable) Len() int {
   301  	return len(this.data)
   302  }
   303  
   304  func (this IntListSortable) Less(i, j int) bool {
   305  	return this.compare(this.data[i], this.data[j])
   306  }
   307  
   308  func (this IntListSortable) Swap(i, j int) {
   309  	this.data[i], this.data[j] = this.data[j], this.data[i]
   310  }
   311  
   312  type IntListKeyVal struct {
   313  	key   int
   314  	value int
   315  }
   316  
   317  func IntListMain() {
   318  	list := NewIntList(30)
   319  
   320  	for i := 0; i < 30; i++ {
   321  		list.AddInt(i)
   322  	}
   323  
   324  	fmt.Println("\nlist")
   325  	for i := 0; i < list.Size(); i++ {
   326  		fmt.Printf("%d ", list.GetInt(i))
   327  	}
   328  
   329  	fmt.Println("\nlist Sorting ASC")
   330  	ascList := list.Sorting(true)
   331  
   332  	for i := 0; i < len(ascList); i++ {
   333  		fmt.Printf("%d ", ascList[i])
   334  	}
   335  
   336  	fmt.Println("\nlist Sorting DESC")
   337  	descList := list.Sorting(false)
   338  
   339  	for i := 0; i < len(descList); i++ {
   340  		fmt.Printf("%d ", descList[i])
   341  	}
   342  
   343  	fmt.Println("\nlist Filtering")
   344  	var listFilter = make([]int, 30)
   345  
   346  	var fList = [30]int{3, 2, 1, 0, 4, 5, 6, 7, 8, 9, 10, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 29, 28, 27, 26, 25, 24, 23, 22, 21}
   347  
   348  	for i := 0; i < len(fList); i++ {
   349  		listFilter[i] = fList[i]
   350  	}
   351  	aList := list.Filtering(listFilter)
   352  
   353  	for i := 0; i < aList.Size(); i++ {
   354  		fmt.Printf("%d ", aList.GetInt(i))
   355  	}
   356  
   357  }