github.com/whatap/golib@v0.0.22/util/list/LongList.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 LongList struct {
    17  	size  int
    18  	table []int64
    19  	lock  sync.Mutex
    20  }
    21  
    22  func NewLongListDefault() *LongList {
    23  	o := new(LongList)
    24  	o.table = make([]int64, 0)
    25  	return o
    26  }
    27  
    28  func NewLongList(initialCapa int) *LongList {
    29  	o := new(LongList)
    30  	o.table = make([]int64, initialCapa)
    31  	return o
    32  }
    33  
    34  func (this *LongList) GetType() byte {
    35  	return ANYLIST_LONG
    36  }
    37  
    38  func (this *LongList) 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([]int64, newSize)
    53  		copy(newTable, this.table)
    54  		this.table = newTable
    55  	}
    56  }
    57  
    58  func (this *LongList) get(i int) int64 {
    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 *LongList) set(i int, v int64) int64 {
    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 *LongList) remove(i int) int64 {
    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 *LongList) add(e int64) {
    93  	this.ensure(this.size + 1)
    94  	//table[size++] = e;
    95  	this.table[this.size] = e
    96  	this.size++
    97  }
    98  
    99  func (this *LongList) AddAll(other *LongList) {
   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 *LongList) AddAllArray(other []int64) {
   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 *LongList) ToArray() []int64 {
   119  	newArray := make([]int64, this.size)
   120  	copy(newArray, this.table)
   121  	return newArray
   122  }
   123  
   124  func (this *LongList) ToString() string {
   125  	return fmt.Sprintf("%v", this.table)
   126  }
   127  
   128  func (this *LongList) Sort() {
   129  	if this.size > 0 {
   130  		//Arrays.sort(this.table, 0, size-1)
   131  		//sort.Ints(this.table)
   132  	}
   133  }
   134  
   135  func (this *LongList) SetInt(i, v int) {
   136  	this.set(i, int64(v))
   137  }
   138  func (this *LongList) SetFloat(i int, v float32) {
   139  	this.set(i, int64(v))
   140  }
   141  func (this *LongList) SetLong(i int, v int64) {
   142  	this.set(i, v)
   143  }
   144  func (this *LongList) SetDouble(i int, v float64) {
   145  	this.set(i, int64(v))
   146  }
   147  func (this *LongList) SetString(i int, v string) {
   148  	n, err := strconv.ParseInt(v, 10, 64)
   149  	if err == nil {
   150  		this.set(i, n)
   151  	} else {
   152  		panic(" Parse error string(" + v + ") to int64")
   153  	}
   154  
   155  }
   156  
   157  func (this *LongList) AddInt(v int) {
   158  	this.add(int64(v))
   159  }
   160  func (this *LongList) AddFloat(v float32) {
   161  	this.add(int64(v))
   162  }
   163  func (this *LongList) AddLong(v int64) {
   164  	this.add(v)
   165  }
   166  func (this *LongList) AddDouble(v float64) {
   167  	this.add(int64(v))
   168  }
   169  func (this *LongList) AddString(v string) {
   170  
   171  	n, err := strconv.ParseInt(v, 10, 64)
   172  	if err == nil {
   173  		this.add(n)
   174  	} else {
   175  		panic(" Parse error string(" + v + ") to int64")
   176  	}
   177  }
   178  
   179  func (this *LongList) GetInt(i int) int {
   180  	return int(this.get(i))
   181  }
   182  func (this *LongList) GetFloat(i int) float32 {
   183  	return float32(this.get(i))
   184  }
   185  func (this *LongList) GetDouble(i int) float64 {
   186  	return float64(this.get(i))
   187  }
   188  func (this *LongList) GetLong(i int) int64 {
   189  	return this.get(i)
   190  }
   191  func (this *LongList) GetString(i int) string {
   192  	return strconv.FormatInt(this.get(i), 10)
   193  	//return fmt.Sprintf("%d", this.get(i))
   194  }
   195  func (this *LongList) GetObject(i int) interface{} {
   196  	return nil
   197  }
   198  func (this *LongList) GetValue(i int) value.Value {
   199  
   200  	return value.NewDecimalValue(this.get(i))
   201  }
   202  
   203  func (this *LongList) Write(out *io.DataOutputX) {
   204  	out.WriteInt3(int32(this.Size()))
   205  	for i := 0; i < this.size; i++ {
   206  		out.WriteDecimal(this.get(i))
   207  	}
   208  }
   209  func (this *LongList) Read(in *io.DataInputX) {
   210  	count := int(in.ReadInt3())
   211  	for i := 0; i < count; i++ {
   212  		this.add(in.ReadDecimal())
   213  	}
   214  }
   215  
   216  func (this *LongList) Size() int {
   217  	return this.size
   218  }
   219  
   220  func (this *LongList) Sorting(asc bool) []int {
   221  	this.lock.Lock()
   222  	defer this.lock.Unlock()
   223  
   224  	table := make([]*LongListKeyVal, this.size)
   225  	for i := 0; i < this.size; i++ {
   226  		table[i] = &LongListKeyVal{i, this.get(i)}
   227  	}
   228  
   229  	c := func(o1, o2 *LongListKeyVal) bool {
   230  		if asc {
   231  			if compare.CompareToLong(o1.value, o2.value) > 0 {
   232  				return false
   233  			} else {
   234  				return true
   235  			}
   236  		} else {
   237  			if compare.CompareToLong(o2.value, o1.value) > 0 {
   238  				return false
   239  			} else {
   240  				return true
   241  			}
   242  		}
   243  	}
   244  
   245  	sort.Sort(LongListSortable{compare: c, data: table})
   246  
   247  	out := make([]int, this.size)
   248  	for i := 0; i < this.size; i++ {
   249  		out[i] = table[i].key
   250  	}
   251  	return out
   252  }
   253  func (this *LongList) SortingAnyList(asc bool, child AnyList, childAsc bool) []int {
   254  	this.lock.Lock()
   255  	defer this.lock.Unlock()
   256  
   257  	table := make([]*LongListKeyVal, this.size)
   258  	for i := 0; i < this.size; i++ {
   259  		table[i] = &LongListKeyVal{i, this.get(i)}
   260  	}
   261  
   262  	c := func(o1, o2 *LongListKeyVal) bool {
   263  		var rt int
   264  		if asc {
   265  			rt = compare.CompareToLong(o1.value, o2.value)
   266  		} else {
   267  			rt = compare.CompareToLong(o2.value, o1.value)
   268  		}
   269  
   270  		if rt != 0 {
   271  			if rt > 0 {
   272  				return false
   273  			} else {
   274  				return true
   275  			}
   276  		} else {
   277  			rt = CompareChild(child, childAsc, o1.key, o2.key)
   278  			if rt > 0 {
   279  				return false
   280  			} else {
   281  				return true
   282  			}
   283  		}
   284  	}
   285  
   286  	sort.Sort(LongListSortable{compare: c, data: table})
   287  
   288  	out := make([]int, this.size)
   289  	for i := 0; i < this.size; i++ {
   290  		out[i] = table[i].key
   291  	}
   292  	return out
   293  }
   294  func (this *LongList) Filtering(index []int) AnyList {
   295  	out := NewLongList(this.size)
   296  	sz := len(index)
   297  	for i := 0; i < sz; i++ {
   298  		out.add(this.get(index[i]))
   299  	}
   300  	return out
   301  }
   302  
   303  type LongListSortable struct {
   304  	compare func(a, b *LongListKeyVal) bool
   305  	data    []*LongListKeyVal
   306  }
   307  
   308  func (this LongListSortable) Len() int {
   309  	return len(this.data)
   310  }
   311  
   312  func (this LongListSortable) Less(i, j int) bool {
   313  	return this.compare(this.data[i], this.data[j])
   314  }
   315  
   316  func (this LongListSortable) Swap(i, j int) {
   317  	this.data[i], this.data[j] = this.data[j], this.data[i]
   318  }
   319  
   320  type LongListKeyVal struct {
   321  	key   int
   322  	value int64
   323  }
   324  
   325  func LongListMain() {
   326  	list := NewLongList(30)
   327  
   328  	for i := 0; i < 30; i++ {
   329  		list.AddInt(i)
   330  	}
   331  
   332  	fmt.Println("\nlist")
   333  	for i := 0; i < list.Size(); i++ {
   334  		fmt.Printf("%d ", list.GetInt(i))
   335  	}
   336  
   337  	fmt.Println("\nlist Sorting ASC")
   338  	ascList := list.Sorting(true)
   339  
   340  	for i := 0; i < len(ascList); i++ {
   341  		fmt.Printf("%d ", ascList[i])
   342  	}
   343  
   344  	fmt.Println("\nlist Sorting DESC")
   345  	descList := list.Sorting(false)
   346  
   347  	for i := 0; i < len(descList); i++ {
   348  		fmt.Printf("%d ", descList[i])
   349  	}
   350  
   351  	fmt.Println("\nlist Filtering")
   352  	var listFilter = make([]int, 30)
   353  
   354  	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}
   355  
   356  	for i := 0; i < len(fList); i++ {
   357  		listFilter[i] = fList[i]
   358  	}
   359  	aList := list.Filtering(listFilter)
   360  
   361  	for i := 0; i < aList.Size(); i++ {
   362  		fmt.Printf("%d ", aList.GetInt(i))
   363  	}
   364  }